How to Setup a Cron Job

There are a couple ways to set up a cron job.  If you have a script that you want to run ever so often, you could drop it in one of the cron directories, like cron.daily, or cron.weekly.  These are located in /etc.  Another way to do it is to use crontab.

To setup a cron job in crontab type

crontab -e

and then,

*/30 * * * * /path/to/yourscript.sh

The “*/30”  makes the script run every 30 minutes.  Below gives you a better idea of how the line works.  The asterisk (*) specifies all possible values for a field, but can/should be replaced with the time you want the script to execute.

* * * * * command to be executed

*(min) *(hr) *(DoM) *(month) *(week) /path/to/command_or_script.sh
  • min= Minute 0 – 50
  • hr = Hour 0-59
  • DoM = Day of month 0-31
  • month = Month 1-12
  • week = Day of Week 0-7, Sunday is 0 or 7

Below are a couple of examples of how to run a script or command.
Every minute.

* * * * * /path/to/script.sh

Every day 10 minutes after 1 in the morning,

10 1 * * * /path/to/command

Also, if you don’t want to be emailed the output of the command, you can simple append “>/dev/null 2>&1” to the end of the crontab line.
Example

0 * 1 * * /path/to/command/ >/dev/null 2>&1

Leave a Reply

Your email address will not be published. Required fields are marked *