|
Home > WestHost How-To > Run Cron Scripts How To Run Cron Scripts On Your WestHost VPSIntroduction to Cron on the WestHost VPSCron is a daemon that executes commands at scheduled times and dates. A cron daemon is part of the software on your VPS. The schedule of commands and when they are to be executed is stored in your VPS in file /var/spool/cron/example . The format of this file is documented on the Linux man page for crontab(5) . The first five fields on each line of the file describe one or more dates and times when the command is to be run. The rest of the line is the command to run. The WestHost VPS cron daemon has a few limitations not present in other releases of Linux. The most important limitation is that command output and error messages are not emailed to you, as they would be on most Linux installations. This makes it difficult to troubleshoot cron scripts, or indeed gather any information about how cron works on your VPS. Writing a Cron ScriptProbably the best place to start is to create a bash script for cron to run. In this script, you can use the mail command to send yourself email containing information about the script's operation. For example, let's create a file named /var/spool/cron/myscript and put into it the following:
#!/bin/bash The environment variables passed to the shell script are similar, but not identical, to the values that you see when you log in to the VPS; $HOSTNAME contains the name of your VPS host (example.com) and $LOGNAME contains the name of your VPS user (example) . In the script above, the first line specifies the program ( /bin/bash ) which is to interpret the rest of the script. The second line runs the printenv command, merges any error messages with normal output ( 2>&1 ), collects the combined output of that command ( ` ` ), and saves it in shell variable ENVAR . The third line runs the mail command to send an email to $LOGNAME with text taken from the here document in the following lines. The file containing the script must be made executable ( chmod a+x /var/spool/cron/myscript ). To test your script, execute it from the command line by typing /var/spool/cron/myscript . We assume that you have arranged to receive mail sent to example@example.com . Modifying Your Crontab FileNow we can configure cron to run myscript at 17 minutes after every hour by adding the line: 17 * * * * /var/spool/cron/myscript to the crontab file /var/spool/cron/example . While you are editing this file, be careful to avoid changing the lines that are there already, since they control various important functions of your VPS. We cannot simply copy the modified crontab file into the /var/spool/cron/ directory. Many Linux cron daemons would automatically notice that the crontab file had been modified, but it is another limitation of the WestHost VPS cron daemon that a change is not automatically recognized. Instead, we must edit the file by using the command line command crontab -e . This command starts an editor to edit your crontab file, then notifies the cron daemon of the change when you exit the editor. Note that this restriction affects only changes to the crontab file, not changes to the scripts that it runs. Last modified: Friday, July 13 2007 at 8:38:03 AM MDT |