execute script B whenever script A is terminated with CTRL C

David Shapero :

so I have this code that use the GPIO and before it start it turns on the LED so we know the the script was triggered and that something abnormal was going on.

echo "17" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio17/direction
echo "1" > /sys/class/gpio/gpio17/value

the problem is that sometimes people will press "ctrl c" and exit of the script without resetting the GPIO

I was wondering of there is some whay to trigger some lines of code to do certin things whenever the code is killed with CTRL C

William Pursell :

If you want to perform some cleanup, set a trap. For this case, it sounds like you want to echo 1 to gpio17/value when the script exits:

trap 'echo 1 > /sys/class/gpio/gpio17/value' 0

This will execute the command no matter how the script exits. (Unless the script gets a SIGKILL. Nothing you can do about that.)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=32947&siteId=1