修复 Systemd 服务203/EXEC 失败(没有这样的文件或目录)

我试图设置一个简单的 Systemd 计时器,以便每天午夜运行 bash 脚本。

systemctl --user status backup.service失败并记录以下内容:

backup.service: Failed at step EXEC spawning /home/user/.scripts/backup.sh: No such file or directory.


backup.service: Main process exited, code=exited, status=203/EXEC
Failed to start backup.
backup.service: Unit entered failed state.
backup.service: Failed with result 'exit-code'.

我迷路了,因为文件和目录都存在。该脚本是可执行的,只是为了检查,我甚至将权限设置为777。

一些背景:

backup.timerbackup.service单元文件位于 /home/user/.config/systemd/user中。

backup.timer已加载并激活,目前正在等待午夜。

看起来是这样的:

[Unit]
Description=Runs backup at 0000


[Timer]
OnCalendar=daily
Unit=backup.service


[Install]
WantedBy=multi-user.target

这里是 backup.service:

[Unit]
Description=backup


[Service]
Type=oneshot
ExecStart=/home/user/.scripts/backup.sh


[Install]
WantedBy=multi-user.target

最后,这是 backup.sh的一个解释:

#!/usr/env/bin bash


rsync -a --delete --quiet /home/user/directory/ /mnt/drive/directory-backup/

如果我自己执行,脚本运行得很好。

不确定这是否重要,但是我使用 fish作为 shell (从.bashrc 开始)。

如果有用的话,我很乐意发布完整的剧本。

243484 次浏览

I think I found the answer:

In the .service file, I needed to add /bin/bash before the path to the script.

For example, for backup.service:

ExecStart=/bin/bash /home/user/.scripts/backup.sh

As opposed to:

ExecStart=/home/user/.scripts/backup.sh

I'm not sure why. Perhaps fish. On the other hand, I have another script running for my email, and the service file seems to run fine without /bin/bash. It does use default.target instead multi-user.target, though.

Most of the tutorials I came across don't prepend /bin/bash, but I then saw this SO answer which had it, and figured it was worth a try.

The service file executes the script, and the timer is listed in systemctl --user list-timers, so hopefully this will work.

Update: I can confirm that everything is working now.

When this happened to me it was because my script had DOS line endings, which always messes up the shebang line at the top of the script. I changed it to Unix line endings and it worked.

I actually used the answer from How do I run a node.js app as a background service? combined with what dwrz said above. In my case, I was creating a Discord bot that needed to be able to run when I was not around.

With this service in place, I initially got the same error that the initial poster did, which brought me here. I was missing the #!/usr/bin/env node at the top of my executed node.js script.

Since then, no problems, although I intend to see what else can be extended to the service itself.

To simplify, make sure to add a hash bang to the top of your ExecStart script, i.e.

#!/bin/bash


python -u alwayson.py

If that is a copy/paste from your script, you've permuted this line:

#!/usr/env/bin bash

There's no #!/usr/env/bin, you meant #!/usr/bin/env.

I ran across a Main process exited, code=exited, status=203/EXEC today as well and my bug was that I forgot to add the executable bit to the file.

try running:

systemctl daemon-reload

and then again run

service <yourservice> status

I faced a similar issue, changed the permission and added executable permission

use chmod +x /etc/systemd/system/<service-filename>

This worked for me