14

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.

15

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.