11 Cron Command Examples in Linux [Schedule Cron Jobs]

In this article, we are going to review and see how we can schedule and run Linux tasks in the background automatically at regular intervals using the Crontab command.

Dealing with a frequent job manually is a daunting task for system administrators and such tasks can be scheduled and run automatically in the background without human intervention using cron daemon in Linux or Unix-like operating system.

For instance, you can automate Linux system backup, schedule updates, and synchronization of files, and many more using Cron daemon, which is used to run scheduled tasks from the command line or use online tools to generate cron jobs.

Cron wakes up every minute and checks scheduled tasks in countable – Crontab (CRON TABle) is a table where we can schedule such kinds of repeated tasks.

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

Crontab in Linux

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

  • Minute (hold values between 0-59)
  • Hour (hold values between 0-23)
  • Day of Month (hold values between 1-31)
  • The month of the year (hold values between 1-12 or Jan-Dec, you can use the first three letters of each month’s name i.e Jan or Jun.)
  • Day of week (hold values between 0-6 or Sun-Sat, Here also you can use the first three letters of each day’s name i.e Sun or Wed. )
  • Command – The /path/to/command or script you want to schedule.

1. List Crontab Entries

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

# crontab -l

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

2. Edit Crontab Entries

To edit the crontab entry, use -e the option shown below. The below example will open scheduled jobs in the VI editor. Make necessary changes and quit pressing :wq keys that save the setting automatically.

# crontab -e

3. List Scheduled Cron Jobs of User

To list scheduled jobs of a particular user called tecmint using the 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 entries. Normal users can’t view others.

4. Remove Crontab Entry

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

# crontab -r

5. Prompt Before Deleting Crontab

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

# crontab -i -r

crontab: really delete root's crontab?

6. Allowed Special Characters (*, -, /, ?, #)

  • Asterisk(*) – Match all values in the field or any possible value.
  • Hyphen(-) – To define a range.
  • Slash (/) – 1st field /10 meaning every ten minutes or increment of range.
  • The Comma (,) – To separate items.

7. System-Wide Cron Schedule

A system administrator can use the predefined cron directory as shown below.

  • /etc/cron.d
  • /etc/cron.daily
  • /etc/cron.hourly
  • /etc/cron.monthly
  • /etc/cron.weekly

8. Schedule a Job for a Specific Time

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

# crontab -e

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

9. Special Strings for Common Schedule

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

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

10. Multiple Commands with Double ampersand(&&)

In the below example, command1 and command2 run daily.

# crontab -e

@daily <command1> && <command2>

11. Disable Email Notifications.

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

[root@tecmint ~]# crontab -e
* * * * * >/dev/null 2>&1

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

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Ravi Saive
I am an experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

54 Comments

Leave a Reply
  1. It is easy to test that the five crontab timing parameters do what you expect. There is a sandbox at cronbuddy.com, enter your values and get back a schedule of run times.

    Reply
  2. Hi,

    I want to know about the backup process…

    How to take OS backup from Linux server and store in AWS storage using crontab cmd from Linux…

    Please help us…

    Reply
  3. Hey, Ravi,

    Can you provide some examples of the common things that Linux System Administrators automate via Crontab?

    I have two jobs set, one to output the first two lines of df -H, and to output the uptime to a log. Both for historical record-keeping, but I’m struggling to find things that *should* be automated.

    01 1 1 * * df -H | head -n 2 >> /var/log/df.log 2>&1
    01 3 1 * * uptime >> /var/log/uptime.log 2>&1

    I get that it will always be contextually related to the things that need to happen in your environment, and my environment is just that of a person learning Linux administration.

    I just learn by example and am drawing a blank.

    -Dalton

    Reply
  4. Hello users here is a very nice website for creating a crontab command within few second with your schedule,

    crontabgenerator.org

    Please review it and let me know if you have any query.

    thanks

    Reply
    • Hello users here is a very nice website for creating a crontab command within few second with your schedule,

      crontabgenerator.org

      Please review it and let me know if you have any query.

      thanks

      Reply
        • Daniel is right. to run at every 45 days, you need to enter 2 lines.

          1. to run of every 30th day for month#1,3,5,7,9,11
          2. to run of every 15th day for month#2,4,6,8,10,12
          Reply
  5. Thanks for this share, I need your help on an real urgent basis, I need my server to automatically update its dynamic ip with the DDNS provider every time my system reboots or boots, kindly help me with the command, i am using ddclient for dynamic ip updating to my DDNS provider with the following command:

    "/usr/sbin/ddclient -daemon 300 -syslog -verbose -noquiet -debug"
    

    kindly tell me the exact command for running the above command as root as soon as my system boots up.

    Secondly i want my server to execute this command on every reboot even if no one has logged in yet, is it possible to run the command using crontab before even anyone logs in the server?

    Really Appreciate any help from any of you who knows how to do it..

    Regards

    Reply
    • @Junaid,

      Which Linux distribution you’re using? if it’s CentOS, you can add the below command to /etc/rc.local file, this file executes commands when system boots up.

      /usr/sbin/ddclient -daemon 300 -syslog -verbose -noquiet -debug
      
      Reply
  6. I got one requirement i need your help.

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

    Reply
    • @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
      
      Reply
  7. 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

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

    Reply
  9. #!/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”

    Reply
    • @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.

      Reply
      • 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

        Reply
        • @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 
          
          Reply
    • @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
      
      Reply
  10. 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.

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

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

    Reply
  13. Hi Ravi,

    Thnx for the tutorial, however I am trying to delete the contents of a temp folder every second. I am using the following command:
    As I can only delete files in this folder als root, I do the following.
    $ sudo crontab -e
    */1 * * * * find /tmp -type f -empty -delete >/dev/null 2>&1

    When I check with:
    ps -ef | grep cron
    It shows only for the current user that cron has run, but not for root.
    Could you tell me what I am overlooking?

    Cheers!

    Reply
  14. One thing worth mentioning is that since cron runs in the background, its important to make sure you have some sort of logging or monitoring in place to make sure things are working as intended. Its all too easy to take things like this for granted, only to realize too late that something has been failing or perhaps not even running at all. https://www.cronalarm.com is a great service to help you keep tabs on all your cron jobs and scheduled tasks.

    Reply
  15. Cron jobs meant for particular user fails to start automatically whenever the server undergoes reboot. what is the solution, to enable the cronjobs of that user to start automatically after the reboot?? help me…..
    Thanks in advance… :)

    Reply
  16. cant get this to run using command sudo crontab -e

    and i added this

    */5 * * * * php -f /var/www/html/cj/email1.php >/dev/null 2>&1

    but it only works when i goto page manually any ideas please anyone?

    Reply
  17. hello and good day
    I write script file that include tar command. when I write ./name file, then it works. but when write below command in crontab, it doesn’t work
    1 * * * * /home/dira/filename
    do you know why does it work?
    best regards

    Reply
  18. Akee, thats one way. Another way to have both commands run no matter what is to use “||” instead of “&&”

    @daily ( command1 || command2 )

    hth

    Reply
  19. This line : “@daily && ” does not mean that command1 AND command2 will be run on a daily basis. It means that command1 will be run on a daily bases, and IF the return code of this command is 0 e.g a success, THEN command2 will be run after.

    You need to have two lines if you want both command to be run in any case :
    @daily
    @daily

    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.