Systemd 未按照预期用户启动服务

Systemd 未按照预期用户启动服务

我有这个 SystemD 脚本:

[单元] 描述=RTC 客户端服务

[Service]
user=linuxuser
WorkingDirectory=/usr/lib/systemd/scripts/
Type=forking
ExecStart=/bin/bash rtc_client.sh start
ExecStop=/bin/bash rtc_client.sh stop
Restart=no
RemainAfterExit=no
TimeoutStartSec=0
TimeoutStopSec=60

[Install]
WantedBy=multi-user.target

它可以正确启动脚本,但始终需要 root 权限。如下所示:

#!/bin/bash

RTCENGINEID=$HOSTNAME'_engine'
RTCUSER='RTCUSER'
RTCPW='RTCPWD'
RTCSERVER='SERVER'
RTCSERVERPORT='9443'
RTCREPOSITORY=https://$RTCSERVER:$RTCSERVERPORT/ccm
export WORKDIR='/opt/ibm/buildsystemtoolkit/buildsystem/buildengine/eclipse'
export JAVACMD=/opt/ibm/java-s390x-71/jre/bin/java
export ARGS="-cp ./plugins/org.eclipse.equinox.launcher_1.1.1.R36x_v20101122_1400.jar org.eclipse.equinox.launcher.Main -application com.ibm.team.build.engine.jazzBuildEngine -repository $RTCREPOSITORY -engineId $RTCENGINEID -userId $RTCUSER -pass $RTCPW"
RTCJAR=org.eclipse.equinox.launcher
PIDFILE='/var/run/rtc_client/rtc_client.pid'
DEBUGLOG='/tmp/rtc_debug.log'

. /home/linuxuser/.bash_profile

start() {

        cd $WORKDIR
        nohup $JAVACMD $ARGS > $DEBUGLOG &
        sleep 10
        pgrep -f $RTCJAR > $PIDFILE
        echo "USER IS: " $(whoami) | tee -a $DEBUGLOG
        echo "PID IS: " $(cat $PIDFILE) | tee -a $DEBUGLOG

}


stop() {

        echo "USER IS: " $(whoami) | tee -a $DEBUGLOG
        echo "PID IS: " $(cat $PIDFILE) | tee -a $DEBUGLOG
        kill $(cat $PIDFILE)
        rm -f $PIDFILE

}

restart() {

        stop
        start

}

reload() {

        restart

}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        restart
        ;;
  reload)
        reload
        ;;
  *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
esac

exit $?

我尝试用“su pers5i”启动它,它运行正常,但我不明白问题出在哪里。

任何帮助深表感谢。

答案1

根据https://www.freedesktop.org/software/systemd/man/systemd.exec.html。也许 user 中的 'u' 必须大写才能满足您的需要。

[单元] 描述=RTC 客户端服务

[服务]
ser=linux用户
工作目录=/usr/lib/systemd/scripts/
类型=分叉
ExecStart=/bin/bash rtc_client.sh 启动
ExecStop=/bin/bash rtc_client.sh 停止
重新启动=否
退出后保留=否
超时启动时间=0
超时停止时间=60

[安装]
WantedBy=多用户.目标

相关内容