Linux – How to run the cron job as a user instead of root user


I have few bash scripts which are adding to cron jobs with specified timing, but it needs to be executed as root user. I am trying to run those scripts i.e., crob jobs but it needs root user permission, since I am running this jobs in ubuntu ec2 instance where root user is restricted. What would be the work around to run those scripts as root user.


Best Answer

I'm not familiar with EC2, but you can define a list of commands which can be executed by sudo without password prompt. Run visudo and insert something like this:

Cmnd_Alias     CRONJOB = /full/path/to/command1, /full/path/to/command2, ...

after that just use sudo in the bash scripts and put these in your cron job.

*/1 * * * * /path/to/the/script1
* */2 * * * /path/to/the/script2