如何使用 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.

相关内容