我已经开发了一个 shell 脚本:
#!/bin/sh
# do some treatment
for i in `seq 0 10000`;do
# do some treatment and create/write into log file
done
注意:我的脚本手动运行完美,没有任何错误,大约需要 58 分钟
我已经创建了 systemd 文件以便在启动时以及每 60 分钟启动一次:
[Unit]
Description=my script service
DefaultDependencies=no
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=myscript.sh > /dev/null 2>&1
[Install]
WantedBy=multi-user.target
我的服务.定时器:
[Unit]
Description=Announce every 60 minutes
[Timer]
OnUnitActiveSec=60m
AccuracySec=10s
[email protected]
[Install]
WantedBy=default.target
之后我启用了服务和计时器
cd /lib/systemd/system/multi-user.target.wants && ln -s ../[email protected] [email protected]
cd /lib/systemd/system/timers.target.wants && ln -s ../myservice.timer
我的问题是脚本在启动时启动,但由于创建了日志文件并写入了一些日志数据,因此脚本停止了。当我检查 dmesg 时,我看到了以下日志:
>[ 4.074064] systemd[1]: Created slice system-myservice.slice.
>[ 4.076893] systemd[1]: Starting my script service service...
>[ 5.071797] systemd[1]: [email protected]: Main process exited, code=exited, status=2/INVALIDARGUMENT
>[ 5.074470] systemd[1]: Failed to start my script service service.
>[ 5.079797] systemd[1]: [email protected]: Unit entered failed state.
>[ 5.079849] systemd[1]: [email protected]: Failed with result 'exit-code'.
>[ 62.635246] systemd[1]: Starting my script service service...
>[ 62.789533] systemd[1]: [email protected]: Main process exited, code=exited, status=2/INVALIDARGUMENT
>[ 62.791783] systemd[1]: Failed to start my script service service.
>[ 62.810654] systemd[1]: [email protected]: Unit entered failed state.
>[ 62.810725] systemd[1]: [email protected]: Failed with result 'exit-code'.
>[ 105.064250] systemd[1]: Starting my script service service...
>[ 105.206430] systemd[1]: [email protected]: Main process exited, code=exited, status=2/INVALIDARGUMENT
>[ 105.208561] systemd[1]: Failed to start my script service service.
>[ 105.228600] systemd[1]: [email protected]: Unit entered failed state.
>[ 105.228669] systemd[1]: [email protected]: Failed with result 'exit-code'.
当我尝试手动启动它时也遇到问题:
systemctl start myservice@*.service
[ 1194.443225] systemd[1]: Failed to start my script service service.
Job for [email protected] failed because the control process exited with error code.
See "systemctl status [email protected]" and "journalctl -xe" for details.
服务状态如下:
systemctl status myservice@*.service
● [email protected] - my script service service
Loaded: loaded (/lib/systemd/system/multi-user.target.wants/../[email protected]; disabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Thu 2019-11-14 21:24:41 UTC; 1min 33s ago
Process: 1910 ExecStart=myscript.sh > /dev/null 2>&1 (code=exited, status=2)
Main PID: 1910 (code=exited, status=2)
为什么我会失败?我该如何解决这个问题?我尝试了很多解决方案来解决这个问题但仍然不起作用,我尝试让服务类型=空闲,类型=简单,...
答案1
您的状态输出中是这样说的:
Process: 1910 ExecStart=myscript.sh > /dev/null 2>&1 (code=exited, status=2)
这意味着你的脚本开始了,但它自行退出,并向操作系统提供返回代码 2。
一个可能的原因是您没有指定正确的 WorkingDirectory= 或正确的 User= 来满足脚本的期望。另一个可能的原因是,由于使用了 DefaultDependencies=no,您的脚本运行太早了并且无法访问尚未安装的文件系统或尚未检测到的设备。
请注意 ExecStart= 参数不是 shell 命令;它不支持重定向,只会将它们作为文字命令行参数传递给程序。确保您的脚本不会将这些参数视为致命错误。(如果您想阻止记录其 stdout,请改用 StandardOutput=null。)
还要注意,.timer 不会激活已经处于“活动”状态的单元。如果您指定 RemainAfterExit=true,则意味着 myscript.service 在第一次运行后将永远保持“活动”状态,因此它永远不会被计时器重新触发。