How Do I Check if Crontab is Working?

The crontab is an extremely useful job scheduler in Linux based systems that allow you to transform your daily routine tasks into crontab jobs that can 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 it is working or not.

At times, you assign the tasks of backing up your system or updating it to the crontab job scheduler. These tasks are considered very crucial for the normal working of any computer system. 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 about whether it is running or not and performing the jobs assigned to it or not.

That is why the goal of today’s article is to teach you the two methods of checking if crontab is working or not while using Linux Mint 20.

Methods of Checking if Crontab is Working or Not

The following two methods can be used to check if crontab is working or not. The first method is the simple one whereas the second method is a bit complex as it has more steps than the first one. Anyhow, we are going to state both the methods below:

Method # 1: By Checking the Status of Cron Service

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

Launch the terminal in whichever Linux distribution you 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 terminal of this Linux distribution in the image below:

Now for checking the status of the Cron service in Linux Mint 20, you need to type the following command:

$ systemctl status cron

Running the “systemctl” command along with the status flag will check the status of the Cron service as shown in the image below. If the status is “Active (Running)” then it will 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 “Active (Running)” status in the image shown above. However, if your crontab is not working, then you can always start this service by running the following command in your terminal:

$ sudo service cron start

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 before the above-mentioned command, then it will render an error message on the terminal. However, if you are logged in to the root user account, then you are good to go 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 are required to perform the following steps:

In this method, we are first going to create a bash script that we will run as a crontab job. If our crontab job will work i.e. if our bash script will execute as 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. After creating this file, we opened it and typed the script shown in the image below in our bash file. This script simply prints a dummy message on the terminal. Then we have saved our bash file and closed it.

The next step is to make our bash file executable so that our crontab job has the privileges to execute this bash file. For making our newly created bash script executable, we will run the following command in our terminal:

$ chmod +x Cron.sh

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

Now we will create a crontab job to run this bash file. For that, we need to open the crontab file on the terminal by running the command stated below in it:

$ crontab –e

When the crontab file will appear on your terminal, you need to type 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. We have pressed Ctrl+ X to save the crontab file and close it so that we can proceed further.

As soon as this file will close, the Cron daemon will install the new crontab as shown in the image below since we have just modified the crontab file.

If our crontab will be working fine, then our bash script will be executed every second. To verify this, we need to check out the/var/log/syslog file by running the following command in the terminal:

$ 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:

Conclusion

In today’s article, we taught you the two methods of verifying whether the crontab is working or not. The first method should be quite sufficient for you if you only intend to check the status of your crontab service, however, if you wish to see if any particular crontab job is executing successfully or not, then you will have to perform Method # 2. These methods were executed on Linux Mint 20, however, you can use any other preferred Linux distribution as well.



from Linux Hint https://ift.tt/38jXQWU

Post a Comment

0 Comments