-1

I have below script running as background process.

#!/bin/sh while true; do : done email_kill_alert() { echo "" | mailx -s "scritp received kill signal." emailid } 

Above script is run as background process with nohup command. I want it to trigger email_kill_alert function to send out email as soon as script or process ID gets killed. I have been reading about use of trap but not sure how I implement it. Note that while loop is infinite as it constantly checks for a files.

1 Answer 1

0

Put this before the while loop: trap 'email_kill_alert' KILL

4
  • I would suggest to use SIGKILL SIGTERM SIGINT instead of KILL. Commented Oct 9, 2019 at 9:34
  • 1
    How can trapping KILL work? SIGKILL can’t be caught (or ignored). Commented Oct 9, 2019 at 9:49
  • Thank you so much guys. it worked. I used below, above while loop. trap 'email_kill_alert' SIGKILL SIGTERM SIGINT Commented Oct 9, 2019 at 20:24
  • There is another problem here. I run above script with command "nohup bash scirpt.sh &" in background. When I use "kill PID", it sends email but PID still running. And when I use "kill -9 PID", it does not send email and PID is killed. How can I make sure using "kill PID" command, it kills PID with email sent and using "kill -9 PID" it sends email with PID killed? Commented Oct 10, 2019 at 3:17

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.