useful cron commands in linux

10 Useful cron Commands in Linux With Examples

Spread the love

In this tutorial, we will explain ten useful cron commands in Linux with examples.

The cron command is used for scheduling tasks to be executed in a specific period, like a month, day, hour, minute, or even seconds. The cron uses a table of tabs called crontabs which have a list of commands that need to be executed with their times respectively. These cron commands are used by system administrators mostly, to make their work easier and be sure not to forget to execute something during a specific period of their work.

In this tutorial, we are going to use Ubuntu 20.04, but you can choose any Linux distro you want. Let’s get started!

Prerequisites

  • Fresh install of Ubuntu 20.04 OS
  • User privileges: root or non-root user with sudo privileges

Update the System

Before we start with the ten useful cron commands, we need to update the system packages to the latest versions available.

sudo apt update -y && sudo apt upgrade -y

Once the system is updated, we are ready to show you the basic cron commands in Linux.

1. Manage crond service

To check the status of the crond service, execute the following command:

systemctl status cron.service

You should receive the following output:

root@host:~# systemctl status cron.service
● cron.service - Regular background program processing daemon
     Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2022-08-08 23:57:40 CEST; 1 weeks 3 days ago
       Docs: man:cron(8)
   Main PID: 583 (cron)
      Tasks: 1 (limit: 4611)
     Memory: 11.7M
     CGroup: /system.slice/cron.service
             └─583 /usr/sbin/cron -f

For starting, stopping, and restarting the cron service, you can use the following commands respectively:

systemctl start cron.service

systemctl stop cron.service

systemctl restart cron.service

2. List the jobs

To list the jobs for the current user, execute the following command:

crontab -l

The output should look similar to this because we do not have cron jobs set yet:

root@host:/var/www/html# crontab -l
no crontab for root

3. Create your first cron job

To create your first cron job execute the following command:

crontab -e

After executing this command, the file will be opened, and you can paste the following lines of code:

1 0 * * *   root cd /var/www/html && /usr/bin/sh script.sh

Save the file and close it. If you execute the command crontab -l, you will get this:

root@host:/var/www/html# crontab -l
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
1 0 * * *   cd /var/www/html && /usr/bin/sh script.sh

4. Schedule a job every 10 minutes

To schedule a job to run every 10 minutes, add the following lines with the crontab -e command:

*/10 * * * * root cd /var/www/html && /usr/bin/sh script.sh

This cron will call the script every 10 minutes.

5. Schedule a job every hour

To schedule a cron job to run every hour, add the following lines with the crontab -e command:

0 * * * * root cd /var/www/html && /usr/bin/sh script.sh

6. Schedule a job every day at 10:00 AM

Now, we will show you a different way to schedule a cron job. Open the file /etc/crontab. You should see the following lines of code:

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#

Now to add the cron job to execute every day at 10:00 AM, just add the following line at the bottom of the file, save it and close it.

0 0 * * * root cd /var/www/html && /usr/bin/sh script.sh

7. Schedule a job At 12:00 AM, only on Friday

To schedule this job, open the /etc/crontab file and paste the following command:

0 0 * * FRI root cd /var/www/html && /usr/bin/sh script.sh

8. Schedule a job for a daily backup

To schedule a daily backup, for example, with some script that will execute commands to generate a daily backup, you do not have to specify intervals. You just need to place the script to the /etc/cron.daily directory on your server:

9. Understanding the cron directories

In the previous example, we explained how to enable daily backup by putting some script into it. What if we want our script to be executed hourly, weekly, or monthly? Do not worry about that. Just execute ls -al /etc/cron and hit tab in your terminal server:

root@host:/var/www/html# ll /etc/cron
cron.d/       cron.daily/   cron.hourly/  cron.monthly/ crontab       cron.weekly/

As you can see, there are directories for different periods where you can place your scripts, which will be executed automatically.

10. Man command for cron

If you want to learn more about the cron, you must execute man cron in your terminal. You should receive complete documentation about the cron command:

CRON(8)                                                               System Manager's Manual                                                               CRON(8)

NAME
       cron - daemon to execute scheduled commands (Vixie Cron)

SYNOPSIS
       cron [-f] [-l] [-L loglevel]

DESCRIPTION
       cron is started automatically from /etc/init.d on entering multi-user runlevels.

OPTIONS
       -f      Stay in foreground mode, don't daemonize.

       -l      Enable  LSB  compliant  names  for  /etc/cron.d  files.   This  setting,  however,  does  not  affect  the  parsing of files under /etc/cron.hourly,
               /etc/cron.daily, /etc/cron.weekly or /etc/cron.monthly.

       -n      Include the FQDN in the subject when sending mails.  By default, cron will abbreviate the hostname.

       -L loglevel
               Tell cron what to log about jobs (errors are logged regardless of this value) as the sum of the following values:

That’s it. You just learned how to set up cron jobs on your own in different ways. If you find this difficult and you want some specific cron at a specific time, just contact our technical support, and they will do the rest. We are available 24/7.

If you liked this post about ten useful cron commands in Linux with examples, please share it with your friends on social networks or simply leave a reply below.

Leave a Reply

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