If you have worked with Linux, you have probably encountered some process or job that you would like to schedule to be run at a certain time. In this post I will discuss a method of scheduling using crontab.

About Crontab

Crontab is found in Unix-like operating systems that is used to schedule commands to be executed periodically. Each line of the Crontab is a separate job and contains information on when the job is to be executed.

Crontab Restrictions

You can execute crontab if your name appears in the file /usr/lib/cron/cron.allow. If that file does not exist, you can use crontab if your name does not appear in the file /usr/lib/cron/cron.deny.

If only cron.deny exists and is empty, all users can use crontab. If neither file exists, only the root user can use crontab. The allow/deny files consist of one user name per line.

Crontab Commands

file Load the crontab data from the specified file. If file is a dash (““), the crontab data is read from standard input
-u user Specifies the user whose crontab is to be viewed or modified. If this option is not given, crontab opens the crontab of the user who ran crontab. Note: using su to switch users can confuse crontab, so if you are running it inside of su, always use the -u option to avoid ambiguity.
-l Display the current crontab.
-r Remove the current crontab.
-e Edit the current crontab, using the editor specified in the environment variable VISUALor EDITOR.

Creating a Crontab File

To view the list of cronjobs for a user, open a terminal and run:

$ crontab -l

To edit the list of cronjobs run:

$ crontab -e

This command will open a default editor to write or edit the crontab. Cronjobs are written in the following format:

* * * * * /bin/run/yourscript.sh

Making Sense of Scheduling

How to use Crontab for scheduling commands in Linux

Run every minute

* * * * * /bin/run/yourscript.sh

  • every minute
  • of every hour
  • of every day of the month
  • of every month
  • and every day in the week

Run every day at 2AM

0 2 * * * /bin/run/yourscript.sh

  • minute: 0
  • of hour: 2
  • of day of month: * (every day of month)
  • of month: * (every month)
  • and weekday: * (every day)

Run every 20 minutes between the hours of 8AM -11AM every day:

*/20 8-11 * * * /bin/run/yourscript.sh

  • minute: */20 (every 20 minutes)
  • of hour: 8-11 (between the hours of 8am -11am
  • of day of month: * (every day of month)
  • of month: * (every month)
  • and weekday: * (every day)

These examples should give you a start on how to use Crontab for scheduling commands in Linux.

Free eBook Download

In the eBook “Having a Conversation with Data”, learn what the current BI infrastructure has been and associated challenges with the traditional approach. How important the user experience is in order to best maximize data’s value (think visualizations!!) to your organization and how to gain a competitive advantage with modern analytics platforms.