Systemd don’t launch service as intended user

I have this SystemD script:

[Unit]
Description=RTC Client Services

[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

It launches the script correctly but always with root. Here is:

#!/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 $?

I tried to launch it with “su pers5i” and it behave correctly and I can’t figure out what’s the problem could be.

Any help is much appreciated.

Answer

According to https://www.freedesktop.org/software/systemd/man/systemd.exec.html. Perhaps ‘u’ in user must be capitalize to accomplish what you need.

[Unit] Description=RTC Client Services

[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

Attribution
Source : Link , Question Author : S4rg0n , Answer Author : horace

Leave a Comment