Using cron on Mac OS X
This article is not about smart card. I wrote it to remember how to do and, possibly, help other Mac OS X users.
Mac OS X is a Unix system and so can be used as a Unix system with all the normal commands and services found on a Unix system.
I discovered recently that every thing was installed to use cron on my Mavericks (10.9) system.
cron - daemon to execute scheduled commands
The goal of cron is to run commands periodically. For example your can start a backup command or a Jenkins job on a regular schedule.Mail configuration
cron sends its output and error messages by email. So you need to have your email configured correctly.Sending an email
By default email is already working on Mac OS X for a local delivery. You can check it using:$ date | mail -s test rousseauOf course you use your own login name instead of rousseau.
This command will send the time and date in an email to rousseau with subject "test".
You should not have any output from the command unless you get an error.
If your mail server require an user authentication you may need to configure your Postfix local configuration.
Reading an email
The same commandmail
can be used to read emails stored locally.$ mail Mail version 8.1 6/6/93. Type ? for help. "/var/mail/rousseau": 1 message 1 new >N 1 rousseau@iMac-de-Lud Sun Feb 15 13:30 14/538 "test" ? press Enter Message 1: From rousseau@iMac-de-Ludovic.local Sun Feb 15 13:30:01 2015 X-Original-To: rousseau Delivered-To: rousseau@iMac-de-Ludovic.local To: rousseau@iMac-de-Ludovic.local Subject: test Date: Sun, 15 Feb 2015 13:30:01 +0100 (CET) From: rousseau@iMac-de-Ludovic.local (Ludovic Rousseau) Dim 15 fév 2015 13:30:01 CET ? d ? q $
Here we can read our test email. Then delete it using the "
d
" command and quit using the "q
" command.Redirecting your emails
Maybe you do not read your email using themail
command on your Mac, me neither. You can redirect your emails using the standard Unix way: a ~/.forward file. This file should contain the email you want to redirect your emails to:$ cat ~/.forward ludovic.rousseau@free.fr
Test again to send you an email and check the redirection is working.
Cron configuration
cron uses a crontab to store the user configuration. Each user has its own crontab configuration. Use the shell commandcrontab
to edit the configuration.vi
crontab uses the editor configured in the EDITOR environment variable or vi by default.The problem with vi (or vim) is that vi exits with a return value that is considered as an error by crontab:
$ crontab -e crontab: "vi" exited with status 1And all your edition is lost.
A solution found at "Error adding cronjobs in Mac OS X Lion" is to use nano instead of vi:
$ EDITOR=nano crontab -e
But I do not like nano and really prefer vim. So another way to solve that is to create a file (you may use the TextEdit graphical application to edit the file but take care to use the Text and not RTF format). I named my file crontab so that vim will do syntax colorization for me.
$ cat Documents/crontab # Every hour 0 * * * * echo "pouet"
Then install it using:
$ crontab Documents/crontab
Check your configuration has been accepted using:
$ crontab -l # Every hour 0 * * * * echo "pouet"
Read the crontab section 5 manual page (
man 5 crontab
) to know the format to use.Conclusion
No need to install a graphical tool to periodically run a command on Mac OS X.Mac OS X is and stays a Unix system under the graphical interface.