Simple Machines Community Forum

SMF Support => Server Performance and Configuration => Topic started by: ziycon on November 15, 2013, 08:47:24 AM

Title: Run serivce as non root user?
Post by: ziycon on November 15, 2013, 08:47:24 AM
Hi all, having an issue, i have the below service setup and it runs grand, only thing is that I need it to run as a particular user and not root, I've tried many thing for example, adding in '--user={username}' between the daemon and config entries with no luck.

Any help is greatly appreciated.

#!/bin/bash

CONFIG="/opt/customapp/new_serv.conf"
DAEMON="/opt/customapp/new_serv"

USER="myuser"

# Checking for customapp binary
test -f $DAEMON || exit 0

# The init commands
case "$1" in
        start)
                echo "Starting customapp server..."
                su - $USER -c $DAEMON $CONFIG  > /dev/null 2>&1 &
                ;;
        stop)
                echo "Stopping customapp server..."
                kill -9 `ps -C new_serv -o pid --no-headers`
                ;;
        restart)
                echo "Stopping customapp server..."
                kill -9 `ps -C new_serv -o pid --no-headers`
                echo "Starting customapp server..."
                su - $USER -c $DAEMON $CONFIG  > /dev/null 2>&1 &
                ;;
        *)
                echo "usage: /etc/init.d/customapp"
                echo "$0 {start | stop | restart}"
                exit 1
                ;;
esac
Title: Re: Run serivce as non root user?
Post by: LiroyvH on November 16, 2013, 12:25:09 PM
By the looks of that config file, you define the user it must run under here:

USER="myuser"


Which when you start it triggers:
Quote
su - $USER -c $DAEMON $CONFIG  > /dev/null 2>&1 &

So that should work just fine and if you look in the process list it should be running under the user you define there.
The user must exist of course.