Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Details of last ran cron job in Unix-like systems?

I want to get the details of the last run cron job. If the job is interrupted due to some internal problems, I want to re-run the cron job.

Note: I don't have superuser privilege.

like image 631
kannanrbk Avatar asked Jun 21 '12 04:06

kannanrbk


People also ask

How can I tell the last time a cron job ran?

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.

How do I see cron job history in Linux?

On Ubuntu, Debian and related distributions, you will find cron jobs logs in /var/log/syslog . Your Syslog contains entries from many operating system components and it's helpful to grep to isolate cron-specific messages. You will likely require root/sudo privileges to access your Syslog.

How do I see cron jobs in Unix?

Cron jobs are typically located in the spool directories. They are stored in tables called crontabs. You can find them in /var/spool/cron/crontabs. The tables contain the cron jobs for all users, except the root user.

Is there a crontab log?

Cron log contains the following information: Timestamp – The date and time when the cron job was executed. Hostname – The hostname of the server (For example, dev-db) The cron deamon name and the PID.


1 Answers

You can see the date, time, user and command of previously executed cron jobs using:

grep CRON /var/log/syslog 

This will show all cron jobs. If you only wanted to see jobs run by a certain user, you would use something like this:

grep CRON.*\(root\) /var/log/syslog 

Note that cron logs at the start of a job so you may want to have lengthy jobs keep their own completion logs; if the system went down halfway through a job, it would still be in the log!

Edit: If you don't have root access, you will have to keep your own job logs. This can be done simply by tacking the following onto the end of your job command:

&& date > /home/user/last_completed 

The file /home/user/last_completed would always contain the last date and time the job completed. You would use >> instead of > if you wanted to append completion dates to the file.

You could also achieve the same by putting your command in a small bash or sh script and have cron execute that file.

#!/bin/bash [command] date > /home/user/last_completed 

The crontab for this would be:

* * * * * bash /path/to/script.bash 
like image 192
Stecman Avatar answered Sep 20 '22 01:09

Stecman