drenda
drenda

Reputation: 6244

Ubuntu run script as non-root user

I've an init script (/etc/init.d) that should run a my executable jar file as a serviceat boot. I need that this script is runned by a specified user.

With su & sudo is possibile but it split the process and I don't like this.

There is another way to run this script as limited user?

This is the relevant part of my init script:

#!/bin/bash

APP_NAME="myapp"
APP_HOME=/home/user1/jetty
JAVA_HOME=/opt/local/java/latest
echo "Service $APP_NAME - [$1]"

echo "JAVA_HOME -> $JAVA_HOME"
echo "APP_HOME -> $APP_HOME"
echo "APP_NAME -> $APP_NAME"

function start {
    if pkill -0 -f $APP_NAME.jar > /dev/null 2>&1
    then
        echo "Service [$APP_NAME] is already running. Ignoring startup request."
        exit 1
    fi
    echo "Starting application..."
    cd $APP_HOME
    nohup $JAVA_HOME/bin/java -jar $APP_HOME/$APP_NAME.jar\
        < /dev/null > $APP_HOME/logs/app.log 2>&1 &
}

Upvotes: 0

Views: 469

Answers (1)

ccarton
ccarton

Reputation: 3666

On Ubuntu you should use the program start-stop-daemon for this. It has options for launching daemons as different users, managing pid files, changing the working directory, and pretty much anything else that is usually needed by init scripts.

Upvotes: 1

Related Questions