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

 

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

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

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.启用和启动服务:

1
2
sudo systemctl enable my-app
sudo systemctl start my-app

 

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

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

 

5.如果报错需要重启

     重载配置 

    systemctl daemon-reload   

1
sudo systemctl restart my-app

 

另外记录rc.local

查看状态

1
systemctl status rc-local.service

启动

1
sudo systemctl start rc-local.service

开机启动

1
sudo systemctl enable rc-local.service

  

添加执行权限

1
sudo chmod +x /etc/rc.local

启动例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/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要用好,找到目录

1
which yarn

  

 

posted @   清风神剑  阅读(113)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示