I'm trying to execute a shell script as a cron job. The problem is that my script needs to be run as a super user. I tried it with just writing sudo
infront of the command in my crontab
file, but it didn't work.
How to execute sudo commands as a cron job
cronsudo
Related Question
- How to root launch a regular Mac application from a cron job in /etc/crontab
- MacOS – Cron script not executing on Mavericks
- MacOS – Root crontab won’t save
- MacOS – Cron job keeps running, not in any crontabs
- Running Cron Job w/ Sudo permissions
- Save thesql backup to afp network drive fails as cron job
- Cron and “command not found”
Best Answer
Cron is not in any way deprecated, but LaunchD has more versatility and is used, and actively maintained/supported by Apple. Their best practices guide is here
LaunchDaemon jobs(versus LaunchAgents, which take on a users id) run in root's context without needing to enable the root user. All that is required is a properly formed XML file(you can just copy-paste pre-existing ones in /Library/LaunchDaemons, substituting your script for the ProgramArguments with the applicable schedule keys) with the proper ownership/permissions. An older writeup can be found here: afp548's launchd-in-depth article