Skip to content
View in the app

A better way to browse. Learn more.

Unraid

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

detect shutdown state from bash script

Featured Replies

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 by ThatDude
more info

Solved by apandey

  • Solution
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 by apandey

  • Author
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

 

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.

Guest
Reply to this topic...

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.