News:

Wondering if this will always be free?  See why free is better.

Main Menu

Run serivce as non root user?

Started by ziycon, November 15, 2013, 08:47:24 AM

Previous topic - Next topic

ziycon

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

LiroyvH

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.
((U + C + I)x(10 − S)) / 20xAx1 / (1 − sin(F / 10))
President/CEO of Simple Machines - Server Manager
Please do not PM for support - anything else is usually OK.

Advertisement: