Ubuntu20.04 配置脚本或者服务开机自启动

论坛 期权论坛 金融     
n7mase   2022-6-26 08:29   2663   0
这里介绍,我认为最简单的方式,当然也还有其他方式
我的系统是Ubuntu 20.04
我配置的是一个scratch-gui在线可视化编程网站的自启动服务
启动脚本为:
cd /home/ubuntu/lite
npm run start想让服务起每次重启,自动执行这段脚本,不需要我每次上服务器去配置。
原理:自定义服务文件,添加到系统服务,通过Systemctl管理
先在cd /home/ubuntu/lite目录下新建脚本
vim start.sh输入内容为
#!/bin/bash
cd /home/ubuntu/lite
npm run start设置脚本权限 创建系统服务
chmod +x start.sh
sudo vim /lib/systemd/system/scratch.service内容为:
[Unit]
Description=scratch service
After=network.target network-online.target syslog.target
Wants=network.target network-online.target

[Service]
Type=simple

#启动服务的命令(命令必须写绝对路径)
ExecStart=bash /home/ubuntu/lite/start.sh

[Install]
WantedBy=multi-user.target命令只写了启动的,重启、停止等可以根据自己情况添加。
允许开机自启:
systemctl enable scratch.service其他命令
启动 sudo systemctl start scratch
重启 sudo systemctl restart scratch
停止 sudo systemctl stop scratch
日志 sudo systemctl status scratch其他服务或者命令、脚本也可以用类似方式,实现开机自启。
详细可参考:Linux Ubuntu 20.04 -添加开机启动(服务/脚本)
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:
帖子:
精华:
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP