11 Cron Scheduling Task Examples in Linux

In this article we are going to review and see how we can schedule and run tasks in the background automatically at regular intervals using Crontab command. Dealing a frequent job manually is a daunting task for system administrator. Such process can be schedule and run automatically in the background without human intervene using cron daemon in Linux or Unix-like operating system.

Read Also: How to Create and Manage Cron Jobs on Linux

For instance, you can automate process like backup, schedule updates and synchronization of files and many more. Cron is a daemon to run schedule tasks. Cron wakes up every minute and checks schedule tasks in crontable. Crontab (CRON TABle) is a table where we can schedule such kind of repeated tasks.

Tips: Each user can have their own crontab to create, modify and delete tasks. By default cron is enable to users, however we can restrict adding entry in /etc/cron.deny file.

Linux Cron Examples

11 Cron Command Examples in Linux


Crontab file consists of command per line and have six fields actually and separated either of space or tab. The beginning five fields represent time to run tasks and last field is for command.

  1. Minute (hold values between 0-59)
  2. Hour (hold values between 0-23)
  3. Day of Month (hold values between 1-31)
  4. Month of the year (hold values between 1-12 or Jan-Dec, you can use first three letters of each month’s name i.e Jan or Jun.)
  5. Day of week (hold values between 0-6 or Sun-Sat, Here also you can use first three letters of each day’s name i.e Sun or Wed. )
  6. Command

1. List Crontab Entries

List or manage the task with crontab command with -l option for current user.

# crontab -l

00 10 * * * /bin/ls >/ls.txt

2. Edit Crontab Entries

To edit crontab entry, use -e option as shown below. In the below example will open schedule jobs in VI editor. Make a necessary changes and quit pressing :wq keys which saves the setting automatically.

# crontab -e

3. List Scheduled Cron Jobs

To list scheduled jobs of a particular user called tecmint using option as -u (User) and -l (List).

# crontab -u tecmint -l

no crontab for tecmint

Note: Only root user have complete privileges to see other users crontab entry. Normal user can’t view it others.

4. Remove Crontab Entry

Caution: Crontab with -r parameter will remove complete scheduled jobs without confirmation from crontab. Use -i option before deleting user’s crontab.

# crontab -r

5. Prompt Before Deleting Crontab

crontab with -i option will prompt you confirmation from user before deleting user’s crontab.

# crontab -i -r

crontab: really delete root's crontab?

6. Allowed special character (*, -, /, ?, #)

  1. Asterik(*) – Match all values in the field or any possible value.
  2. Hyphen(-) – To define range.
  3. Slash (/) – 1st field /10 meaning every ten minute or increment of range.
  4. Comma (,) – To separate items.

7. System Wide Cron Schedule

System administrator can use predefine cron directory as shown below.

  1. /etc/cron.d
  2. /etc/cron.daily
  3. /etc/cron.hourly
  4. /etc/cron.monthly
  5. /etc/cron.weekly

8. Schedule a Jobs for Specific Time

The below jobs delete empty files and directory from /tmp at 12:30 am daily. You need to mention user name to perform crontab command. In below example root user is performing cron job.

# crontab -e

30 0 * * *   root   find /tmp -type f -empty -delete

9. Special Strings for Common Schedule

Strings Meanings
@reboot Command will run when the system reboot.
@daily Once per day or may use @midnight.
@weekly Once per week.
@yearly Once per year. we can use @annually keyword also.

Need to replace five fields of cron command with keyword if you want to use the same.

10. Multiple Commands with Double amper-sand(&&)

In below example command1 and command2 run daily.

# crontab -e

@daily <command1> && <command2>

11. Disable Email Notification.

By default cron send mail to user account executing cronjob. If you want to disable it add your cron job similar to below example. Using >/dev/null 2>&1 option at the end of the file will redirect all the output of the cron results under /dev/null.

[[email protected] ~]# crontab -e
* * * * * >/dev/null 2>&1

conclusion: Automation of tasks may help us to perform our task better ways, error free and efficiently. You may refer manual page of crontab for more information typing ‘man crontab‘ command in your terminal.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Ravi Saive

I am Ravi Saive, creator of TecMint. A Computer Geek and Linux Guru who loves to share tricks and tips on Internet. Most Of My Servers runs on Open Source Platform called Linux. Follow Me: Twitter, Facebook and Google+

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

46 Responses

  1. Sudhir Kumar Ponnekanti says:

    I got one requirement i need your help.

    One unix command need to run 3 time
    with time intervel of 5 min.

    • Ravi Saive says:

      @Sudhir,

      With cron that would not possible, but you can use sleep command to achieve the same.

    • Ravi Saive says:

      @Sudhir,

      Create a following shell script and add the command that you want to execute every 5mins..

      #!/bin/bash
      command_here
      sleep 5m
      command_here
      sleep 5m
      command_here
      
  2. sarah badar says:

    I would like to schedule crontab job by shell script
    crontab -e
    */10 5 * * 2 /abc/test.sh
    Escape
    :wq!

    I am wondering where to put #!/bin/sh

  3. waqas says:

    I am facing problem in crontab .
    i create file on desktop in linux centos 6 .
    name of file is file .php
    in crontab -e : 1 * * * * root echo “hello waqas how r u ” >> /home/waqas/Desktop/file.php
    cronjob executed but nothing found in file and file is r-w-x please help me sir

  4. Pawan says:

    #!/bin/bash
    cd /var/www/html/taskScheduling/
    rails runner Task.SendData

    this is the script, that i ma trying to run using cron tab, but it is not running. directory is changing accordingly but the next one is not…plz reply how to run the command from cron tab ” rails runner Task.SendData”

    • Ravi Saive says:

      @Pawan,

      First create a file called “rails.sh” and add the content of script, now open the crontab file add the following line:

      30 23 * * * sh /path-to-file/rails.sh
      

      For example, the above script runs daily at 11:30pm.

      • waqas says:

        hello
        my cron job is executing but write noting in the file. I have created a file file.php on Linux centos 6 desktop and add a job in crontab -e.

        1  *  *  *  *  root  echo  "hello Waqas how r u  " >> /home/waqas/Desktop/file.php 
        

        job execute but no words shown in the file and file is r-w-x please help me

        • Ravi Saive says:

          @Waqas,

          you should add the binary location of php to execute PHP files properly via cron as shown:

          1  *  *  *  *  root  /usr/bin/php echo  "hello Waqas how r u  " >> /home/waqas/Desktop/file.php 
          
  5. Manju Thakur says:

    how to send mail alert to superuser if any user do changes in the scheduled job

    • Ravi Saive says:

      @Manju,

      Use this following script to track the changes to crontab files and if any changes would take place it will send a mail alert.

      CRONDIR=/var/spool/cron/crontabs
      CKFILE=/tmp/last.crontab.check
      [email protected]
      if [ -f $CKFILE ]
      then
          find $CRONDIR -type f -newer $CKFILE | while read tabfile
          do
             echo "Crontab file for user $(basename $tabfile) has changed" | mail -s "Crontab changed" $ALERT
          done
      fi
      touch $CKFILE
      
  6. Naitik says:

    thanks.
    but my cron job duplication records.
    how to avoid duplication
    my cron job running every minutes

  7. harinatha reddy says:

    Hi Ravi,

    I Want to run cronjob every Tuesday, as of my knowledge (* * * * 2 command), but here Im having some doubt every week tuesday means we can mention * in day of month field or not. can u help me out.

  8. Hi Ravi, @daily is mentioned twice (repeated) under point no. 9(Special Strings for Common Schedule) . Good tut.

  9. Chad says:

    I know this was covered once, but && and || don’t mean exactly what is written above. && and || act like If..then logic where && is “then” and || is “else.” So in the cases mentioned in comments and in the article, you won’t get what you expect…. cmd1 && cmd2, will execute cmd1 and if it exits correctly (no errors) then cmd2 will be executed. In the case of cmd1 || cmd2, it’s the opposite, if cmd1 exits with something OTHER than exit 0 (there WERE errors,) then it will execute cmd2. SO! You COULD do something like, cmd1 && || which will work exactly like an if..then…else logic block.

    If you DO want to run multiple commands, you can do it two ways. Way the first, you could write a script and then have cron execute that script. I find that to be the best way cause then I get to add all sorts of logic and error handling that can’t be done on a single line in cron. If you don’t need to be fancy, the other way to go about it is:

    @daily /path/to/cmd1 ; /path/to/cmd2 ; /path/to/cmd3; etc….

    Keep in mind that this is true on the command line itself as well. You can:

    $ cmd1 ; cmd2

    OR

    $ cmd1 && cmd2 || cmd3

  10. Sal says:

    Thanks for the article. a useful tool crontab syntax generator like http://www.crontab-generator.org/ can help you write crontab line correctly.

Got something to say? Join the discussion.

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.