Celebrate CPAN day on August 16th
Give back to the community on CPAN day
Cron is a job scheduling program available on UNIX-like platforms. Most system commands can be scheduled including the execution of Perl programs. Once a job is setup, cron will run it as scheduled even if the user is not logged in, which can be a great way to automate sysadmin tasks or repetitive jobs. This article describes how to run Perl scripts with cron.
When preparing a script to be run by cron, there a few things to keep in mind. You may want to add a shebang line to the of the script for the Perl binary you want to execute. For example:
If you plan to run the script on different platforms, you can omit the shebang line, as the location of the Perl binary may vary from platform to platform (more on this later).
Also ensure that any paths used in the script are absolute paths (such as for opening filehandles, database connection strings and external program paths).
To schedule a job with cron, the job needs to be added to the user's crontab. To do this open a terminal and type the following:
This will open the crontab in a text editor. To add a job, a line must be added in the following format:
* * * * * command to be executed | | | | | | | | | | | | | | +----- day of week (0 - 6, Sunday=0) | | | +------- month (1 - 12) | | +--------- day of month (1 - 31) | +----------- hour (0 - 23) +------------- min (0 - 59) An asterisk (*) means all.
Here are some example crontab entries:
# execute every minute * * * * * perl /path/to/Beacon.pl # execute every 5 minutes */5 * * * * perl /path/to/Beacon.pl # execute every hour at 0 minutes past the hour 0 * * * * perl /path/to/Beacon.pl # execute every 12 hours at half past the hour 30 */12 * * * perl /path/to/Beacon.pl
If your script does not contain a shebang line, provide the absolute path to the Perl binary in the crontab. For example:
30 */12 * * * /usr/local/bin/perl /path/to/Beacon.pl
Save the crontab and exit the text editor. To check the job has been scheduled, you can check your crontab with this command from the terminal:
This will print your current crontab to the terminal.
Working with cron is usually straightforward, but if you are having difficulty getting the Perl script to run, check the following: