How to detect pending system shutdown on Linux?

前端 未结 9 978
死守一世寂寞
死守一世寂寞 2020-12-05 14:37

I am working on an application where I need to detect a system shutdown. However, I have not found any reliable way get a notification on this event.

I know that o

相关标签:
9条回答
  • 2020-12-05 15:05

    see man systemctl, you can determine if the system is shutting down like this:

    if [ "`systemctl is-system-running`" = "stopping" ]; then
        # Do what you need
    fi
    

    this is in bash, but you can do it with 'system' in C

    0 讨论(0)
  • 2020-12-05 15:10

    There is no way to determine if a SIGTERM is a part of a shutdown sequence. To detect a shutdown sequence you can either use use rc.d scripts like ereOn and Eric Sepanson suggested or use mechanisms like DBus.

    However, from a design point of view it makes no sense to ignore SIGTERM even if it is not part of a shutdown. SIGTERM's primary purpose is to politely ask apps to exit cleanly and it is not likely that someone with enough privileges will issue a SIGTERM if he/she does not want the app to exit.

    0 讨论(0)
  • 2020-12-05 15:15

    Its a little bit of a hack but if the server is running systemd if you can run

    /bin/systemctl list-jobs shutdown.target

    ... it will report ...

    JOB UNIT            TYPE  STATE
    755 shutdown.target start waiting     <---- existence means shutting down
    
    1 jobs listed.
    

    ... if the server is shutting down or rebooting ( hint: there's a reboot.target if you want to look specifically for that )

    You will get No jobs running. if its not being shutdown.

    You have to parse the output which is a bit messy as the systemctl doesnt return a different exit code for the two results. But it does seem reasonably reliable. You will need to watch out for a format change in the messages if you update the system however.

    0 讨论(0)
  • 2020-12-05 15:22

    Maybe a little bit late. Yes, you can determine if a SIGTERM is in a shutting down process by invoking the runlevel command. Example:

    #!/bin/bash
    trap "runlevel >$HOME/run-level; exit 1" term
    read line
    echo "Input: $line"
    

    save it as, say, term.sh and run it. By executing killall term.sh, you should able to see and investigate the run-level file in your home directory. By executing any of the following:

    sudo reboot
    sudo halt -p
    sudo shutdown -P
    

    and compare the difference in the file. Then you should have the idea on how to do it.

    0 讨论(0)
  • 2020-12-05 15:22

    Making your application responding differently to some SIGTERM signals than others seems opaque and potentially confusing. It's arguable that you should always respond the same way to a given signal. Adding unusual conditions makes it harder to understand and test application behavior.

    Adding an rc script that handles shutdown (by sending a special signal) is a completely standard way to handle such a problem; if this script is installed as part of a standard package (make install or rpm/deb packaging) there should be no worries about control of user machines.

    0 讨论(0)
  • 2020-12-05 15:22

    When the system shuts down, the rc.d scripts are called.

    Maybe you can add a script there that sends some special signal to your program.

    However, I doubt you can stop the system shutdown that way.

    0 讨论(0)
提交回复
热议问题