How to detect pending system shutdown on Linux?

前端 未结 9 1103
死守一世寂寞
死守一世寂寞 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: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.

提交回复
热议问题