15

As a non-root user, I want to run a background job when the system boots. It's sort of a service which doesn't require root privilege. Is there a way to do it?

One way is to put sudo -u user command in rc.local, but editing rc.local requires root privilege.

Another way is to launch it from cron every minute and check for any running instance, but firstly it wakes up the system unnecessarily and secondly, there can be race condition in checking running instances.

A third way is to run it in ~/.bash_profile, but I want to start it without user login.

1 Answer 1

19

You can use cron if your version has the @reboot feature. From man 5 crontab:

Instead of the first five fields, one of eight special strings may appear:

  string         meaning
  ------         -------
  @reboot        Run once, at startup.
  …

You can edit a user-local crontab with the command crontab -e without root privileges. Then add the following line:

@reboot /usr/local/bin/some-command

Now your command will be run once at boot time.

2
  • Is it unusual for crontab -e to create the cron file under /tmp? Commented Jun 2, 2017 at 23:07
  • @Elliott That depends on your editor. In the end the file will end up in /var/spool/cron/crontabs.
    – Marco
    Commented Jun 5, 2017 at 11:16

You must log in to answer this question.

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