You are not logged in.
Hi,
I need to differentiate if my system has been rebooted or powered-off in a cron script.
I know the command
last -x|grep shutdown | head -1
but this doesn't indicate if the system has been powered-off...in fact if I power off or shutdown -r now I got the same message
last -x|grep shutdown | head -1
shutdown system down 4.2.5-gnu-1 Wed Nov 18 13:01 - 13:01 (00:00)
Is there a simple command to know the latest status (shutdown / poweroff) ?
I know I can grep the information into journactl but I need to play with some time range and it is not trivial in case of my script...
Many thanks
Last edited by belette (2015-11-18 18:57:01)
Offline
Maybe I'm not undestanding your request. I believe both those are just symlinks to /usr/bin/systemctl which gets called in either case, no?
CPU-optimized Linux-ck packages @ Repo-ck • AUR packages • Zsh and other configs
Offline
I think he's asking how to check if, the last time the machine was halted, it was a simple reboot or a full shutdown. The only thing that I can think at the moment is to check the journal: for example something like the following will output 1 if the machine was rebooted and 0 otherwise (full shutdown, power fault, etc.)
$ journalctl -u systemd-logind -b -1 | awk '/System is/ {if ($NF == "rebooting.") {print "1"} else {print "0"}}'
but it is not very elegant
Offline
hi many thanks for your replies.
Yes mauritiusdadd you understood correctly what I am trying to achieve.
I was testing and thinking about the same thing for journalctl the only thing I didn't figured out yet is how to find only the last poweroff/shutdown status (and not all the other which occur before in time) but perhaps your idea would fit?
Offline
Could you override "shutdown" and "reboot" with scripts in /usr/local/bin which call the original binaries but also create some special custom log file in /var which you can then check? I think that's earlier in path so it should execute your custom scripts instead when you call shutdown or reboot from terminal.
Last edited by TheChickenMan (2015-11-18 16:42:54)
If quantum mechanics hasn't profoundly shocked you, you haven't understood it yet.
Niels Bohr
Offline
Many thanks for all of theses good ideas.
at the end the best and simplest way I found is
journalctl -b -1 | grep 'Power-Off'
in fact in case of poweroff this is logged in journactl (Shutting down is logged in both situation so useless to try to match it)
Offline