11 Cron Scheduling Task Examples in Linux

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

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Use our Linode referral link if you plan to buy VPS (it starts at only $10/month).
  4. Support us via PayPal donate - Make a Donation
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux course in 5 Days

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.

Receive Your Free Complimentary eBook NOW! -

Download Free Linux eBooks

Advanced Bash-Scripting Guide
Linux Bible
A Newbie's Getting Started Guide to Linux
Introduction to Linux - A Hands on Guide

You may also like...

34 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
      ALERT=someuser@your.alerthost
      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 *

Join Over 150K+ Linux Users
  1. 100,756
  2. 5,113
  3. 36,418

Enter your email to get latest Linux Howto's