When you schedule a new cron job, or audit the existing jobs in a crontab file, one of the main questions you confront is whether the job is actually running or not. In this guide we will show you the logs and command line tools to figure that out.
How do you check if your job is running?
First, check your cron logs for clues. Cron jobs are run in the background by a process called
crond. When it runs a job, it writes a few details to a system-level log file. Because these logs are written by
crond itself, they will cover all jobs, across all crontab files. That centralization makes this the right place to start checking, but it does have a catch: If you can see your job invoked in the cron logs, it doesn't mean your job is healthy: there might still be bugs or environment issues that crash your process. But it does rule-out a lot of possible failure scenarios. You will know that cron is running, your crontab is correct, file permissions are acceptable, your have the cron schedule right, and your command is at least being invoked.
The content and formatting of cron logs will vary across operating systems and distributions, but generally you will see an entry like this when a job is run:
Nov 29 16:15:01 data-processing CRON: (dataproc) CMD (/home/data-archiver.py)
About half the time cron logs are written to syslog, but this will also vary across operating systems. If you're not sure where yours are, head over to our guide on cron job logs to figure it out before you continue on.
If you are a Cronitor user, all cron logs are captured and retrievable from the dashboard and the API. For example, to see recent executions for a job, find the job on your dashboard and click to access execution history including the time, host, duration, job arguments and stdout capture.
A note about log rotation
System log files that could accrue indefinitely are usually managed by a log rotation process to keep them from filling disks and growing to unusable dimensions. The logrotate process will run hourly (using a cron job!), so even if you have a frequently-running job like this example that runs every 15 minutes, there's a possibility you are checking the server right after rotation has completed and the logs you want are actually in the file that was just rotated. This day in age, with our plentiful disk space, it's normal to leave recently-rotated logs uncompressed making it easy for you to grep them, but you may find you will need to untar a rotated file first, before you are able to grep it. In most cases, these rotated files will be in the same directory as the log files themselves, with a numeral at the end. The most recently rotated log is always ".1".
A little trick is to just check the first line of the current file so you are aware of when it was last rotated:
$ head /var/log/syslog
Nov 29 00:00:02 ip-10-0-1-169 ntpd: Soliciting pool server
Grepping the Logs
To find a job in the cron logs, you will need to match by the job command itself. First, view the crontab so you can copy the value exactly.
$ crontab -l
*/15 * * * * python /home/data-archiver.py
Next, copy the command and grep the cron log you identified in the previous step. Note: this will usually require root or sudo privileges.
$ grep "/home/data-archiver.py" /var/log/syslog
Nov 29 15:01:01 data-processing CRON: (dataproc) CMD (python /home/data-archiver.py)
Nov 29 15:15:01 data-processing CRON: (dataproc) CMD (python /home/data-archiver.py)
Nov 29 15:30:01 data-processing CRON: (dataproc) CMD (python /home/data-archiver.py)
Nov 29 15:45:02 data-processing CRON: (dataproc) CMD (python /home/data-archiver.py)
Nov 29 16:00:01 data-processing CRON: (dataproc) CMD (python /home/data-archiver.py)
Nov 29 16:15:01 data-processing CRON: (dataproc) CMD (python /home/data-archiver.py)
Here, you can see the command running every 15 minutes as expected.
Want alerts if you cron jobs stop working?
Monitor your cron jobs with Cronitor to easily collect output, capture errors and alert you when something goes wrong.
How to check if your cron job is running right now
Sometimes you will want to know if a cron job is running right now. This might be because you are expecting that your job should be running and you just want to check, or it might be because you know that your job should not be running right now and you want to rule out the possibility. In either case, you will use the ps command.
$ ps aux | grep "/home/data-archiver.py" | grep -wv grep
In this example, the job is not currently running. If it is running you will see an output like this:
$ ps aux | grep "/home/data-archiver.py" | grep -wv grep
dataproc 3301559 0.0 0.4 347308 133072 ? Sl Nov29 149:41 python /home/data-archiver.py
If you see more than one line, treat that as a yellow flag that there is something to investigate with this job. There are three common reasons you might see overlapping cron job executions:
- The job is inadvertently scheduled in more than one crontab file. This can create a real crunch on shared resources like databases and APIs.
- The job is hanging when it runs and not exiting cleanly. This might mean the job is not completing successfully.
- The job is running normally but takes longer to complete than the scheduled interval between job invocations.
If you are a Cronitor user, you can view a list of all running jobs from your dashboard or via the API by setting the "Running" filter. Additionally, jobs on the dashboard that are actively running are given a blue "Running" tag, and are marked in blue on your job timeline.
What to do if your job is not running
There are many reasons your cron jobs might not be running. Work through the possibilities with our cron job debugging guide
See all currently running cron jobs
If you are casting a wider net when validating and auditing jobs, head over to our guide to see all running cron jobs
Automatic cron job monitoring
Staying on top of background jobs and validating that they are running as expected is a time-consuming task that's better left to an automated monitoring system. We created Cronitor to solve this problem. You can install CronitorCLI on a host and monitor all of your cron jobs in minutes.