I'm particularly interested in this for looking at the output of oneshot services that run on a timer. The --unit
flag is close, but it concatenates all the runs of the service together. The most obvious way I can think of would be to filter on PID, but that makes me worry about PID reuse / services that fork, and getting the last PID is pretty inconvenient. Is there some other identifier that corresponds to a single run of a service, that I could use to filter the logs?
EDIT: I would happily accept an authoritative "no" if that's the real answer.
Since
systemd
version232
, we have the concept of invocation ID. Each time a unit is ran, it has a unique 128 bit invocation ID. UnlikeMainPID
which can be recycled, orActiveEnterTimestamp
which can have resolution troubles, it is a failsafe way to get all the log of a particular systemd unit invocation.To obtain the latest invocation ID of a unit
To obtain the journal of the latest invocation of, say,
openipmi
, whether it failed or not, you can use the one liner(Note that the
--value
is available sincesystemd 230
, older thanInvocationID
)I'm not sure which timestamp makes the most sense but this works for me. Hopefully there is a better way of working with the timestamps from
systemctl show
than awk - could not figure out how to control the format of timestamps.You can use the boot flag to fetch only the logs from that boot. for instance
These might help you:
journalctl -u foo.service | tail -n 2
or replace 2 with expected number of lines
journalctl -u foo.service --since='2016-04-11 13:00:00'
You can as well combine them to get firstly the last run time timestamp, and then use that timestamp with the --since switch.
You can use field filters with Journalctl. E.g.
Get a list of all the available fields using:
One available field is
_PID
.You can get the PID of a running process using
pidof
orsystemctl show --property MainPID <SERVICE_NAME>
So here's how I get the logs from the current Kubernetes kubelet process:
Now tell me why I Kubernetes is so hard to install :-(
journalctl -r | grep -m1 foo.service