bash Systemd 脚本失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43956077/
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
Systemd script fail
提问by Rodrigo
I want to run a script at system startup in a Debian 9 box. My script works when run standalone, but fails under systemd.
我想在系统启动时在 Debian 9 机器中运行一个脚本。我的脚本在独立运行时有效,但在 systemd 下失败。
My script just copies a backup file from a remote server to the local machine:
我的脚本只是将备份文件从远程服务器复制到本地机器:
#!/bin/sh
set -e
/usr/bin/sshpass -p "PASSWORD" /usr/bin/scp -p [email protected]:ORIGINPATH/backupserver.zip DESTINATIONPATH/backupserver/
Just for privacy I replaced password, user, and paths above.
只是为了隐私,我替换了上面的密码、用户和路径。
I wrote the following systemd service unit:
我编写了以下 systemd 服务单元:
[Unit]
Description=backup script
[Service]
Type=oneshot
ExecStart=PATH/backup.sh
[Install]
WantedBy=default.target
Then I set permissions for the script:
然后我为脚本设置权限:
chmod 744 PATH/backup.sh
And installed the service:
并安装了服务:
chmod 664 /etc/systemd/system/backup.service
systemctl daemon-reload
systemctl enable backup.service
When I reboot the script fails:
当我重新启动脚本失败时:
● backup.service - backup script
Loaded: loaded (/etc/systemd/system/backup.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sat 2017-05-13 13:39:54 -03; 47min ago
Main PID: 591 (code=exited, status=1/FAILURE)
Result of journalctl -xe:
journalctl -xe 的结果:
mai 16 23:34:27 rodrigo-acer systemd[1]: backup.service: Main process exited, code=exited, status=6/NOTCONFIGURED
mai 16 23:34:27 rodrigo-acer systemd[1]: Failed to start backup script.
mai 16 23:34:27 rodrigo-acer systemd[1]: backup.service: Unit entered failed state.
mai 16 23:34:27 rodrigo-acer systemd[1]: backup.service: Failed with result 'exit-code'.
What could be wrong?
可能有什么问题?
回答by Rodrigo
Solved guys. There was 2 problems:
解决了伙计们。有2个问题:
1 - I had to change the service unit file to make the service run only after network was up. The unit section was changed to:
1 - 我必须更改服务单元文件才能使服务仅在网络启动后运行。单元部分改为:
[Unit]
Description = World server backup
Wants = network-online.target
After = network.target network-online.target
2 - The root user did not have the remote host added to the known host list, unlike the ordinary user I used to test the script.
2 - root 用户没有将远程主机添加到已知主机列表中,这与我用来测试脚本的普通用户不同。
回答by Tomachi
Failed with result 'exit-code' you could try this on your last line:
结果“退出代码”失败,您可以在最后一行尝试此操作:
# REQUIRED FOR SYSTEMD: 0 means clean no error
exit 0
You may also need to add:
您可能还需要添加:
Type=forking
to the systemd entry similar to: https://serverfault.com/questions/751030/systemd-ignores-return-code-while-starting-service
到类似于以下内容的 systemd 条目:https: //serverfault.com/questions/751030/systemd-ignores-return-code-while-starting-service
If your service or script does not fork add a & at the end to run it in the background, and exit with 0 fast. Otherwise it will be like a startup that times out and takes forever / seems like frozen service.
如果您的服务或脚本没有 fork,请在末尾添加 & 使其在后台运行,并以 0 快速退出。否则,它将像一个超时和永远需要的启动/看起来像冻结的服务。