1.系统级
cd /etc/systemd/system
sudo vim xxx.service
[Unit]
Description = blablablabla
[Service]
Type = simple
ExecStart = /bin/bash /脚本路径/demo.sh
Restart = always
[Install]
WantedBy = multi-user.target
启动服务
systemctl enable xxx.service #开机自启动
systemctl start xxx.service #单次启动
systemctl stop xxx.service #停止
systemctl status xxx.service #查看状态
添加或修改配置文件后,需要重新加载
systemctl daemon-reload
2.用户级
系统级会在root下运行。但我需要访问用户路径,在root权限在没有找到获得用户路径的方法,所以采用了用户级。
cd /etc/systemd/user
sudo vim xxx.service
[Unit]
Description = /opt/RA/screenrecording/videostart.sh when the computer starts up
[Service]
Type = simple
ExecStart = /bin/bash /opt/RA/screenrecording/videostart.sh
Restart = always
[Install]
WantedBy = default.target
systemctl --user enable xxx.service #开机自启动
systemctl --user start xxx.service #单次启动
systemctl --user stop xxx.service #停止
systemctl --user status xxx.service #查看状态
systemctl --user daemon-reload