How to Schedule Tasks with Cron and CornTab [Linux]

If you are familiar with Linux, you will know that Cron is the application used to schedule and automate tasks in a Linux environment. CronTab is the command that is used to access the Cron file where the user can set and schedule tasks. No, I didn’t misspell CronTab in the title, CornTab is a web-based GUI that allows you to set your schedule and commands, and then simply copy and paste it to your cron file.

There are other GUI apps built to easily edit cron, such as Gnome-Schedule. However, if you don’t want to install any new apps Corntab is a great web based solution, especially if you are setting up a cron job on a computer that doesn’t have Gnome-Schedule, or isn’t compatible with it.

The uses for cron are pretty obvious, anything from scheduling backups to running custom scripts at specific times, to opening programs on a schedule, cron is the way to get it done.

Let’s start with scheduling a task that simply creates a timestamp every minute to a log file, first using the shell/command line. Then we’ll run through the same steps using Corntab, so you can see how easy it is to use. Creating a timestamp or any output to a log file is a great way to test that your settings in cron are working, and of course to monitor your schedule going forward.

Open a terminal. Before we start, it is best to know where to go for information. The manual for crontab can be found by typing:

man crontab

Sure, you can Google and find other tutorials and probably the manual pages online, but it’s always good to know your way around the command line!

The syntax for cron is as follow:

crontab [ -u user ] file
crontab [ -u user ] [ -i ] { -e | -l | -r } [-s]
  • -l option causes the current crontab to be displayed on standard output.
  • -r option causes the current crontab to be removed.
  • -e option is used to edit the current crontab using an editor specified by the VISUAL or EDITOR environment variables.
  • -u option specifies the name of the user whose crontab is to be tweaked. If this option is not given, crontab will use the current user who is executing the command.
  • -s option appends the current SELinux security context string as an MLS_LEVEL setting to the crontab file before editing / replacement occurs (see the documentation of MLS_LEVEL in crontab. This is not used in all variations of Linux/cron).

Note that some programs need to be executed with root privileges.  When this is the case, you will need to change to the root user with the su command, or the sudo command which should work in most Linux variations (Debian, Ubuntu, Fedora).

To edit your crontab type:

crontab -e

or

sudo crontab -e
  • A field may be an asterisk (*), which always stands for “first-last”.
  • m -minute (0-59)
  • h -hour (0-23)
  • dom -day of the month (1-31)
  • mon -month of the year (1-12)
  • dow -day of the week (0-6 with 0=Sunday)
  • command -application, script, etc

string         meaning
——         ——-
@reboot        Run once, at startup.
@yearly        Run once a year, “0 0 1 1 *”.
@annually      (same as @yearly)
@monthly       Run once a month, “0 0 1 * *”.
@weekly        Run once a week, “0 0 * * 0″.
@daily         Run once a day, “0 0 * * *”.
@midnight      (same as @daily)
@hourly        Run once an hour, “0 * * * *”

Comma-separated values can be used to run more than one instance of a particular command within a time period.

Dash-separated values can be used to run a command continuously.

If you are first entering crontab you will probably be given a choice of editor to use. Feel free to choose any, but I normally stick with nano since it is simple, clean, and gets the job done.

cron in nano editor

To accomplish our original task, simply type:

* * * * * echo "The current minute is: $(date)" >> /tmp/minute.log

“Control-x” will prompt to save, then enter will confirm. You should see that cron was successfully updated. The job will print the current date/time every minute to a file called minute.log in the /tmp directory.

Here’s the automatic output for 6 minutes:

cron

Let’s get fancy, how about printing the date every 2 minutes…

*/2 * * * * echo “The current minute is: $(date)>> /tmp/minute.log

Using */n where n is a positive integer will say every n minute, or every n hours.

How about printing the date at 5:30am every Monday and Friday:

30 5 * * 1,5 echo "The current minute is: $(date)" >> /tmp/minute.log

Feel free to play around with scheduling simple “echo” to logfiles. Then move up to scripts and applications.

Now let’s try out CornTab, the web based cron editor that will take care of the syntax while we just click the options.

Here’s a step by step to create a crontab that will print the current date/time every two minutes, only in January, February, March, and April, on Thursday’s and Friday’s.

First, click “every n minutes” and move the slider to 2 minutes.

cron

Next, click to expand month, click to select “each selected month” and select Jan, Feb, Mar, Apr.

cron

Next, click to expand Day of Week, select “each selected day of the week” and select Thu and Fri.

cron

Finally, click to expand Command and type in the code/script. Then you can copy the full cron code from Corntab and paste it into your crontab by using the steps at the beginning of this article.

cron

cron

http://www.pantz.org/software/cron/croninfo.html
http://www.computerhope.com/unix/ucrontab.htm
https://help.ubuntu.com/community/CronHowto