首页 > 解决方案 > systemd 不启动服务 - 在步骤 USER spawning 失败

问题描述

我正在尝试编写一个 systemd 服务脚本。它从 root 用户创建非登录用户开始,并赋予他特权。然后 nologin 用户启动应用程序。

我正在使用 Linux-5.0.7-2019.05.28.x86_64 的 rhel-7.5 (Maipo)。这是我尝试过的。

/root/myhome/my_setup.sh:

#!/bin/bash

# Create nologin user with workingdir. Make hime owner for DB files, binary files he runs.
crdb_setup() {
    /bin/mkdir -p /var/lib/lsraj /root/crdb || return $?
    /usr/bin/getent group lsraj || /usr/sbin/groupadd -g 990 lsraj|| return $?
    /usr/bin/getent passwd lsraj || /usr/sbin/useradd -u 990 -g 990 \
        -c 'CRDB User' -d /var/lib/lsraj -s /sbin/nologin -M -K UMASK=022 lsraj || return $?
    /bin/chown lsraj:lsraj /var/lib/lsraj /root/crdb /root/myhome/cockroach || return $?
}

crdb_setup


[root@lsraj ~]# 
total 99896
-rwxr-xr-x 1 root root 102285942 Jun 18 16:54 cockroach
-rwxr-xr-x 1 root root       521 Jun 18 17:07 my_setup.sh
[root@lsraj ~]# 

服务脚本:

[root@lsraj~]# cat /usr/lib/systemd/system/lsraj.service 
[Unit]
Description=Cockroach Database Service
After=network.target syslog.target

[Service]
Type=notify
# run the script with root privileges. The script creates user and gives him privileges.
ExecStartPre=+/root/myhome/my_setup.sh
User=lsraj
Group=lsraj
WorkingDirectory=/var/lib/lsraj
ExecStart=/root/myhome/cockroach start --insecure --host=localhost --store=/root/crdb
ExecStop=/root/myhome/cockroach quit --insecure --host=localhost
StandardOutput=journal
Restart=on-failure
RestartSec=60s
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=cockroachdb
[Install]
WantedBy=multi-user.target
[root@lsraj~]# 


Jun 18 17:30:51 lsraj systemd: [/usr/lib/systemd/system/lsraj.service:8] Executable path is not absolute, ignoring: +/root/myhome/my_setup.sh
Jun 18 17:30:51 lsraj systemd: Starting Cockroach Database Service...
Jun 18 17:30:51 lsraj systemd: Failed at step USER spawning /root/myhome/cockroach: No such process
Jun 18 17:30:51 lsraj systemd: lsraj.service: main process exited, code=exited, status=217/USER
Jun 18 17:30:51 lsraj systemd: Failed at step USER spawning /root/myhome/cockroach: No such process
Jun 18 17:30:51 lsraj systemd: lsraj.service: control process exited, code=exited status=217
Jun 18 17:30:51 lsraj systemd: Failed to start Cockroach Database Service.
Jun 18 17:30:51 lsraj systemd: Unit lsraj.service entered failed state.
Jun 18 17:30:51 lsraj systemd: lsraj.service failed.

标签: bashredhatsystemd

解决方案


我已将我的评论移至此处以支持更丰富的格式。

我无法就您对“+”的需求提出建议,我只是在为您阅读错误消息,它说 systemd 忽略了 ExecStartPre 路径,因为它不是绝对的。

也许这是 freedesktop.org 中存在的一个功能,但是我的 Redhat 7.6 版本(您指出您正在使用的版本)在systemd.service单元文件手册页中没有包含类似的语句(或表)。另外,您会在单元文件中收到关于该行的非常清晰的错误消息。

它提到“-”和“@”的手册页,但其他都没有......

这是手册页的摘录(我在上面提供了指向完整页面的链接)。

       ExecStartPre=, ExecStartPost=
           Additional commands that are executed before or after the command in ExecStart=, respectively. Syntax is the same as for ExecStart=, except that multiple command lines are
           allowed and the commands are executed one after the other, serially.

           If any of those commands (not prefixed with "-") fail, the rest are not executed and the unit is considered failed.

           Note that ExecStartPre= may not be used to start long-running processes. All processes forked off by processes invoked via ExecStartPre= will be killed before the next service
           process is run.

我建议先尝试删除“+”,看看会发生什么,然后从那里开始。


推荐阅读