如何使用 systemd 服務執行 bash 腳本?

如何使用 systemd 服務執行 bash 腳本?

我想透過 systemd 啟動圖形應用程式。我之前嘗試透過 udev 執行此操作,但應用程式在規則終止後被終止。當我直接透過 systemd 運行應用程式時它可以工作,但當我運行帶有一些約束的啟動應用程式的腳本時它就不起作用。

我的服務文件如下圖所示:

[Unit]
Description=App starter

[Service]
Type=oneshot
RemainAfterExit=yes
Environment=DISPLAY=:0
Environment=HOME=/home/user/
ExecStart=/home/user/runApp

[Install]
WantedBy=multi-user.target

Journalctl -xe 給了我

Sep 25 13:50:00 host systemd[1]: Starting App starter...
Subject: A start job for unit app.service has begun execution
Defined-By: systemd
Support: https://forum.manjaro.org/c/support
 
A start job for unit xournalpp.service has begun execution.
 
The job identifier is 19196.
Sep 25 13:50:00 host runApp[313232]: App is not running
Sep 25 13:50:00 host systemd[1]: app.service: Main process exited, code=exited, status=1/FAILURE
Subject: Unit process exited
Defined-By: systemd
Support: https://forum.manjaro.org/c/support
 
An ExecStart= process belonging to unit xournalpp.service has exited.
 
The process' exit code is 'exited' and its exit status is 1.
Sep 25 13:50:00 host systemd[1]: app.service: Failed with result 'exit-code'.
Subject: Unit failed
Defined-By: systemd
Support: https://forum.manjaro.org/c/support
 
The unit xournalpp.service has entered the 'failed' state with result 'exit-code'.
Sep 25 13:50:00 host systemd[1]: Failed to start App starter.
Subject: A start job for unit app.service has failed
Defined-By: systemd
Support: https://forum.manjaro.org/c/support
 
A start job for unit app.service has finished with a failure.
 
The job identifier is 19196 and the job result is failed.

這是腳本。可能是由於後台運行所致&?需要這個,因為我想重用該腳本。

#!/bin/bash

  if pgrep app
  then
      echo App is running
          wmctrl -x -a app
          wmctrl -x -r app -b "add,maximized_vert,maximized_horz"
          
  else
          echo App is not running
          /usr/bin/app & disown
  fi

答案1

可能是權限問題。

/home/user通常不允許任何其他使用者存取它們,並且像 apparmor 和 selinux 這樣的安全系統通常禁止這種使用。

如果你想在systemd中使用腳本,你最好把它放在/usr/local/bin.

相關內容