How to schedule scripts or commands with cron on SuSE Linux

  • 3842311
  • 12-Sep-2006
  • 01-Apr-2014

Environment

Novell Open Enterprise Server (Linux based)
Novell SUSE Linux Enterprise Server
Novell SUSE Linux Enterprise Desktop
Novell SUSE Linux

Situation

How to schedule tasks, commands, or scripts to execute at specific times or intervals.

Resolution

crontab, cron.hourly, cron.daily,or cron.monthly allow scripts or commands to be run at specific times or intervals.

Additional Information

Cron jobs can be scheduled in many different methods with the most common being using /etc/crontab to schedule the script or command. This file is separated into seven columns with the format and description as follows:

minute hour day month dayofweek user command

  • Minute an integer from 0 to 59
  • Hour an integer from 0 to 23 (a 24 hour clock is used)
  • Day an integer from 1 to 31 (this must be a valid day for the given month. For exampleFeb 31will not work)
  • Month an integer from 1 to 12
  • Dayofweek integer from 0 to 7 where both 0 and 7 represent Sunday
  • User the user who cron will use to execute the command
  • Command the desired command to execute. This can be a script or a normal command like
    hwinfo >> hardware.txt
Be aware that more information about crontab exists, like the use of wildcards, and is available in the man pages or by consulting documentation at /usr/share/doc/packages/cron. In addition to /etc/crontab the /etc directory also contains the cron.daily, cron.hourly, and cron.monthly directories to enable easy scheduling of tasks. If a script needs to run every hour placing the script in the cron.hourly automatically schedules the task to run each hour. The cron.daily and cron.monthly follow the same logic and run those tasks daily or monthly respectively.  Also be aware that if the day of week and day of month are restricted (ie not *) then cron will perform an OR to determine if the command should be run.

*** Please note that when a task is scheduled using cron it is common for the job not to run the first time but will run the second time it is scheduled. This is by design and is mentioned here as an aid to the user. Commonly new scripts or commands are scheduled in crontab to run frequently while verifying the results before the script or command is copied to the cron.daily, cron.hourly, cron.monthly directory.


Troubleshooting cron and custom scripts can be a frustrating experience. A few, easy to use, lines of code can help troubleshoot possible problems.
  1. Use the date command in the beginning of the script as a check to verify if the script was called. A simple
    date >> /tmp/scripttest
    placed at the top of the script enables a quick check to see if the script was called and processed. The scripttest file will reveal when the script was called and when the date command was executed.
  2. If the date command reveals the script is running but the desired result of the script is not realized there is a problem with the script. With cron jobs, any output that would normally go to the screen is lost. Redirecting the output and errors to a file may help discover the problem, for example
    mycmd> /tmp/script.out 2>&1

Feedback service temporarily unavailable. For content questions or problems, please contact Support.