How to view a cron job running currently?
Asked Answered
O

7

42

I have a cron job set up for daily execution (on my own ubuntu, just for trial) like so:

0 0 * * * /path/exec.sh

It is been set for daily execution. I usually open my machine around 8a.m. I would like to find out
- what time my cron job ran, if it has already run ?
- I would also like to see if any of my cron job is running at the moment?

Is there a way to find out IF a cron job is actually running at the moment?

Oliphant answered 27/4, 2016 at 7:20 Comment(0)
S
78

to check if cron is actually running anything at this moment in time (works on ubuntu)

pstree -apl `pidof cron`

and you'll either get

2775,cron # your pid (2775) will be different to mine :-)

or a tree output with all the child processes that cron is running (it may not name them if you don't have sufficient privileges) and as Hamoriz says the logs are in /var/log/syslog so

grep CRON /var/log/syslog

will get you the logs just for cron

Spitsbergen answered 27/4, 2016 at 7:33 Comment(1)
I ran the command and got cron,273458 -f, I don`t get what the "-f" implies?Isacco
H
8

you can display all active cron jobs using

crontab -l

To vie cron job history, you can show logs by-

grep CRON /var/log/syslog

You can find cron daemon is running or not by-

pgrep cron

or all about cron status by-

systemctl status cron
Humanism answered 10/2, 2022 at 6:41 Comment(0)
P
6

I would also like to see if any of my cron job is running at the moment?

ps aux |grep "path/exec.sh"

what time my cron job ran ?

Cron log only show when start task off crond, not log when end. You need put this on your task or embedded your task en one scritp with control time of start and end.

if it has already run ?

cat /path/logs/messages or /path/logs/file when your system put logs of crond (this depends on your distribution settings or your computer)

Preselector answered 27/4, 2016 at 9:51 Comment(2)
Can you explain / decode the output of this command.Sauls
This will show the executed grep command as part of the result, too. You could get rid of it e.g. by using ps aux | grep -v "grep" | grep "path/exec.sh".Dobbs
G
1

If you are just interested in knowing if your cronjob is currently running and when it last started, then I find the following the easiest way:

0 0 * * * touch /path/cron.start;  /path/exec.sh; touch /path/cron.end

This will create a file /path/cron.start with a timestamp which is the start time. When the job finishes, the file /path/cron.end will have the timestamp when the cron finished. So a simple ls -lrt /path/cron.{start,end} will tell you when the job started and if it is still running (the order will tell you if it is still running).

Gabrielgabriela answered 18/9, 2019 at 12:32 Comment(1)
Or even better, have your cron job write a log file with timestamps so you can see how it's progressing. Then just tail -f that file to see what it's up to.Pregnancy
J
0

I think you can only check your processes or monitor /var/log/cron

update: on ubuntu the logs are in /var/log/syslog

Joinery answered 27/4, 2016 at 7:23 Comment(0)
O
0

On Centos the cron logs get generated under /var/log/cron*

Or if its not then you can follow these steps to enable it:

vi /etc/rsyslog.conf

In the file, you will find the following line:

#cron.*                     -/var/log/cron

Uncomment the line (remove the #) and save the rsyslog..conf file. Next you will have to restart the rsyslog daemon:

service rsyslog restart
Oxidate answered 24/9, 2019 at 12:23 Comment(0)
T
0

On Ubuntu OS, you can access this log file. If your cronjob is currently running, you can view this command line by executing:

sudo tail -f /var/log/syslog
Twickenham answered 20/5, 2024 at 8:17 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.