|
|
Fixing Mac OS X's "periodic" commandI've been discussing the key tasks that a Mac OS X user has to run to ensure that their system stays healthy and in tip-top shape, and one set that we've all agreed upon are the daily, weekly and monthly cron jobs. You can figure out when they're run with a simple grep command.
Here's the command:
$ grep -E '(daily|weekly|monthly)' crontab
These entries are read, left-to-right, as minute, hour, day, month, day-of-month, so you can see that my daily cron job is run at 3:15am every night, my weekly job at 4:30am on the 6th day of each week (Sunday, because Unix starts counting at day = 0), and the monthly job is run at 5:30am on the first day of each month...# Run daily/weekly/monthly jobs. 15 3 * * * root periodic daily 30 4 * * 6 root periodic weekly 30 5 1 * * root periodic monthly If you don't have your computer running at 3:15am every night - and you probably don't because either you shut it off or you have it "sleeping" - then an easy way to run the daily job is to duplicate the command: $ periodic daily
But that's not a good solution! Why? Because the periodic script needs to run as the root user, and it's not smart enough to check and ensure that's the case. Instead, you get lots of weird errors about permissions being incorrect. To fix it, you need to actually run the command:
$ sudo periodic daily
But why not instead fix periodic to ensure that you're running as root? Well, one reason is that periodic can ostensibly be run as other users, but if you check out the man page, you'll see it's described: "The periodic program is intended to be called by cron(8) to execute shell
scripts located in the specified directory."
How do you fix a system script to run the way you want? There are two approaches. One is that you can actually edit the periodic script itself, which involves you finding out where it lives:
$ which periodic
You can see it's a shell script (as I suspected) and that it lives in the system directory /usr/sbin (I'm on Panther, btw, for all of this). The first few lines of the script is where we could add a simple test to ensure that it's being run as root, as the highlighted lines below show: /usr/sbin/periodic $ ls -l /usr/sbin/periodic -r-xr-xr-x 1 root wheel 2936 12 Sep 20:18 /usr/sbin/periodic $ file /usr/sbin/periodic /usr/sbin/periodic: a /bin/sh - script text executable
$ head -24 /usr/sbin/periodic
If you're not comfortable hacking into system shell scripts - which I can totally understand - then an alternative is to create a short wrapper script that you ensure is earlier in your path. It might look like this:#!/bin/sh - # # $FreeBSD: src/usr.sbin/periodic/periodic.sh,v 1.9.2.7 2000/11/26 06:06:18 kris Exp $ # # Run nightly periodic scripts # # usage: periodic { daily | weekly | monthly } - run standard periodic scripts # periodic /absolute/path/to/directory - run periodic scripts in dir # usage () { echo "usage: $0 echo "or $0 { daily | weekly | monthly }" 1>&2 exit 1 }
if [ $(id -u) -ne 0 ] ; then
echo "You need to be root to run this command: use sudo first" 1>&2 exit 1 fi if [ $# -lt 1 ] ; then usage fi
#!/bin/sh
Short and sweet. One more step needed: check your path to ensure that it's in a directory that shows up prior to /usr/sbin or, even better, create an alias that ensures it's the one executed when you type in periodic at the command line: # wrapper script for 'periodic' that ensures it's run as root if [ $(id -u) -ne 0 ] ; then echo "You need to be root to run this command: use sudo first" 1>&2 exit 1 fi exec /usr/sbin/periodic "$@"
alias periodic="$HOME/bin/periodic"
Drop this into your .bashrc or .cshrc (depending on your login shell), log out, log in again, and next time you're on the ball and remember to run your periodic commands, but forget to use sudo, you'll get the friendly error message: $ periodic weekly
There's a lot more we can do with this idea, too, some of which is discussed - at length, with plenty of fun examples - in my new book Wicked Cool Shell Scripts.
You need to be root to run this command: use sudo first I hope this article was helpful reading!
Related Shell Script Programming articles:
✔ Secretly capture screenshots on my Mac?
When I used to work on a Linux system, there was a utility we had that would let me take screen captures every...
✔ Parsing "id" strings in a Shell Script?Hello Dave. I need a Bash shell script that creates a directories with the group names automatically when user logs in to the...
✔ Copy and Paste from the Mac OS X Command Line?I am constantly running commands in Terminal.app on my MacBook and then copying and pasting the results into email messages or documents. Yes,...
✔ Script to test line lengths for Twitter compatibility?I've been tasked with writing a series of tweets for a Black Friday marketing campaign and am finding it a bit tricky because...
✔ Shell script to convert lowercase to title case?As part of a project I'm working on, I find myself deep in a Linux shell script, needing to have a subroutine that...
Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help
right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
Categorized:
Shell Script Programming
(Article 3679,
Written by Dave Taylor)
Tagged: cron, crontab, mac os x, periodic, system administration Previous: How do I circumvent content filters? Next: Hacking Mac WiFi? Reader Comments To Date: 5ramana said, on February 21, 2007 7:50 AM:
hi this is ramana, i want hellp, script Gee Deezy said, on December 16, 2009 7:26 AM:
This was awesome, thank you! I am now wondering if apple support HOURLY as a periodic command. Also, is it true that crontab has been basically replaced with launchtl? Dave Taylor said, on December 16, 2009 7:39 AM:
"hourly" isn't supported in the periodic command, but it's quite easy to add an hourly task to crontab: just use "* * * * *" as the time specifier. And I have no idea what "launchtl" is, and don't envision crontab going away any time soon! GuruBob's Blog said, on January 21, 2010 5:22 PM:
Are you sure you don't mean launchctl. Here is the man page for launchctl from the Mac OSX reference library: http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man1/launchctl.1.html
I do have a comment, now that you mention it!Check This Out Too... |
Recent Entries
Look for Answers
Recommended
All Our Categories
Apple iPad Help
Articles and Reviews Auctions and Online Shopping Blogs and Blogging Building Web Site Traffic Business and Management Computer and Internet Basics d) None of the Above Facebook Help Google Gmail Help Google Plus Help HTML, JavaScript and Web Site Programming Industry News and Trade Shows iPhone and Cell Phone Help iPod, Sony PSP and MP3 Player Help Kindle Fire Help Mac OS X Help Pay Per Click (PPC) Advertising Pinterest Help Search Engine Optimization (SEO) Shell Script Programming Tech Support Video Help The Writing Business Twitter, LinkedIn and Social Network Help Unix and Linux Help Video Game Tips and Help Windows PC Help Find Me on Google+ ADT on G+ |
I appreciate your thoughtful tip. Another very useful piece of information would be a date for the article's authoring. Apple changes it's OS quite frequently and following tips for an out-of-date system can wreak havoc.
In my particular case, I'm trying to ensure that my monthly periodic script runs on the 2nd of each month but my users' crontabs (including roots) do not have periodic included, my guess being that launchtl now has taken over such functions.
msq