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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45776003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:35:36  来源:igfitidea点击:

Fixing a systemd service 203/EXEC failure (no such file or directory)

bashsystemd

提问by dwrz

I'm trying to set up a simple systemd timer to run a bash script every day at midnight.

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

systemctl --user status backup.servicefails and logs the following:

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'.

I'm lost, since the files and directories exist. The script is executable and, just to check, I've even set permissions to 777.

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

Some background:

一些背景:

The backup.timerand backup.serviceunit files are located in /home/user/.config/systemd/user.

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

backup.timeris loaded and active, and currently waiting for midnight.

backup.timer已加载并处于活动状态,目前正在等待午夜。

Here's what it looks like:

这是它的样子:

[Unit]
Description=Runs backup at 0000

[Timer]
OnCalendar=daily
Unit=backup.service

[Install]
WantedBy=multi-user.target

Here's backup.service:

这是backup.service

[Unit]
Description=backup

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

[Install]
WantedBy=multi-user.target

And lastly, this is a paraphrase of backup.sh:

最后,这是对以下内容的释义backup.sh

#!/usr/env/bin bash

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

The script runs fine if I execute it myself.

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

Not sure if it matters, but I use fishas my shell (started from .bashrc).

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

I'm happy to post the full script if that's helpful.

如果有帮助,我很高兴发布完整的脚本。

回答by dwrz

I think I found the answer:

我想我找到了答案:

In the .servicefile, I needed to add /bin/bashbefore the path to the script.

.service文件中,我需要/bin/bash在脚本路径之前添加。

For example, for backup.service:

例如,对于backup.service:

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

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

As opposed to:

与之相反:

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

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.targetinstead multi-user.target, though.

我不知道为什么。也许fish。另一方面,我为我的电子邮件运行了另一个脚本,并且服务文件似乎在没有/bin/bash. 它使用default.target代替multi-user.target,虽然。

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.

我遇到的大多数教程都没有预先准备好/bin/bash,但是我随后看到了这个 SO 答案,并认为值得一试。

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

服务文件执行脚本,计时器列在 中systemctl --user list-timers,所以希望这会起作用。

Update: I can confirm that everything is working now.

更新:我可以确认现在一切正常。

回答by ke4ukz

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.

当这发生在我身上时,是因为我的脚本有 DOS 行结尾,这总是弄乱脚本顶部的 shebang 行。我将其更改为 Unix 行结尾并且它起作用了。

回答by crizCraig

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

为简化起见,请确保在 ExecStart 脚本的顶部添加一个哈希爆炸,即

#!/bin/bash

python -u alwayson.py    

回答by Ivan Savcic

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.

没有#!/usr/env/bin,你是说#!/usr/bin/env

回答by Karl Pokus

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

Main process exited, code=exited, status=203/EXEC今天也遇到了,我的错误是我忘记将可执行位添加到文件中。

回答by Wirehead

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.

我实际上使用了如何将 node.js 应用程序作为后台服务运行?结合上面dwrz所说的。就我而言,我正在创建一个 Discord 机器人,当我不在时,它需要能够运行。

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 nodeat the top of my executed node.js script.

有了这项服务,我最初遇到了与最初发布者相同的错误,这让我来到了这里。我错过了#!/usr/bin/env node我执行的 node.js 脚本顶部的 。

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

从那时起,没有问题,虽然我打算看看还有什么可以扩展到服务本身。