| by Arround The Web | No comments

How to Check If Crontab Is Working

Crontab stands for cron table, where cron is obtained from the Greek word Chronos which means time. Crontab is a scheduler that is responsible for managing the tasks that are scheduled by the user. The crontab is the set of commands that enables the system to perform the tasks automatically. It is the silent job handler. Due to which, it becomes necessary to check whether it is working properly or not. If we don’t check for the crontab whether it is working or not, it stops the system from working. For example, if we want to back up the database regularly for some reason, crontab stops working. If we don’t check for that until the crontab is started again by the user, the whole data will be lost. The sixth is a fixed number of fields. The first five of which are used to store the time and date on which the job is done and the sixth one is the command to be executed.

Crontab Uses:

The major uses of crontab are as follows:

  • It helps get a backup of the database or log files.
  • It is useful to delete the log files and databases.
  • It is used to schedule the emails like expiry emails or newsletters.
  • It can be used to clean up the cache without performing it manually by the user.
  • It is also useful in automating the Unix jobs.

Methods:

There are two ways to evaluate cron’s functioning.

Method 1: Checking the Cron Service

By looking at the cron service’s status, we will try to determine whether or not the crontab is functioning in this manner. To check the cron service, we must have access to the terminal and Linux should be working. In this demonstration, we use the Ubuntu 20.04.

Syntax:

The following is the syntax to check the status of the crontab where the systemctl command is used to manage the system and is also useful in managing the services. It also allows the admin to start and stop these services:

linux@linux-Virtualbox:~$ systemctl status cron

After getting the terminal ready, we run the provided command. After running the provided command, we get the following output in which the status of the cron is displayed. We can see in the following snippet that the active status is shown in the green-colored text. If the crontab is not working, it displays the inactive message with the red-colored text.

If the cron tab is not working, we can start it by just writing a simple command as follows:

linux@linux-Virtualbox:~$ sudo service cron start

In the previous command, Sudo stands for “super user do”. From this, we can get an idea of what purpose it is used for. Sudo allows the admin to run the commands to which he is allowed to perform.

With the use of the aforementioned command, we can successfully get our cron to function properly. When we run this command, it asks for the user password and then starts the crontab again. We can verify it’s working by running the status-checking command.

Method 2:  Running the Cronjob

In this method, we first have to create a Bash. Bash scripting is a convenient way to automate things on any Linux system. To create a Bash, we must know which Bash we are using right now. To check that, we simply write the following command:

linux@linux-Virtualbox:~$ which bash

After running this command, we get the output like in the following figure. In our case, it is “/usr/bin/bash”.

Once we have the location, we can just copy this and create a new bash file. For this, we simply write the command in our terminal as follows:

linux@linux-Virtualbox:~$ nano bash.sh

After running this command, we get the file like the one in the following. You can name the file with whatever you want but it does need to be named “sh”. In the bash.sh file, we write the link of the Bash file that we copied from the previous snippet which is “usr/bin/bash”. If we simply run the Bash file, it doesn’t display anything. So, to display the working of the Bash file, we display the “My cron job is working” message using the “echo” keyword which is used to display the content that is written inside its commas. After saving that, we close our file.

Now, we execute our Bash. But before that, it must be an executable file. To make it executable, we run the following command:

linux@linux-Virtualbox:~$ chmod +x bash.sh

After that, we create a cron job to execute this file. To do that, we first open the crontab file by running the given command as follows:

linux@linux-Virtualbox:~$ crontab –e

Upon running the previous command, we get the text opened in which we simply write the following command in our case as follows:

“*****/usr/bin/bash/bash.sh”

The five asterisks match the time of execution in which the first asterisk indicates the minutes, the second indicates hours, the third indicates the day, the fourth indicates the month, and the last asterisk indicates the year on which it is to be performed. Following that, we also enter the file’s path and name.

After closing the file and successfully saving it, it installs the new crontab job for this file.

After installing the crontab successfully, we run the following command to execute the crontab:

linux@linux-Virtualbox:~$ sudo grep –a “bash.sh” /var/log/syslog

When we run the previous command, it displays the number of times that the crontab is executed. In our case, it is after every second.

Conclusion

In this guide, we briefly demonstrated about crontab, what the crontab is, and discussed its uses. Our main focus is to check the working of the crontab because it would lead to major issues like disabling the working of the system. After that, we discussed two methods to check if it was working. To perform this task, we used the Ubuntu 20.04 version.

Share Button

Source: linuxhint.com

Leave a Reply