How to safely exit my program before KEY_POWER shutdown?
The gpio-shutdown overlay shuts down the system, but how can I monitor this KEY_POWER event and safely shuts down my own program (save import files) before the system shutdown?
- thagrol
- Posts: 4041
- Joined: Fri Jan 13, 2012 4:41 pm
- Location: Darkest Somerset, UK
- Contact: Website
Re: How to safely exit my program before KEY_POWER shutdown?
Given that AIUI all the overlay does is send a KEY_POWER event you'd do it the same way you'd trap a clean shutdown by any other means: trap a few signals and act on them.
The exact way you do that depends on which language your programme is written in.
The exact way you do that depends on which language your programme is written in.
Arguing with strangers on the internet since 1993.
Re: How to safely exit my program before KEY_POWER shutdown?
Does it mean I won't be able to act upon the KEY_POWER event? If so can I register a system event and act on it, so I don't have to constantly monitor the gpio pin. I use c++ but has limited knowledge about Linux.thagrol wrote: Given that AIUI all the overlay does is send a KEY_POWER event you'd do it the same way you'd trap a clean shutdown by any other means: trap a few signals and act on them.
The exact way you do that depends on which language your programme is written in.
- thagrol
- Posts: 4041
- Joined: Fri Jan 13, 2012 4:41 pm
- Location: Darkest Somerset, UK
- Contact: Website
Re: How to safely exit my program before KEY_POWER shutdown?
Can't help you with C++, I don't know it. My understanding is that when the switch is pressed the kernel sees a KEY_POWER event which triggers the normal shutdown process. You shouldn't need to monitor the gpio yourself, just have your programme trap and respond to a SIGTERM (I think it's SIGTERM, google will probably know).njsss wrote: ↑Mon Feb 25, 2019 7:28 pmDoes it mean I won't be able to act upon the KEY_POWER event? If so can I register a system event and act on it, so I don't have to constantly monitor the gpio pin. I use c++ but has limited knowledge about Linux.thagrol wrote: Given that AIUI all the overlay does is send a KEY_POWER event you'd do it the same way you'd trap a clean shutdown by any other means: trap a few signals and act on them.
The exact way you do that depends on which language your programme is written in.
Arguing with strangers on the internet since 1993.
- DougieLawson
- Posts: 40579
- Joined: Sun Jun 16, 2013 11:19 pm
- Location: A small cave in deepest darkest Basingstoke, UK
- Contact: Website Twitter
Re: How to safely exit my program before KEY_POWER shutdown?
Normal shutdown sends SIGTERM to every process. If the process doesn't end normally then 90s later it sends SIGKILL.
You can trap SIGTERM with a C++ signal handler. You can't trap SIGKILL.
https://www.tutorialspoint.com/cplusplu ... ndling.htm gives you an example of trapping signals.
You can trap SIGTERM with a C++ signal handler. You can't trap SIGKILL.
https://www.tutorialspoint.com/cplusplu ... ndling.htm gives you an example of trapping signals.
Any language using left-hand whitespace for syntax is ridiculous
Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.
Any requirement to use a crystal ball or mind reading will result in me ignoring your question.
Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.
Any requirement to use a crystal ball or mind reading will result in me ignoring your question.
Re: How to safely exit my program before KEY_POWER shutdown?
You need to write a signal handler for your program. SIGTERM is the one you should take care of first.
ghans
ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org
Re: How to safely exit my program before KEY_POWER shutdown?
I tried to implement a simple test in both c++ and python, but couldn't trap SIGTERM. Shutdown happens immediately either by pull the shutdown-pin down or just type "sudo shutdown now".