I want to know the time duration since a systemd
service was started, using the --property=ActiveEnterTimestamp
i can see the time it has started, and i would like to compare it with current time and get a value in seconds or minutes, how can i achieve this with bash
If i use this solution i am getting a string, but i cannot actually get any time object to make the decision, any help on this would be appreciated.
CodePudding user response:
You could use GNU date to convert the ActiveEnterTimestamp value to seconds-since-the-epoch, then subtract the current seconds-since-the-epoch to get the running time in seconds.
servicestartsec=$(date -d "$(systemctl show --property=ActiveEnterTimestamp your-service-here | cut -d= -f2)" %s)
serviceelapsedsec=$(( $(date %s) - servicestartsec))
Substitute "your-service-here" for your actual service name.
The first line assigns the start time in seconds by extracting the date portion of systemctl show --property=ActiveEnterTimestamp...
(using cut
to extract the second =
-delimited field) and then passing it to GNU date
and asking for output in seconds-since-the-epoch.
The second line simply subtracts that start time from the current time to get an elapsed time in seconds. Divide that as needed to get elapsed minutes, hours, etc.