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
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.
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.
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.
Let me also clarify about situations 1 and 2: cron jobs run independently of any user login sessions, and any programs they're running.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With