11 Cron Scheduling Task Examples in Linux

If you have any questions or problems regarding this article and want help within 24 Hours? Ask Now

Support TecMint: Did you find this tutorial helpful?. Please help to keep it alive by donating. Every cent counts! - Donate Now

Ravi Saive

Simple Word 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.

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...

30 Responses

  1. 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

  2. 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

  3. 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 
          
  4. 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
      
  5. Naitik says:

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

  6. 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.

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

  8. 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

  9. 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.

Leave a Reply

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

Join Over 95000+ Linux Users
  1. 66,682
  2. 3,836
  3. 31,647

Enter your email to get latest Linux Howto's