用於執行 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

相關內容