22

Is there a way to have an application launch during startup under a certain user account?

For example, I would like to have a_small_app run under the user account bob (which is in the group bobsgroup). Do I add something to /etc/init.d?

Note: I don't want the application to start after a user logs in but rather when the computer starts.

Flyk
  • 1,480
Nathan Osman
  • 32,155

3 Answers3

17

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
Oli
  • 293,335
4

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'"
Sixtyfive
  • 141
  • 1
    If you don't want to mess with bob's crontab, you may alternatively edit the system-wide /etc/crontab and add the following line there: @reboot bob /path/to/a_small_app. Or put this line into a separate file (say myapp) placed in directory /etc/cron.d – raj Aug 12 '21 at 12:43
  • awesome ! I came here exactly to see how to start tmux at login, so that last part was the cherry on top. I would add that I passed -l to bash to make it a login shell and thus set my PATH correctly, without headache of specifying full path to the script – Ciprian Tomoiagă Aug 09 '23 at 09:15
  • you should consider adding your answer to this question: https://unix.stackexchange.com/questions/443569/start-tmux-and-execute-a-set-of-commands-on-boot . It works well and feels more intuitive and linux-way than existing answers – Ciprian Tomoiagă Aug 09 '23 at 09:31
  • Done. Mulțumesc, Ciprian! – Sixtyfive Aug 09 '23 at 13:58
  • I found that the HOME variable is already set appropriately. Yet beware that in this context, the command execution does not have a terminal attached, so commands like watch do not work. – dvo Nov 14 '23 at 19:55
  • Hence "if". I've had missing $HOME, etc. in the wild, esp. in legacy systems. – Sixtyfive Nov 15 '23 at 13:44
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>"
Chai T. Rex
  • 5,193
vrodic
  • 21
  • 1