0

I'm running various Cron tasks in my Linux machine. Recently I updated it and want to check if it is working fine. For example:

0 6 * * 1-5 /scripts/foo.sh

Except for the case that I check during the scheduled time that the process is actually running ps x , is there a way to simply check the executed program/scripts within last 24 hours or something like that?

I'm new to Linux. Any idea/hint will work for me!

2
  • 1
    Not that I can think of. But you can make it write a log file. And then output date at the beginning and date at the end. But, without you doing more, it doesn't keep track of what programs have run, regardless of whether or not you used cron. That's something you'll need to do yourself. Some sysadmin programs that you can set up to run with cron (i.e., backup programs) would write to /var/log, for example. – Ray Feb 15 at 6:10
  • Are you running a flavor of Ubuntu? – David Feb 15 at 6:38
2

I add date and printf statements near the beginning and end of my scripts to write the date and time to a text file in my home directory. I also direct the output to that file so it records the output you would get if it was run in a terminal. Here is an example

#!/bin/sh

set -e

date >> /home/john/anacronjobs.txt

printf "pizero Starting!\n" >> /home/john/anacronjobs.txt

rsync -avh pi@pizero1.local:/home/pi/Python /home/john/pizero1 >> /home/john/anacronjobs.txt 2>&1

printf "pizero Done!\n\n" >> /home/john/anacronjobs.txt

date >> /home/john/anacronjobs.txt

exit

Using the >> operator ensures it appends to rather than overwrites the file

The output looks like this

Sun 14 Feb 11:06:04 GMT 2021
pizero Starting!
receiving incremental file list
Python/test.txt

sent 6.53K bytes  received 6.42K bytes  278.52 bytes/sec
total size is 1.17M  speedup is 90.55
pizero Done!
1
  • 1
    Easier to add timestamp using ts, printf "some string" | ts >> file – bac0n Feb 15 at 14:35
1

Cron traditionally logs to /var/log/syslog, so a simple grep for the strings CRON and/or foo.sh should determine whether execution was attempted.

On modern systemd-based systems, you can access equivalent log entries via journalctl ex.

journalctl -u cron.service --since -1day | grep -F foo.sh

or perhaps

journalctl -u cron.service --grep 'foo\.sh' --since -1day

if your system's journalctl was compiled with pattern matching support.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.