Centos/RHEL 8 systemd 服务无法从自定义位置引用脚本

Centos/RHEL 8 systemd 服务无法从自定义位置引用脚本

我正在尝试为某个位置(/usr/local/bin 除外..)中存在的简单脚本创建一个 systemd 服务

下面的脚本是在/home/vagrant/temp/test.sh

#!/bin/sh

MAX=500
i=0;
while true
do
i=$((i+1));
sleep 2
echo "$i = $(date)"
if [ $i == $MAX ]; then
  exit 0;
fi;
done;
fi

我创建了名为 的简单usr-print.service服务/etc/systemd/system/。文件的内容是

[Unit]
Description=Simple print service
After=network.target

[Service]

Type=simple
Restart=always
StandardOutput=journal
StandardError=journal

ExecStart=/home/vagrant/temp/test.sh

[Install]
WantedBy=multi-user.target

当我使用启动服务时,systemctl daemon-reload; systemctl start usr-print.service出现以下异常。

● usr-print.service - Simple print service
   Loaded: loaded (/etc/systemd/system/usr-print.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Mon 2021-08-02 06:10:39 UTC; 7s ago
  Process: 1504 ExecStart=/home/vagrant/temp/test.sh (code=exited, status=203/EXEC)
 Main PID: 1504 (code=exited, status=203/EXEC)

Aug 02 06:10:39 localhost.localdomain systemd[1]: usr-print.service: Main process exited, code=exited, status=203/EXEC
Aug 02 06:10:39 localhost.localdomain systemd[1]: usr-print.service: Failed with result 'exit-code'.
Aug 02 06:10:39 localhost.localdomain systemd[1]: usr-print.service: Service RestartSec=100ms expired, scheduling restart.
Aug 02 06:10:39 localhost.localdomain systemd[1]: usr-print.service: Scheduled restart job, restart counter is at 5.
Aug 02 06:10:39 localhost.localdomain systemd[1]: Stopped Simple print service.
Aug 02 06:10:39 localhost.localdomain systemd[1]: usr-print.service: Start request repeated too quickly.
Aug 02 06:10:39 localhost.localdomain systemd[1]: usr-print.service: Failed with result 'exit-code'.
Aug 02 06:10:39 localhost.localdomain systemd[1]: Failed to start Simple print service.

但是,如果我移动脚本并更新 usr-print.service 文件, ExecStart=/usr/local/bin/test.sh服务就会按预期启动。

有没有办法使用/home/vagrant/temp/test.sh服务文件中的路径?

Centos 7 -ExecStart=/home/vagrant/temp/test.sh在服务文件中工作(shell 正在运行)

Centos/RHEL 8 -ExecStart=/home/vagrant/temp/test.sh服务文件不起作用。

更新:

看起来 SELinux 无法执行该脚本,尽管它具有执行权限。

SELinux is preventing /usr/lib/systemd/systemd from execute access on the file /home/vagrant/temp/test.sh.
                                                              
*****  Plugin catchall (100. confidence) suggests   **************************

If you believe that systemd should be allowed execute access on the user-print-service file by default.
    Then you should report this as a bug.
    You can generate a local policy module to allow this access.
    Do allow this access for now by executing:
       # ausearch -c '(-service)' --raw | audit2allow -M my-service
       # semodule -X 300 -i my-service.pp

在 Redhat 中注意到与此相关的票证:https://bugzilla.redhat.com/show_bug.cgi?id=1832231

答案1

在本地运行的一种可能的方法是使用下面的

getenforce在 SELinux 上启用了强制模式

sudo setenforce 0禁用它并置于宽容模式。现在服务运行了。

但在生产环境中不建议这样做。

相关内容