ThatDude Posted January 21, 2023 Share Posted January 21, 2023 (edited) Hi I have a bash script that starts with the array and continues running in a while loop. This script is preventing my server from shutting down (if I kill the script manually shutdown works as expected). What's the most efficient way to detect a shutdown state from within the bash script so I can have it self terminate? Something like: if exists /tmp/shutdown.txt; then exit 1; fi Many thanks Edited January 21, 2023 by ThatDude more info Quote Link to comment
Solution apandey Posted January 22, 2023 Solution Share Posted January 22, 2023 (edited) 2 hours ago, ThatDude said: What's the most efficient way to detect a shutdown state from within the bash script so I can have it self terminate? You should trap SIGTERM and terminate when you see it. How you do it is up to you - set a poison pill variable or kill $$ EDIT: SIGTERM is sent to process initially on shutdown, but a SIGKILL is also sent subsequently. So just make your script handle the signals properly Edited January 22, 2023 by apandey 1 Quote Link to comment
ThatDude Posted January 22, 2023 Author Share Posted January 22, 2023 10 hours ago, apandey said: You should trap SIGTERM and terminate when you see it. How you do it is up to you - set a poison pill variable or kill $$ EDIT: SIGTERM is sent to process initially on shutdown, but a SIGKILL is also sent subsequently. So just make your script handle the signals properly Thank you. SIGINT was the missing part of the puzzle, I've never come across that before. #!/bin/bash exit_script() { trap - SIGINT SIGTERM # clear the trap kill -- -$$ # Sends SIGTERM to child/sub processes } trap exit_script SIGINT SIGTERM while true; do echo "Do some task" done Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.