背景:
想在服务器开机启动的时候,自动执行python脚本,于是就把它做成了linux服务的形式。
在/etc/systemd/system/myclock.service
[Unit]
Description=my first linux service
[Service]
Type=simple
ExecStart=/data/mytools/python38/bin/python3 /data/myclock.py
[Install]
WantedBy=multi-user.target
在输入以下命令后:
systemctl daemon-reload
systemctl start myclock.service
systemctl status myclock.service
发现服务没起来,而且报错:
Failed at step EXEC spawning /data/mytools/python38/bin/python3 No such file or directory
原因分析:
由于/data分区是通过nfs挂载的,所以猜测是systemd启动myclock.service服务的时候,磁盘还没有挂载上,导致找不到文件或目录。
所以需要修改myclock.service服务的配置,使它在指定分区/data挂载后再启动。
解决方案:
在[Unit]中增加一行RequiresMountsFor=/data/mytools/python38/bin
[Unit]
Description=my first linux service
RequiresMountsFor=/data/mytools/python38/bin
[Service]
Type=simple
ExecStart=/data/mytools/python38/bin/python3 /data/myclock.py
[Install]
WantedBy=multi-user.target
然后重新启动就OK了:
systemctl daemon-reload
systemctl start myclock.service
systemctl status myclock.service
参考:
linux - Failed at step EXEC spawning: No such file or directory - Super User
CentOS 7 系统服务配置--单元(Unit)_requiresmountsfor_CodingSir的博客-CSDN博客