You are not logged in.
I'd like to get the last run time of a systemd timer in a user session. It is for a taskbar-style information display for when my mbsync timer service last ran.
Things that come close:
1) `systemctl --user list-timers` provides the information, but it is nested among many other columns. The columns are separated by spaces, but column items may sometimes have internal spaces and othertimes not - so there is no trivial invocation of cut or awk that will pull out just the last run time. I do have an ugly awk hack that seems to work so far but it is far from ideal and may fail under unanticipated conditions:
systemctl --user list-timers mbsync.timer | awk 'NR==2 { end=index($0,"ago"); print substr($0,end-9,9);}'
2) Systemd-wide timers create files in /var/lib/systemd/timers/stamp-* whose creation time indicates the last run time of the timer unit. This would be perfect, except that user session timers do not create these files. I searched my entire filesystem while my user session timer was running, and there are no other stamp-* files anywhere.
A perfect solution would be some undocumented flag or option[1] for systemctl that can report just a specified column of the list-timers output, or a location of where the equivalent of the stamp- files are for user sessions if such files are created.
[1] or documented somewhere I haven't found yet
EDIT: my awk hack is already failing when the elapsed time is less than a minute, I get "EDT 36s" as a result. I could certainly make it more robust, but then it'd be longer and uglier yet.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
FWIW, it appears that systemd --user is creating the stamp-* files in $HOME/.local/share/systemd/timers/. However it's only for the OnCalendar timers (2 of my 11 timers) which may be why it's not showing up for you.
Scott
Offline
Thanks, that would be why I'm not seeing the user stamp files. Good to know. I only have this one --user timer and it's not an OnCalendar schedule.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
I don't know any way for reading the last run time of a systemd timer directly, but you can always read the last run time of the systemd unit which is activate by the timer. For example, assuming that your mbsync.timer activates the unit mbsync.service then you can use something like the following:
systemctl show mbsync.service --property=ExecMainStartTimestamp | awk -F= '{print $2}'
to get the last time the unit was activated or
systemctl show mbsync.service --property=ExecMainExitTimestamp | awk -F= '{print $2}'
to get the last time the unit exited.
--edit[0]: updated the commands per suggestion below. Thanks Raynman
--edit[1]: Just found that the last run timestamp of a timer can be retrieved accessing the property LastTriggerUSec but it is shown as years/months/days since the epoch.
Last edited by mauritiusdadd (2015-06-10 12:01:48)
Offline
You can use --property=ExecMainStartTimestamp with the systemctl show command (and -F for awk).
Offline
Thanks, thats useful - but that gives the full timestamp, not how ago it was. To turn it into the latter, I'd have to wrap it in a date command and subtract it from the current date:
echo $(($(date +%s) - $(date -d "$(systemctl --user show mbsync.service --property=ExecMainStartTimestamp | awk -F= '{print $2}')" +%s)))
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
2) Systemd-wide timers create files in /var/lib/systemd/timers/stamp-* whose creation time indicates the last run time of the timer unit. This would be perfect, except that user session timers do not create these files. I searched my entire filesystem while my user session timer was running, and there are no other stamp-* files anywhere.
I just did some tests and it seems that for user session timers, the stamp-* files are stored in the directory ~/.local/share/systemd/timers but only for the timers that have the option Persistent=True (even if they don't have the OnCalendar option).
Last edited by mauritiusdadd (2015-06-10 12:49:53)
Offline
Thanks, that works perfectly satisfying option 2 in my original post. But in hindsight, Raynman's approach gives about the same info as I still need to use a date subtraction to get how long ago it was, though this one is a little cleaner:
sec=$(( $(date +%s) - $(stat -c %Y ~/.local/share/systemd/timers/stamp-mbsync.timer) )) && echo $(($sec / 60)):$(($sec % 60))
But that works as a solution for me. If anyone happens to know/find a way to have systemctl itself spit out only the 'PASSED' column for list-timers, that would be a bit better yet.
EDIT: this does the trick:
systemctl --user status mbsync.service | awk '/Active:/ { sub(/[^;]*; /,"",$0); print; }'
Last edited by Trilby (2015-06-14 16:47:24)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline