How I can run init scripts as non-root user?

5,863

Solution 1

You could run it in crontab with the @reboot option

crontab -e to open up the user crontab.

add the line @reboot /path/to/script and it should start up your script on reboot as that user

Some more examples of cron use here

Solution 2

In Upstart, use the setuid keyword:

description "Run code for ProxyServer"
start on runlevel [23]

setuid mark
chdir /home/mark/selenium-client
exec java -jar selenium-server-standalone-2.20.0.jar -role hub -port 1111

(script is unnecessary.)

Alternatively, create a user job instead of a system one.

Solution 3

You could use e.g.

sudo -u mark exec java -jar selenium-server-standalone-2.20.0.jar -role hub -port 1111

to use the root privileges to execute the process as a specified user.


EDIT: Or perhaps

exec sudo -u mark java -jar selenium-server-standalone-2.20.0.jar -role hub -port 1111

depending on what upstart wants (I don't use it myself).

Share:
5,863

Related videos on Youtube

Mark Design
Author by

Mark Design

Updated on September 18, 2022

Comments

  • Mark Design
    Mark Design over 1 year

    I put this file in /etc/init/proxyserver.conf that execute this script at startup (Ubuntu 11.10):

    description "Run code for ProxyServer"
    start on runlevel [23]
    
    script
        cd /home/mark/selenium-client
        exec java -jar selenium-server-standalone-2.20.0.jar -role hub -port 1111
    end script
    

    This works fine but if I execute this in the terminal:

    ps aux | grep selenium
    

    It return this screen:

    root  783   0.0   2.3  680584   23688   ?   Ssl   Apr18   4:45   java -jar selenium-server-standalone-2.20.0.jar -role hub -port 1111
    

    But I want to run the script as non-root user, how I can do this?

    Thanks!

    • B T
      B T almost 8 years
      How about using su to run it as whatever user you want. You still would need root to set that up tho i think