21

I have a couple of commands I want executed after the machine boots up. A user shouldn't have to log in or anything for them to run.

I found this great answer suggesting crontab with the @reboot keyword, however this will run as root, and I'd like to run as a specific user created for these "services" (not actual system services).

What are someways to achieve that same effect with any given user?

1 Answers1

34

You could create a crontab for the specific user like so:

crontab -u <username> -e

Or more simply, you could just run crontab -e when logged in as that user.

Alternatively, you could prefix your command in your (root) crontab with sudo -u <username> to run the command as the specified user.

kraxor
  • 5,527
  • Thanks, I wasn't able to get it working with those. But I do now see that I can create a crontab for anyone. The commands wound up running fine as root with sudo. I didn't know I could use sudo there (was expecting it to fail waiting for a password). – Louis Waweru Jul 31 '14 at 22:22
  • 2
    @Louis sudo does not require a password when you invoke it as root. Basically you're not gaining permissions but giving them up. – kraxor Aug 01 '14 at 10:58
  • 1
    @kraxor - you could just run crontab -e when logged in as that user. i think this is not correct . – bhv Oct 20 '16 at 04:07
  • Could the specific user has root permissions during the run? – alper Nov 19 '20 at 14:45