Verify that a cron job has completed

9

7

Is there a command that can be run to verify that a users cron job has run successfully?

Platform is Ubuntu 8.04 LTS.

I have scripts in /home/useraccount/bin/

running

crontab -l

while logged in as user results in:

# m h  dom mon dow   command

@hourly /home/useraccount/bin/script_1

@hourly /home/locateruser/bin/script_2

I realize scripts could send email or write to a log with a timestamp, but wondering if there is just a way to verify it ran from the command line.

EDIT :

I ran

ps -ef|grep cron 

... and it shows

root      4358     1  0 Mar12 ?        00:00:00 /usr/sbin/cron

Not sure if this indicates it is running the jobs though....

CaseyIT

Posted 2010-03-30T14:02:38.043

Reputation: 3 213

i think cron is always running. but if your cron jobs starts another process, then ps can be used to test whether said new process is running – David Fox – 2010-03-30T14:44:03.637

Answers

15

grep scriptname /var/log/syslog

Paused until further notice.

Posted 2010-03-30T14:02:38.043

Reputation: 86 075

The question is specifically about user-crontabs. Normal users usually do not have access to /var/log/syslog. So this only works if you also have root on that machine. – jlh – 2017-01-09T16:47:59.843

4

/var/log/cron

you can check to if its currently running with:

ps aux

David Fox

Posted 2010-03-30T14:02:38.043

Reputation: 449

3

To make sure a script completed successfully one should really use a temp file. Create it when the job starts and delete it when it finished. This also catches crashes and avoids running the same job again in case of errors.

#!/bin/bash

# check if there is already a temp file with suffix .myscript in /tmp,
# if file exists return with status of 666
[ -f /tmp/*.bla ] && exit 666

# create a temp file with suffix .myscript
TEMP_FILE=`mktemp --suffix .myscript`
touch $TEMP_FILE

#
# script stuff
#

# we are done, clean-up after ourselves
rm $TEMP_FILE

Benjamin Bannier

Posted 2010-03-30T14:02:38.043

Reputation: 13 999

This is good practice – Henry the Hengineer – 2012-09-11T22:48:04.440

1

I've built a tool, http://cronitor.io, because I needed a solution to monitor a few cron jobs and wasn't happy with the existing options. It's free to monitor a single job, and there are paid plans for business use.

What's great about Cronitor is that you just give it a cron expression like */5 * * * M-W and you will be alerted if the job doesn't start on schedule, runs longer than expected, or overlaps itself.

Encoderer

Posted 2010-03-30T14:02:38.043

Reputation: 133

0

You can also have results emailed to you.

30 3 * * * find /home/*/Maildir/.Spam/{new,cur}/ -type f -mtime +6 -delete| \
           mail -e -s "task #1 report" postmaster@example.com

Art W

Posted 2010-03-30T14:02:38.043

Reputation: 99