乌班图22.04 开机执行cd /www/wwwroot/myapp/ yarn start 用 systemd服务模式

 

使用systemd服务来在Ubuntu上开机执行命令可以提供更灵活的控制和管理。以下是创建systemd服务的步骤:

1.创建一个新的服务单元文件。在终端中执行:

sudo nano /etc/systemd/system/my-app.service

2.在打开的编辑器中,添加以下内容:

由于用的yarn,需要添加node的环境变量

[Unit]
Description=my-App Service
After=network.target

[Service]
Type=simple
WorkingDirectory=/www/wwwroot/myapp/
ExecStart=/www/server/nodejs/v18.19.1/bin/yarn start
Restart=always
Environment="PATH=/www/server/nodejs/v18.19.1/bin:/usr/bin:/www/server/nodejs/v18.19.1/bin"
[Install]
WantedBy=multi-user.target

题外:命令行复制  ctrl+insert
   命令行粘贴 shift+insert

确保WorkingDirectoryExecStart的路径适用于你的实际设置。这个例子假设yarn的可执行文件在/usr/bin/目录中。如果不在这个目录,你需要调整路径。

3.启用和启动服务:

sudo systemctl enable my-app
sudo systemctl start my-app

 

4.如果需要检查服务状态或查看日志,可以使用以下命令:

sudo systemctl status my-app
sudo journalctl -u my-app   这个可以查看具体报错

 

5.如果报错需要重启

     重载配置 

    systemctl daemon-reload   

sudo systemctl restart my-app

 

另外记录rc.local

查看状态

systemctl status rc-local.service

启动

sudo systemctl start rc-local.service

开机启动

sudo systemctl enable rc-local.service

  

添加执行权限

sudo chmod +x /etc/rc.local

启动例子

#!/bin/bash
# This script is executed at the end of each multiuser runlevel
# Make sure that the script will "exit 0" on success or any other
# value on error.

# Add your custom commands below this line:

# Change to the desired directory
cd /www/wwwroot/my-app/

# Run yarn start command
yarn start

# Exit with success
exit 0

  这个which要用好,找到目录

which yarn

  

 

posted @ 2024-03-11 10:46  清风神剑  阅读(13)  评论(0编辑  收藏  举报