Ask Dave Taylor
  • Facebook
  • Instagram
  • Linkedin
  • Pinterest
  • Twitter
  • YouTube
  • Home
  • YouTube Videos
  • Top Categories
  • Subscribe via Email
  • Ask A Question
  • Meet Dave
  • Home
  • Linux Shell Script Programming
  • Fixing Mac OS X’s “periodic” command

Fixing Mac OS X’s “periodic” command

February 13, 2004 / Dave Taylor / Linux Shell Script Programming / 5 Comments

I’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
# Run daily/weekly/monthly jobs.
15 3 * * * root periodic daily
30 4 * * 6 root periodic weekly
30 5 1 * * root periodic monthly

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…

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
/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

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:

$ head -24 /usr/sbin/periodic
#!/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 ” 1>&2
  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

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
# 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 “$@”

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:

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
You need to be root to run this command: use sudo first

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.

I hope this article was helpful reading!

About the Author: Dave Taylor has been involved with the online world since the early days of the Internet. Author of over 20 technical books, he runs the popular AskDaveTaylor.com tech help site. You can also find his gadget reviews on YouTube and chat with him on Twitter as @DaveTaylor.

Let’s Stay In Touch!

Never miss a single article, review or tutorial here on AskDaveTaylor, sign up for my fun weekly newsletter!
Name: 
Your email address:*
Please enter all required fields
Correct invalid entries
No spam, ever. Promise. Powered by FeedBlitz
Please choose a color:
Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you, Dave, for all your helpful information by buying you a cup of coffee!

5 comments on “Fixing Mac OS X’s “periodic” command”

  1. GuruBob's Blog says:
    January 21, 2010 at 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

    Reply
  2. Dave Taylor says:
    December 16, 2009 at 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!

    Reply
  3. Gee Deezy says:
    December 16, 2009 at 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?

    Reply
  4. ramana says:
    February 21, 2007 at 7:50 am

    hi this is ramana, i want hellp, script
    to download files from remote server to local unix sever using shell script. pls help me.
    thank u

    Reply
  5. mistersquid says:
    February 27, 2006 at 1:38 pm

    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

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Search

Recent Posts

  • How to Use Twitter Reply-to-DM Features and New Reaction Emoji
  • Find and Play Minesweeper, Solitaire and more in Win11
  • How Do I Copy Audiobooks from my Mac to my iPhone?
  • Create a Google Image Search Box for Photos of the Coronation?
  • Does the Android WiFi “Settings” Show Network Speed?

On Our YouTube Channel

BlendJet 2 Portable Blender & Orbiter Drinking Lid -- DEMO & REVIEW

EKSATelecom H16 Pro Wireless Bluetooth Headset -- DEMO & REVIEW

Categories

  • AdSense, AdWords, and PPC Help (106)
  • Amazon, eBay, and Online Shopping Help (166)
  • Android Help (234)
  • Apple iPad Help (150)
  • Apple Watch Help (53)
  • Articles, Tutorials, and Reviews (346)
  • Auto Tech Help (19)
  • Business Advice (200)
  • ChromeOS Help (37)
  • Computer & Internet Basics (788)
  • d) None of the Above (166)
  • Facebook Help (384)
  • Google, Chrome & Gmail Help (190)
  • HTML & Web Page Design (248)
  • Instagram Help (49)
  • iPhone & iOS Help (630)
  • iPod & MP3 Player Help (173)
  • Kindle & Nook Help (100)
  • LinkedIn Help (90)
  • Linux Help (178)
  • Linux Shell Script Programming (90)
  • Mac & MacOS Help (918)
  • Most Popular (16)
  • Outlook & Office 365 Help (35)
  • PayPal Help (68)
  • Pinterest Help (54)
  • Reddit Help (20)
  • SEO & Marketing (82)
  • Spam, Scams & Security (98)
  • Trade Show News & Updates (23)
  • Twitter Help (224)
  • Video Game Tips (66)
  • Web Site Traffic Tips (62)
  • Windows PC Help (962)
  • Wordpress Help (206)
  • Writing and Publishing (72)
  • YouTube Help (47)
  • YouTube Video Reviews (159)
  • Zoom, Skype & Video Chat Help (64)

Archives

Social Connections:

Ask Dave Taylor


Follow Me on Pinterest
Follow me on Twitter
Follow me on LinkedIn
Follow me on Instagram


AskDaveTaylor on Facebook



microsoft insider mvp


This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this site or on any linked site. Further, please note that by submitting a question or comment you're agreeing to our terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site. Our lawyer says "Thanks for your cooperation."
© 2023 by Dave Taylor. "Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.
Privacy Policy - Terms and Conditions - Accessibility Policy