Categories
SysOps

How to determine when service was started

Determine when systemd service was started.

Basic operations

The easiest way to determine when service was started is to display service status.

$ systemctl status unbound
● unbound.service - Unbound DNS server
   Loaded: loaded (/lib/systemd/system/unbound.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2021-04-05 21:11:50 CEST; 1 weeks 0 days ago
     Docs: man:unbound(8)
  Process: 359 ExecStartPre=/usr/lib/unbound/package-helper chroot_setup (code=exited, status=0/SUCCESS)
  Process: 380 ExecStartPre=/usr/lib/unbound/package-helper root_trust_anchor_update (code=exited, status=0/SUCCESS)
 Main PID: 544 (unbound)
    Tasks: 1 (limit: 2181)
   CGroup: /system.slice/unbound.service
           └─544 /usr/sbin/unbound -d

Apr 05 21:11:50 desktop systemd[1]: Started Unbound DNS server.

You do not need to display the whole status to get this information.

$ systemctl show --property=ActiveEnterTimestamp unbound
ActiveEnterTimestamp=Mon 2021-04-05 21:11:50 CEST
$ systemctl show --property=ActiveEnterTimestamp --value unbound
Mon 2021-04-05 21:11:50 CEST

In a case like this, specify the timezone when you want to share this information.

$ TZ=GMT systemctl show --property=ActiveEnterTimestamp --value unbound
Mon 2021-04-05 19:11:50 GMT

Automation

You can calculate this value inside a shell script if you want to perform some operations on it.

Get the boot time (see how to determine when the operating system was booted).

$ awk '/btime/ {print $2}' /proc/stat 
1617649812

Determine the exact moment service was started since system boot.

$ systemctl show --property=ActiveEnterTimestampMonotonic --value unbound
98510114

Calculate and display when the service was started.

$ date -d "@$(echo "1617649812 + (98510114/1000000)" | bc)"
Mon Apr  5 21:11:50 CEST 2021

Additional notes

There are different timers, so you are not limited only to the described one.

$ systemctl show unbound | grep Timestamp
WatchdogTimestampMonotonic=0
ExecMainStartTimestamp=Mon 2021-04-05 21:11:50 CEST
ExecMainStartTimestampMonotonic=98502163
ExecMainExitTimestampMonotonic=0
StateChangeTimestamp=Mon 2021-04-05 21:11:50 CEST
StateChangeTimestampMonotonic=98510114
InactiveExitTimestamp=Mon 2021-04-05 21:11:50 CEST
InactiveExitTimestampMonotonic=98434450
ActiveEnterTimestamp=Mon 2021-04-05 21:11:50 CEST
ActiveEnterTimestampMonotonic=98510114
ActiveExitTimestampMonotonic=0
InactiveEnterTimestampMonotonic=0
ConditionTimestamp=Mon 2021-04-05 21:11:50 CEST
ConditionTimestampMonotonic=98429908
AssertTimestamp=Mon 2021-04-05 21:11:50 CEST
AssertTimestampMonotonic=98431845

See org.freedesktop.systemd1 manual page.