Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run the cron job as a user instead of root user [closed]

Tags:

linux

bash

cron

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.

Thanks

like image 305
Jeevan Dongre Avatar asked Nov 18 '11 06:11

Jeevan Dongre


People also ask

How do I run a cron job as a specific user?

You can run a custom cronjob by adding a new line in the /etc/crontab file and specifying the user who will run the command as in the following example. #m h dom mon dow user command * * * * * someuser echo 'Hello world!' Copied! This will echo Hello world! in the terminal every minute.

Does crontab run as root or user?

Like any other user, root has a user crontab. Essentially the same as any other user crontab, you are editing the root crontab when you run sudo crontab -e . Jobs scheduled in the root user crontab will be executed as root with all of its privileges.

Do cron jobs need to run as root?

They all run as root . If you need otherwise, use su in the script or add a crontab entry to the user's crontab ( man crontab ) or the system-wide crontab (whose location I couldn't tell you on CentOS). Save this answer.

Do cron jobs run when user is not logged in?

Let me also clarify about situations 1 and 2: cron jobs run independently of any user login sessions, and any programs they're running.


1 Answers

You can make a script execute as root by using the setuid flag, which makes a script run as its owner:

chmod +s yourscript
chown root yourscript

Just make yourscript run whatever command you want to run as root.

Note that with this method, any user can run the script.

like image 110
Brendan Long Avatar answered Oct 11 '22 04:10

Brendan Long