SUSE Support

Here When You Need Us

How to schedule scripts or commands with cron on SuSE Linux

This document (3842311) is provided subject to the disclaimer at the end of this document.

Environment

SUSE Linux Enterprise Server
SUSE Linux Enterprise Desktop
 

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

Disclaimer

This Support Knowledgebase provides a valuable tool for SUSE customers and parties interested in our products and solutions to acquire information, ideas and learn from one another. Materials are provided for informational, personal or non-commercial use within your organization and are presented "AS IS" WITHOUT WARRANTY OF ANY KIND.

  • Document ID:3842311
  • Creation Date: 12-Sep-2006
  • Modified Date:15-Mar-2021
    • SUSE Linux Enterprise Desktop
    • SUSE Linux Enterprise Server

< Back to Support Search

For questions or concerns with the SUSE Knowledgebase please contact: tidfeedback[at]suse.com

tick icon

SUSE Support Forums

Get your questions answered by experienced Sys Ops or interact with other SUSE community experts.

tick icon

Support Resources

Learn how to get the most from the technical support you receive with your SUSE Subscription, Premium Support, Academic Program, or Partner Program.

tick icon

Open an Incident

Open an incident with SUSE Technical Support, manage your subscriptions, download patches, or manage user access.