Cron Logs (with Timestamps)

I am very new to the Linux world, mainly using windows machines. However, most of the web hosting I use is run through Linux machines and as I approach some of the more advanced features of WordPress I find myself needing to move towards the command-line (e.g. SSH). Because I am self-taught and discover things as I go I thought it might be handy to put some of my tips from along the way on my site. This post specifically focuses on how to keep track of your Cron jobs and ensure that they are running.



Within Linux, if you run a shell command you will often get an output on the screen, for example

'echo hello' will output hello.


This if fine for a shell command, but what about if you want to run it some other way so that you can store the output. Thankfully you can redirect the output to a file like below;

'echo hello >> /home/username/output.log 2>&1 ' will output hello into /home/username/output.log


That sets us up very well because it means that we can now use this within many of our Cron jobs, for example in your Cron if you entered the above, it would enter ‘hello’ once for each line it was run. That is good and bad, and no doubt you can see the issue; how do I know if a specific Cron job ran at a specific time?

Sadly with the above sample you cannot, however, the answer is simple, you just prepend date && command and put the lot in brackets as per the below;

'(date && echo hello) >> /home/username/output.log 2>&1' will output the date in one line, then hello into /home/username/output.log


I hope this helps, next time you have a Cron you might want to consider outputting the result to a file so you can keep track of how it is running. Just remember if you have a frequent cron the file will get big, so you may need to purge it every so often (maybe another Cron job :p)