A fairly common question for scheduled tasks that run in the background is, “Are they running?” These tasks, commonly known as cron jobs, are tasks that need to automatically run for maintenance, backups, and other functional needs for the website and server.
In order to determine if your cron jobs are running, you will need to distinguish between the scheduling of the tasks (the crontab) and the actual tasks themselves (the cron jobs). Cron jobs that do not produce output results require that you become familiar with the server logs. Upon determining the results of the server logs, you can then troubleshoot cron jobs that are not running. You can also modify a script so that it produces an output to show that it is running.
The following article will help you distinguish between a crontab and a cron job, two ways you can see when a cron job is running, and how to troubleshoot cron jobs that are not running.
- Understanding the Difference Between Crontab and Cron Job
- 2 Ways to See If Your Cron Job is Running
- Troubleshooting a Cron Job
NOTE: To perform this function, you will need to have root access on your VPS or Dedicated server.
Difference Between Crontab and Cron Job
To help prevent confusion between a beginner administrator and a developer or experienced server administrator, it is important to understand the meaning of the terms “crontab” and “cron job.”
If you search for “scheduling cron jobs”, you will find the term crontab defined or described with hardly any introduction. The term crontab is short for “cron table’ and is a Linux command for scheduling cron jobs. The crontab is also the actual text file that contains the scheduling for cron jobs.
A cron job is an actual code or script that you are trying to run in the background at a particular scheduled time. The Linux command for a cron job is simply cron.
So, if someone asks you how a cron job is scheduled in the crontab, you know that they are referring to the timing and frequency information applied to the cron job in the text file known as the crontab.
For the purposes of cPanel, the crontab and cron job can be edited directly in the cPanel interface on the Cron Jobs page. Here you can see the upper portion of the Cron Jobs page:
If you have any existing cron jobs, then you will see them at the bottom of the Cron Jobs page. You will see the timing for the cron job (from the crontab file) as well as the code for the cron job. You can add, edit, and delete cron jobs and their corresponding schedules on this page. Here’s how the bottom section looks:
Using the crontab information provided in cPanel you can determine when a cron job was supposed to run. In some cases, cron jobs will not generate an output, so it is important that you become familiar with the generated log files and how to search them.
2 Ways to See If Your Cron Job is Running
Checking the Log Files
In this example, we will search the crontab log by using the fictional file name script.sh. The command commonly used to search for files or characters in a particular pattern is grep. This terminal command can be used to search the log file for instances when “script.sh” is running. This is the command used to search for “script.sh” in the cron log file:
grep “script.sh” /var/log/cron
If you were to search for a different script or term in the log file, then you would simply replace “script.sh” with the name of your script file or text string that you’re trying to find in the log.
Using the grep command, you can view the log to see the last time when the specific script in the cron job was executed. If the cron job does not produce a visible output, then you would need to check to see if the cron job has actually taken place.
For example, you can see the last time when a cache clearing cron job was executed. You would then check to see if the cache is cleared by seeing if the cache is empty or not. Here’s an example of seeing a cronjob running by checking the log:
The log shows a record of when the file was run. Please see our tutorial if you need information on how to access the terminal through cPanel.
Using Output In a Script to Show a Running Cron Job
The easiest way to see if a cron job (with its crontab settings) is working is to edit an existing cron job so that it produces a visible output. You can add a line of code in your existing script to output a result when the script is run. If the result of this command produces an output, then you can use this output to confirm that your cron script is running.
The modification of a cron job script should not be performed by a user who is not familiar with the code or script being executed. Since cron jobs can be written in many different ways, you may need to speak with an experienced developer to help prevent any issues with your website.
Troubleshooting Cron Jobs
If you are not seeing any activity in the crontab log or if you are not receiving an email regarding the cron activity (for cron jobs with an output), then you will want to first double-check that the time parameters for the cron job are correct. This will help to determine if (and when) the cron is supposed to be running so that you can properly troubleshoot possible issues.
If you are still suspecting a problem with the cron job, then there are normally 3 possibilities why it might not be running:
- The cron daemon (the system process that runs cron jobs) could not run the script or was unable to send an email
- There were issues with the mail server mailing the output or the email was lost
- The cron job did not produce an output or error message
In all three cases, the cron job is silently failing. In other words, there is no visual result of the cron job running.
In the first possibility, you will need to check the system log on the webserver in order to search for errors that may be recorded during the attempted execution of the cron job. You can then use the error message to isolate where the cron job script may be failing and make necessary corrections.
You may also need to go through the mail server log to double-check if there was an attempt to send an email after the completion of the cron job. Remember that the designated email address is set near the top of the cron job page in cPanel. You can use the email address to isolate the mail attempts in the mail server log.
You would also check the mail server log if you are seeing the record of the cron job running, but no expected email. Emails are sent on the successful completion of cron jobs with an output.
In the third possibility, you will need to add code to your cron job so that it produces an easily tracked output to verify that it is running.
If you are unsure how to check mail server logs, then contact your hosting service’s technical support or work with an experienced administrator to use the logs to help you identify problems related to the mail server.
There are many ways that a cron job can fail. If you are not sure how to code cron jobs then consult with an experienced developer to help you make corrections or modify code for troubleshooting purposes.
You can read our full guide on setting up cron jobs in cPanel if you need more information about that topic.