/etc/systemd/system/目录下创建xx.serviceSystemd unit文件

其一般默认格式如下

[Unit]
Description=My Shell Script Service
After=network.target

[Service]
ExecStart=/path/to/your_script.sh
WorkingDirectory=/path/to/script_directory
Restart=always
User=your_username

[Install]
WantedBy=default.target

问题

  • 当创建shell脚本为守护进程时,务必在脚本第一行加上解释器说明!
#!/usr/bin/shell
xxx你的代码xxx

否则,守护进程将错误退出!

  • 当修改完Systemd unit文件后,需要执行一行代码使其生效
systemctl daemon-reload

山和山不相遇,人与人要相逢