How to Check If Crontab Is Working

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.


In such a case, start the cron service and verify its status again to ensure that it’s working. It should display the active (running) status.

$ sudo service cron status

 

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. Confirm with the Log Files

Another excellent way to verify if the crontab is working is by checking the log files. If you have a cron job running, you can check the log files to ascertain that crontab is working. The log files for the cron job can only reflect if the crontab is active.

In this example, we created a cron job to execute a script every minute.


Using the tail command, we can check the last lines for the log file. On Linux, the syslog contains the log files for the cron service.

Note how the log files display the output for cron every minute, confirming that our crontab is active and working.

$ tail -f /var/log/syslog | grep “cron”

 

The three ways are the easiest and most convenient methods of checking if crontab is working on your system.

Conclusion

Knowing whether crontab is working is an essential routine when working with cron jobs. We covered three ways of verifying if crontab is working: using the cron service, checking the process ID, and checking the log files for the cron service. Try the methods and see which works best for your case.



from https://ift.tt/HOboiqj

Post a Comment

0 Comments