Javascript required
Skip to content Skip to sidebar Skip to footer

Cron Scripte to Check to See if Its Already Running Before Running It Again

The crontab is an extremely useful job scheduler in Linux based systems that permit yous to transform your daily routine tasks into crontab jobs that tin run automatically at the specified schedule. However, since this job scheduler works silently in the background, that is why most of the users wonder whether information technology is working or not.

At times, you assign the tasks of bankroll upwards your system or updating it to the crontab job scheduler. These tasks are considered very crucial for the normal working of whatsoever estimator organisation. That is why, if they get missed out for any reason, your system will stop working consequently. So, it is considered extremely important to keep a check on your crontab job scheduler as well to stay informed well-nigh whether it is running or non and performing the jobs assigned to it or non.

That is why the goal of today's commodity is to teach you lot the two methods of checking if crontab is working or not while using Linux Mint twenty.

Methods of Checking if Crontab is Working or Not

The following two methods tin can be used to check if crontab is working or not. The get-go method is the unproblematic one whereas the second method is a bit complex equally it has more steps than the first one. Anyhow, we are going to state both the methods beneath:

Method # 1: By Checking the Status of Cron Service

For checking if crontab is working or not by looking at the condition of Cron service, you are required to perform the following steps:

Launch the terminal in whichever Linux distribution you lot are working with. As already stated above, we have used Linux Mint 20 to demonstrate both methods of checking if crontab is working or not. Therefore, we have also shown the last of this Linux distribution in the image below:

Now for checking the condition of the Cron service in Linux Mint 20, yous need to blazon the following command:

Running the "systemctl" control along with the status flag will check the condition of the Cron service as shown in the image below. If the condition is "Active (Running)" so information technology volition be confirmed that crontab is working perfectly well, otherwise not.

In our case, the crontab was working fine, that is why you could witness the "Agile (Running)" status in the image shown higher up. All the same, if your crontab is not working, and so you can e'er start this service past running the following command in your concluding:

$ sudo service cron first

Starting a new service in Linux Mint 20 always requires root user privileges. That is why if you will forget to mention the "sudo" keyword earlier the in a higher place-mentioned command, then information technology will render an error message on the last. However, if yous are logged in to the root user business relationship, then y'all are expert to become even without the "sudo" keyword.

Method # 2: By Running a Crontab Job

For checking if crontab is working or not by running a crontab job, you lot are required to perform the following steps:

In this method, we are kickoff going to create a bash script that we will run as a crontab task. If our crontab job will piece of work i.e. if our bash script will execute every bit intended, then it will imply that crontab is working perfectly fine, otherwise not. So, we decided to create a bash file in our Home directory named Cron.sh. Later creating this file, we opened it and typed the script shown in the prototype below in our fustigate file. This script simply prints a dummy message on the final. Then we have saved our fustigate file and closed it.

The next step is to brand our bash file executable so that our crontab job has the privileges to execute this fustigate file. For making our newly created bash script executable, nosotros will run the post-obit command in our last:

Running this command will not brandish any output on the terminal rather the command volition be handed back to you lot which will bespeak that this control has been executed successfully.

Now we volition create a crontab job to run this fustigate file. For that, we need to open the crontab file on the final by running the control stated below in it:

When the crontab file will announced on your terminal, y'all need to blazon the line highlighted in the following image in your file. Typing this line will create a crontab job to execute our bash file every second. Nosotros have pressed Ctrl+ X to save the crontab file and close it so that we can proceed farther.

As before long as this file will close, the Cron daemon will install the new crontab as shown in the epitome below since nosotros have only modified the crontab file.

If our crontab volition be working fine, and so our bash script volition be executed every second. To verify this, we demand to check out the/var/log/syslog file by running the following command in the concluding:

$ sudo grep –a "Cron.sh" /var/log/syslog

This file will contain a log of all the times your bash file has been executed which will imply that your crontab is working as shown in the image below:

Decision

In today's article, we taught you the two methods of verifying whether the crontab is working or not. The kickoff method should exist quite sufficient for you if you only intend to check the status of your crontab service, withal, if you wish to run into if whatever particular crontab job is executing successfully or not, and so you volition have to perform Method # 2. These methods were executed on Linux Mint 20, however, you tin can use whatsoever other preferred Linux distribution as well.

About the author

I am a cocky-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.

beaumontwaregs.blogspot.com

Source: https://linuxhint.com/check_working_crontab/