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.

2
  • Do you want the app to start before login in the login window? Jan 5, 2011 at 22:55
  • @Martin: Well, it doesn't really matter... I'm using Apache as an example here. When does it start? Jan 5, 2011 at 23:10

3 Answers 3

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
7
  • I suggest that the example
    – waltinator
    Sep 28, 2011 at 22:15
  • I get sudo: unknown group: 1004 Sep 4, 2014 at 5:01
  • Also, poke: Time to update line about upstart.
    – muru
    Jan 19, 2015 at 11:16
  • @muru I will. But group isn't required. sudo will use the specified user's default group.
    – Oli
    Jan 19, 2015 at 12:08
  • Better edit it out, then. That command's been wrong for three years since somebody improved it! :D
    – muru
    Jan 19, 2015 at 12:10
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'"
6
  • 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, 2021 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 Aug 9, 2023 at 9:15
  • you should consider adding your answer to this question: unix.stackexchange.com/questions/443569/… . It works well and feels more intuitive and linux-way than existing answers Aug 9, 2023 at 9:31
  • Done. Mulțumesc, Ciprian!
    – Sixtyfive
    Aug 9, 2023 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, 2023 at 19:55
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>"
1
  • If you need a login shell, the equivalent is sudo -i -u ....
    – muru
    Jan 19, 2015 at 11:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .