Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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.

share|improve this question
add comment

3 Answers

up vote 4 down vote accepted

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

share|improve this answer
    
Cron is actually deprecated. See Daemons and Services Programming Guide: Scheduling Timed Jobs. –  Lauri Ranta Oct 27 '12 at 10:41
add comment

It's a security risk, but you can enable the root user and run the command in root's cron. Alternatively, have you tried using sudo -s to get a "root shell" and making root's cron that way. Depending on your OS level, you might not need to have root enabled as a user for it's cron to run.

If you want to do it without enabling the root user (which is not really necessary in this case), you may (as any administrative user) enter sudo crontab -u root -e to edit root's crontab directly.

share|improve this answer
    
It works! Thank you so much! –  mcb Oct 10 '11 at 18:11
    
This should be two answers =1 for enable root, but +1 for the edit crontab as another user –  Mark Oct 27 '12 at 13:08
add comment

Comment "Defaults requiretty" from /etc/sudoers using visudo.

share|improve this answer
    
And where does the password come from in this case? –  patrix Oct 27 '12 at 15:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.