How to test a weekly crontab job on Linux?

LinuxOperating SystemOpen Source

In order to test a crontab job we first need to explore and understand what a crontab job is.

A crontab job is nothing but a list of commands that we can run during a cron job. A cron job is a utility that schedules automatic execution of commands at specific times.

We can start a cron job with the help of bash script by following the commands shown below −

crontab -e

This will open a file which you can edit, insert the cron job shell script in the above file and then close that file.

Just insert the code shown below in the above file

* * * * *

The above command contains 5 *, where each * indicates the time and then follows the stars. We have the script which we want to run as a cron job. In the

we need to write the following command to make the environment variables available to it.

One way to test a weekly cron job is to make use of a lock file that when used will set the cron job to run every minute. This way you can keep editing the files and each minute these files will be tested and you will be aware if your cron job is working properly or not.

Just create a shell script for the same inside which you can place your cron job.

The shell script is shown below −

if [ -e /tmp/lock ]
   echo "cronjob locked"
   exit 1

touch /tmp/lock
< your regular cron here ...>
rm -f /tmp/lock

An alternate way is to make use of the /var/log directory and then run the following command shown below −

tailf /var/log/cron

The above command will allow you to check the cron log updates in real time.

Updated on 31-Jul-2021 11:57:46