You are not logged in.

#1 2015-11-18 12:12:45

belette
Member
Registered: 2014-11-17
Posts: 121

[Solved]Bash script:how to differentiate shutdown -r against poweroff?

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

#2 2015-11-18 12:23:41

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,595
Website

Re: [Solved]Bash script:how to differentiate shutdown -r against poweroff?

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 packagesZsh and other configs

Offline

#3 2015-11-18 12:32:42

mauritiusdadd
Member
From: Benevento, Italy
Registered: 2013-10-27
Posts: 776

Re: [Solved]Bash script:how to differentiate shutdown -r against poweroff?

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 hmm


About me - github

-- When you have eliminated the impossible, whatever remains, however improbable, must be the truth -- Spock | Sherlock Holmes

Offline

#4 2015-11-18 14:35:23

belette
Member
Registered: 2014-11-17
Posts: 121

Re: [Solved]Bash script:how to differentiate shutdown -r against poweroff?

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

#5 2015-11-18 16:40:23

TheChickenMan
Member
From: United States
Registered: 2015-07-25
Posts: 354

Re: [Solved]Bash script:how to differentiate shutdown -r against poweroff?

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

#6 2015-11-18 18:55:42

belette
Member
Registered: 2014-11-17
Posts: 121

Re: [Solved]Bash script:how to differentiate shutdown -r against poweroff?

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

Board footer

Powered by FluxBB