How to Check If Crontab Is Working

Estimated reading: 9 minutes 888 views

Introduction

Scheduling jobs is a common thing when managing the systems. It could be that you are scheduling automatic backups or sending emails. To schedule the jobs, you use the crontab which stands for cron table. A scheduled job becomes a cron job. The crontab has to be running for you to schedule the jobs and for your scheduled job to get executed.

In this guide, we learn the different ways of verifying if your crontab is working and how to start it if it’s not running.

How to Verify If Crontab is Working

If your crontab is inactive, you can still schedule the jobs. But the problem is that the jobs won’t execute. To avoid such a scenario, we must verify if crontab is working before or after scheduling a job.

There are different ways of checking if crontab is working:

1.Check the Cron Status

The crontab is a cron service; if you’ve not started it, cron remains inactive. Use the following command to check its status:

sudo service cron status

If its status displays as inactive (dead), it means that crontab is not working. It could be that you stopped it or haven’t started it.

sudo service cron status
service cron start

2. Check the Running Processes

If crontab is working, the cron service should be one of the currently running processes on your system. Here, you can use the ps command to display all running processes. Then, combine it with the grep command to filter “cron” from the output of the running processes.

Here’s the command to use:

ps aux | grep cron

If crontab is working, the command returns the PID of the cron service under different users. For this case, it returns the cron process for the root and user named “kyle.” That confirms that the crontab is working.


Alternatively, you can verify if crontab is working by checking if it returns a PID using the pgrep command. The pgrep command looks through the list of process IDs and finds the target process.

pgrep cron

Note that the process ID that is returned in the following output matches the one for the root PID for the cron service that we found in the earlier example. That also confirms that your crontab is up and running.

Suppose we stopped the cron service and tried to get its PID using pgrep to verify that crontab is running. The command yields no output. In such a case, it confirms that crontab is not working and you must start it.


3. How to use crontab command for cron jobs

You need to use the crontab command to edit/create, install, deinstall or list the cron jobs in Vixie Cron. Each user can have their own crontab file, and though these are files in /var/spool/cron/crontabs, they are not intended to be edited directly. You need to use crontab command for editing or setting up your own cron jobs.

Types of cron configuration files

There are different types of configuration files:

  1. The UNIX / Linux system crontab : Usually, used by system services and critical jobs that requires root like privileges. The sixth field (see below for field description) is the name of a user for the command to run as. This gives the system crontab the ability to run commands as any user.
  2. The user crontabs: User can install their own cron jobs using the crontab command. The sixth field is the command to run, and all commands run as the user who created the crontab

How Do I install or create or edit my own cron jobs?

To edit or create your own crontab file, type the following command at the UNIX / Linux shell prompt:
crontab -e

Do I have to restart cron after changing the crontable file?

No. Cron will examine the modification time on all crontabs and reload those which have changed. Thus cron need not be restarted whenever a crontab file is modified.

Syntax of crontab (field description)

The syntax is:

1 2 3 4 5 /path/to/command arg1 arg2

OR

1 2 3 4 5 /root/script.sh

Where,

  • 1: Minute (0-59)
  • 2: Hours (0-23)
  • 3: Day (0-31)
  • 4: Month (0-12 [12 == December])
  • 5: Day of the week(0-7 [7 or 0 == sunday])
  • /path/to/command – Script or command name to schedule

Easy to remember format:

* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

Your cron job looks as follows for system jobs:

1 2 3 4 5 USERNAME /path/to/command arg1 arg2

OR

1 2 3 4 5 USERNAME /path/to/script.sh

How do I use operators?

An operator allows you to specifying multiple values in a field. There are three operators:

  1. The asterisk (*: This operator specifies all possible values for a field. For example, an asterisk in the hour time field would be equivalent to every hour or an asterisk in the month field would be equivalent to every month.
  2. The comma (,) : This operator specifies a list of values, for example: “1,5,10,15,20, 25”.
  3. The dash (-) : This operator specifies a range of values, for example: “5-15” days , which is equivalent to typing “5,6,7,8,9,….,13,14,15” using the comma operator.
  4. The separator (/) : This operator specifies a step value, for example: “0-23/” can be used in the hours field to specify command execution every other hour. Steps are also permitted after an asterisk, so if you want to say every two hours, just use */2.

How do I disable email output?

By default the output of a command or a script (if any produced), will be email to your local email account. To stop receiving email output from crontab you need to append >/dev/null 2>&1. For example:

0 3 * * * /root/backup.sh >/dev/null 2>&1

To mail output to particular email account let us say [email protected] you need to define MAILTO variable as follows:

MAILTO="[email protected]"
0 3 * * * /root/backup.sh >/dev/null 2>&1

Task: List all your cron jobs

Type the following command:
crontab -l
crontab -u username -l

To remove or erase all crontab jobs use the following command:
# Delete the current cron jobs #
crontab -r
## Delete job for specific user. Must be run as root user ##
crontab -r -u username

Use special string to save time

Instead of the first five fields, you can use any one of eight special strings. It will not just save your time but it will improve readability.

Special string Meaning
@reboot Run once, at startup.
@yearly Run once a year, “0 0 1 1 *”.
@annually (same as @yearly)
@monthly Run once a month, “0 0 1 * *”.
@weekly Run once a week, “0 0 * * 0”.
@daily Run once a day, “0 0 * * *”.
@midnight (same as @daily)
@hourly Run once an hour, “0 * * * *”.

More about /etc/crontab file and /etc/cron.d/* directories

/etc/crontab is system crontabs file. Usually only used by root user or daemons to configure system wide jobs. All individual user must must use crontab command to install and edit their jobs as described above. /var/spool/cron/ or /var/cron/tabs/ is directory for personal user crontab files. It must be backup with users home directory.

Understanding Default /etc/crontab

Typical /etc/crontab file entries:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly

First, the environment must be defined. When you omit the SHELL line, /bin/sh is the default shell. Similarly, if PATH is not set, no default will be used. In other words, file locations need to be absolute. For example, instead of saying rsync, you need to use the full path: /usr/bin/local/rsync. Finally, when HOME is not set, cron will use the invoking users home directory. Therefore, I suggest you set up, SHELL, PATH, MAILTO, and HOME variable as per your needs.

Additionally, cron reads the files in /etc/cron.d/ directory. Usually system daemon such as sa-update or sysstat places their cronjob here. As a root user or superuser you can use following directories to configure cron jobs. You can directly drop your scripts here. The run-parts command run scripts or programs in a directory via /etc/crontab file:
Directory Description
/etc/cron.d/ Put all scripts here and call them from /etc/crontab file.
/etc/cron.daily/ Run all scripts once a day
/etc/cron.hourly/ Run all scripts once an hour
/etc/cron.monthly/ Run all scripts once a month
/etc/cron.weekly/ Run all scripts once a week

How do I backup installed cron jobs entries?

Simply type the following command to backup your cronjobs to a nas server mounted at /nas01/backup/cron/users.root.bakup directory:
crontab -l > /nas01/backup/cron/users.root.bakup
crontab -u userName -l > /nas01/backup/cron/users.userName.bakup

crond and cron jobs log file

You can use the cat command/grep command/tail command to view crond log file. For example, on a CentOS 7 Linux on can use the following commands:
cat /var/log/cron
tail -f /var/log/cron
grep "my-script.sh"
tail -f /var/log/cron

On modern Linux distro one can use the systemctl command or journalctl command:

sudo systemctl status cron
less /var/log/syslog | grep "cron"
sudo journalctl -u cron
sudo journalctl -u cron | grep backup-script.sh

Examples of Crontab

Compress the file when creating it
Create a script to do this and run every minute every day.
Create Folder1 and Folder2 with the command: mkdir

Create a script named testscript.sh with the following content:

 #!/bin/bash
NOW=$(date_+%T)
zip -r Folder1-$NOW.zip Folder1
zip -r Folder2-$NOW.zip Folder2

Save and close the file.Then type the command

crontab -e

Add a line with content. Run a script every 1 minute

*/1 * * * * /bin/testscripts.sh

Set the permissions using the chmod command:
chmod +x /bin/testscripts.sh

OR

bash /bin/testscripts.sh


Conclusion

Thus, I have completed the article summarizing the basic knowledge about using crontab in Linux. Now that you know what crontab is, how crontab works, as well as how to use crontab…
I hope this article is useful to you, especially server administrators, and website admins.
Continue to support me, please share the article if you like it

Support 24/24h with coupon code: 1DOLLAROFF 10% discount. Click now! 👆

Leave a Comment

Share this Doc

How to Check If Crontab Is Working

Or copy link

CONTENTS