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

42,132

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

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'"

Solution 3

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>"
Share:
42,132

Related videos on Youtube

Nathan Osman
Author by

Nathan Osman

Email: [email protected] I am both an Ubuntu user and Ubuntu member. By profession, I am a software developer and I work with C++, Python, and (more recently) Go. I enjoy tinkering with different things like motion tracking in Blender, creating an Android app for time-lapse photography, or writing Linux kernel modules. - 2buntu - community blog that I sometimes contribute to - NitroShare - a cross-platform network file transfer utility - REST Easy - Firefox add-on for analyzing HTTP responses

Updated on September 17, 2022

Comments

  • Nathan Osman
    Nathan Osman over 1 year

    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.

    • Martin Owens -doctormo-
      Martin Owens -doctormo- over 13 years
      Do you want the app to start before login in the login window?
    • Nathan Osman
      Nathan Osman over 13 years
      @Martin: Well, it doesn't really matter... I'm using Apache as an example here. When does it start?
  • goo
    goo over 12 years
    I suggest that the example
  • ThorSummoner
    ThorSummoner over 9 years
    I get sudo: unknown group: 1004
  • muru
    muru over 9 years
    If you need a login shell, the equivalent is sudo -i -u ....
  • muru
    muru over 9 years
    Also, poke: Time to update line about upstart.
  • Oli
    Oli over 9 years
    @muru I will. But group isn't required. sudo will use the specified user's default group.
  • muru
    muru over 9 years
    Better edit it out, then. That command's been wrong for three years since somebody improved it! :D
  • raj
    raj over 2 years
    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