How to check the last run time (linux)?

Get help for specific problems
Posts: 11
Joined: 5 Jun 2024

bladewinds

Hello,
so I have configured the batch file and added it to the crontab (Ubuntu) to be executed daily at 17h.
Now, since cron doesn't care if a job is executed or not (for example if PC is off at that time), I need a way to check when was the last run.

Basically, I want to run a custom script in cron every hour and check the time of the last FFS run.

If the time is past 17h and FFS didn't run (PC was off), then run FFS now.
If the time is past 17h and FFS did run at 17h, then do nothing.

So, I just need a way to check the date and time of the last FFS run. Is there an official command for that or maybe some other ideas on how to achieve that?

Thanks :)
User avatar
Posts: 3909
Joined: 11 Jun 2019

xCSxXenon

You can parse the log file that FFS produces to obtain the date/time
Posts: 309
Joined: 7 Jan 2018

bgstack15

or check the timestamp of the log file? That would be when it was last modified, so when the last action of the sync process occurred.
stat --format '%y' /path/to/logfile
If FreeFileSync doesn't make such a useful log, then maybe capturing standard output from cron might be useful (assuming FreeFileSync prints to stdout). In your cron entry:
0 17 * * * bladewinds  /opt/FreeFileSync/FreeFileSync --other-params-here 1>~/log/sync.log 2>&1
Or you could even run a series of steps that would touch a file when the sync has run, such as:
0 17 * * * bladewinds  /opt/FreeFileSync/FreeFileSync --other-params-here && touch ~/log/sync.timestamp
Where the && means run the second command (touch) only if the first command finishes successfully. I'm not sure if FreeFileSync, as a graphical program, would return a non-zero response if it finishes with error.
But also, you might want to check the specific cron implementation in your version of Ubuntu. Anacron and cronie have mechanisms in place for running a job if it was missed, etc. I found https://www.baeldung.com/linux/crontab-scheduled-jobs-computer-shutdown helpful to understand briefly how anacron might be used.
Posts: 11
Joined: 5 Jun 2024

bladewinds

or check the timestamp of the log file? That would be when it was last modified, so when the last action of the sync process occurred.
stat --format '%y' /path/to/logfile
If FreeFileSync doesn't make such a useful log, then maybe capturing standard output from cron might be useful (assuming FreeFileSync prints to stdout). In your cron entry:
0 17 * * * bladewinds  /opt/FreeFileSync/FreeFileSync --other-params-here 1>~/log/sync.log 2>&1
Or you could even run a series of steps that would touch a file when the sync has run, such as:
0 17 * * * bladewinds  /opt/FreeFileSync/FreeFileSync --other-params-here && touch ~/log/sync.timestamp
Where the && means run the second command (touch) only if the first command finishes successfully. I'm not sure if FreeFileSync, as a graphical program, would return a non-zero response if it finishes with error.
But also, you might want to check the specific cron implementation in your version of Ubuntu. Anacron and cronie have mechanisms in place for running a job if it was missed, etc. I found https://www.baeldung.com/linux/crontab-scheduled-jobs-computer-shutdown helpful to understand briefly how anacron might be used. bgstack15, 17 Jun 2024, 20:42
Good thinking, I will take that timestamp approach then.