用于执行 shell 脚本的简单 systemd 服务

用于执行 shell 脚本的简单 systemd 服务

我试图启动一个简单的 .sh 文件,它说Failed at step EXEC spawning /etc/start.sh: No such file or directory

内容gnome.service

[Unit]
Description=Description for sample script goes here
After=network.target
RequiresMountsFor=/etc/

[Service]
Type=simple
ExecStart=/etc/start.sh
TimeoutStartSec=0

[Install]
WantedBy=default.target

内容start.sh

#!/bin/bash
echo "This is a sample script to test auto run during boot" > /root/Documents/script.txt
echo "The time the script run was -->  `date`" >> /root/Documents/script.txt

输出systemctl status gnome -l

● gnome.service - Description for sample script goes here
   Loaded: loaded (/etc/systemd/system/gnome.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Thu 2019-06-06 18:25:32 CEST; 3s ago
  Process: 11713 ExecStart=/etc/start.sh (code=exited, status=203/EXEC)
 Main PID: 11713 (code=exited, status=203/EXEC)

Jun 06 18:25:32 some.server.adress systemd[1]: Started Description for sample script goes here.
Jun 06 18:25:32 some.server.adress systemd[11713]: Failed at step EXEC spawning /etc/start.sh: No such file or directory
Jun 06 18:25:32 some.server.adress systemd[1]: gnome.service: main process exited, code=exited, status=203/EXEC
Jun 06 18:25:32 some.server.adress systemd[1]: Unit gnome.service entered failed state.
Jun 06 18:25:32 some.server.adress systemd[1]: gnome.service failed.

输出ls -l /etc/start.sh

-rwxr-xr-x 1 root root 179 Jun  6 18:19 /etc/start.sh

输出/etc/start.sh

-bash: /etc/start.sh: /bin/bash^M: bad interpreter: No such file or directory

答案1

我在 Windows 上使用 Notepad++,所以我必须运行

sed -i -e 's/\r$//' /etc/start.sh

替换 Windows 行结尾。

^M 是回车符。 Linux 使用换行符来标记行结束,而 Windows 使用两个字符序列 CR LF。您的文件具有 Windows 行结尾,这会让 Linux 感到困惑。

答案主题: https://askubuntu.com/questions/304999/not-able-to-execute-a-sh-file-bin-bashm-bad-interpreter

相关内容