Crontab in Ubuntu

“Cron is a system daemon used to execute desired tasks (in the background) at designated times. ” – the official CronHowTo

CRON SERVICE

Start/Stop/Restart/Status the Cron service

/etc/init.d/cron start/stop/restart/status

Or

service cron start/stop/restart/status

Verify that our Cron service is running
pgrep cron

Or

ps -ef | grep cron | grep -v grep

Using CRON


Show help for Crontab:

man crontab

We can start Crontab editor with the following command:

crontab -e

With the command above we run crontab like the current user. If we would like to run crontab like a different user:

crontab -u username -e

For a list of crontab jobs:

crontab -l

LOCATION

In the /etc we can find a few sub directories related with the Crontab

  • /etc/cron.d/
  • /etc/crond.daily/
  • /etc/cron.hourly/
  • /etc/cron.monthly/
  • /etc/crontab
  • /etc/cron.weekly/
The location for the personal crontab files

  • /var/spool/cron/

Syntax

Bellow you can find the Linux Crontab format:

MIN HOUR DOM MON DOW user cmd 

user_17980_59cdaeced595a.JPG

* We can also combine the parameters (*/10, 1-5, etc) in the fields
Examples
Some of the Crontab jobs examples:
* * * * * root /home/ondrej/check_free_disc_space.sh
Script will be running every minute, every hour, every day, etc..
15 23 * * 6 root /home/ondrej/backup.sh
15 - 15th minute
11 - 11pm
* - Every day
* - Every month
6 - Saturday
root - user
/home/ondrej/backup.sh - this script will be run
00 07-17 * * 1-5 root /home/ondrej/send_notification.sh
00 - 0th minute
07-17 - 7am, 8am, 9am, 10am 11am, 12pm, 1pm, 2pm, 3pm, 4pm, 5pm
* - Every day
* - Every month
1-5 - Mon, Tue, Wed, Thu and Fri (Every weekday)
root - user
*/10 * * * 1-5 root /home/ondrej/test.sh
*/10 - every 10minutes
* - Every hour
* - Every day
* - Every month
1-5 - Mon, Tue, Wed, Thu and Fri (Every weekday)
An other example of a Crontab job- this one will be running every 1hr 30min:
0 0,3,6,9,12,15,18,21 * * * /home/ondrej/send_notification.sh
30 1,4,7,10,13,16,19,22 * * * /home/ondrej/send_notification.sh

Crontab also allows the use of the following format:
user_17980_59cdaac544cd1.JPG

Run process after reboot can look like this:
@reboot /home/ondrej/run_process.sh

More about Crontab

An example of ENV variables inside of Crontab job

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

The output from cron can be forwarded into email:

MAILTO="username@domain.org"

Forwarding of STDOUT & STDERR

Options for forwarding STDOUT (Standard output) & STDERR (Standard error)

Redirect of the output to a log file (if the log file already exists, it will clear the current content with the new STDOUT):
@reboot /home/ondrej/run_process.sh > /var/log/run_process.log

Redirect of the output to a log file (if the log file already exists, it will add the STDOUT at the bottom of the log file):

@reboot /home/ondrej/run_process.sh >> /var/log/run_process.log

Redirect of the STDOUT & STDERR to a log file (if the log file already exists, it will add the STDOUT at the bottom of the log file):

@reboot /home/ondrej/run_process.sh > /var/log/run_process.log 2>&1

Supress the output (it also disables the the email forwarding):

@reboot /home/ondrej/run_process.sh > /dev/null 2>&1
Last Notes
  • The good practise is restart of Cron service after creating / editing / removing of Crontab job
  • The root user should create its crontab jobs in this location /etc/cron.d/
  • The standard permission of Crontab job file is 0644
  • If the machine / Cron service is not running, a Crontab job will not be execute later, it will be execute in the next cycle (look for some alternatives below which support the late start)
  • For debugging purposes we can forward our ENV Variables into the file
* * * * * env > /var/log/env.stdout
  • We want to have a newline (empty line/blank line) after the last line at our Crontab job file
* * * * * env > /var/log/env.stdout

  • The Crontab job files should not contain the dot ( . ) in their name, otherwise they will be skiped
  • The standard logfile for Crontab jobs at Ubuntu is in this location
/var/log/syslog
  • Use absolute path for the commands (you can find it with ” $ which “)
root@test://# which rm
/bin/rm
root@test://# which service
/usr/sbin/service
root@test://# which apt-get
/usr/bin/apt-get

External Sources

Online crontab generator – http://www.crontab-generator.org

The official CronHowTo – https://help.ubuntu.com/community/CronHowto

0 Comments

Leave a Reply

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