How to run an application at startup as a certain user?

Solution 1:

A startup system agnostic method. You can use this in /etc/rc.local, a /etc/init.d/... script, an Upstart configuration file and probably a systemd configuration too.

sudo -u oli /full/path/to/application

This command will run as the root user and sudo will drop down to the "oli" user. Obviously change the user and the command for your purposes.

I should add that there are at least a dozen ways of doing just this... But in my experience they're all largely identical in effect. Here's an upstart example using its setuid stanza:

start on (filesystem and stopped udevtrigger)
stop on runlevel [06]

setuid oli
respawn

exec /full/path/to/application

Solution 2:

It seems that the first answer doesn't work in Ubuntu 14.10 anymore.

This is how I do it there (put it in /etc/rc.local)

su <username> - -c "<command>"

Solution 3:

Not sure about older versions of Ubuntu, but recent ones also allow the use of @reboot (see e.g. here in the Ubuntu Wiki, scroll down a little or Ctrl+F your way to @reboot).

This might be a useful choice for when you want a user to be able to run their own commands at boot time without either having to become root (or have whoever has access to the root account do it for them) or even to have to login. If it works, it works without logging in.

So, as "bob", type crontab -e, and then add a new line at the bottom of the file that opens, such as:

# bottom of bob's crontab
@reboot /path/to/a_small_app

In case it doesn't work, you might want to check your environment variables, especially PATH. I usually set HOME and PATH explicitly atop the crontab. Something like:

# top of bob's crontab
HOME=/home/bob
PATH=/home/bob/.local/bin:/usr/local/bin:/usr/bin:/bin

A technique for figuring out why it might not be working:

# bottom of bob's crontab
* * * * * /path/to/a_small_app 2>&1 | tee /tmp/a_small_app.log # will run every minute

... and then open /tmp/a_small_app.log in your favorite editor after 60 seconds to see if there's any useful info in there.

When trying out @reboot today to initiate a tmux session within which the command-to-be-run should live, I've stumbled over a stubborn script today, which ended up working with this little trick. Showing it in combination with the above logging thing:

@reboot tmux new-session -d -s mysession "bash -c '/path/to/a_small_app 2>&1 | tee /tmp/a_small_app.log'"