在Ubuntu18下编写开机启动服务及脚本

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_21845263/article/details/83041879

Ubuntu18采用systemctl来管理开机启动的脚本,但是这里面有一些坑需要注意
举例来说

[Unit]
After=network.target

[Service]
Type=simple 
ExecStart=/usr/local/xxx.sh

[Install]
WantedBy=multi-user.target

1.编写的脚本/usr/local/xxx.sh的第一行一定要加上一行

#!/bin/sh

如果想让脚本在执行到过程中碰到错误就不往下面执行,将其改为

#!/bin/sh -e

如果不加上这一行,会出现莫名奇妙的错误,如图:
systemctl出错图片

10月 13 20:06:38 systemd[1]: Started .service.
10月 13 20:06:38  systemd[5542]: .service: Failed to execute command: Exec format error
10月 13 20:06:38  systemd[5542]: .service: Failed at step EXEC spawning /usr/local/.sh: Exec format error
10月 13 20:06:38 systemd[1]: .service: Main process exited, code=exited, status=203/EXEC
10月 13 20:06:38 systemd[1]: .service: Failed with result 'exit-code'.

猜你喜欢

转载自blog.csdn.net/qq_21845263/article/details/83041879