0
/bin/sh -version
GNU sh, version 1.14.7(1)

exitfn () {
          # Resore signal handling for SIGINT
    echo "exiting with trap" >> /tmp/logfile
    rm -f /var/run/lockfile.pid    # Growl at user,
    exit                     #   then exit script.
}
trap 'exitfn; exit' SIGINT SIGQUIT SIGTERM SIGKILL SIGHUP

The above is my function in shell script.

I want to call it in some special conditions...like when:

  1. "kill -9" fires on pid of this script
  2. "ctrl + z" press while it is running on -x mode
  3. server reboots while script is executing ..

In short, with any kind of interrupt in script, should do some action eg. rm -f /var/run/lockfile.pid but my above function is not working properly; it works only for terminal close or "ctrl + c"

Kindly don't suggest to upgrade "bash / sh" version.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Jatin Bodarya
  • 1,425
  • 2
  • 20
  • 32
  • 1
    You cannot trap signal 9. – tripleee Jun 14 '13 at 07:13
  • What does "running on -x mode" mean? Do you perhaps want a trap to fire only when the shell's flags include `-x`? That can be done inside the trap with `case $- in *x*) echo Shell has -x;; esac` – tripleee Jun 14 '13 at 07:18
  • When a controlled reboot takes place, `init` sends a regular `HUP` signal which can be trapped by regular means. There is -- obviously -- no way to detect an uncontrolled reboot. – tripleee Jun 14 '13 at 07:21
  • -x means " sh -x script.sh" so if i cannot track on reboot then how server take cares of other process pid files ? for e.g snmpd service... its pid is automaticly removed when server reboots – Jatin Bodarya Jun 14 '13 at 07:23

2 Answers2

1

SIGKILL cannot be trapped by the trap command, or by any process. It is a guarenteed kill signal, that by it's definition cannot be trapped. Thus upgrading you sh/bash will not work anyway.

Samveen
  • 3,482
  • 35
  • 52
1

You can't trap kill -9 that's the whole point of it, to destroy processes violently that don't respond to other signals (there's a workaround for this, see below).

The server reboot should first deliver a signal to your script which should be caught with what you have.

As to the CTRL-Z, that also gives you a signal, SIGSTOP from memory, so you may want to add that. Though that wouldn't normally be a reason to shut down your process since it may be then put into the background and restarted (with bg).


As to what do do for those situations where your process dies without a catchable signal (like the -9 case), the program should check for that on startup.

By that, I mean lockfile.pid should store the actual PID of the process that created it (by using echo $$ >/var/run/myprog_lockfile.pid for example) and, if you try to start your program, it should check for the existence of that process.

If the process doesn't exist, or it exists but isn't the right one (based on name usually), your new process should delete the pidfile and carry on as if it was never there. If the old process both exists and is the right one, your new process should log a message and exit.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • so how to store original pid in that file ? and then I can force the script always execute with the same pid only... – Jatin Bodarya Jun 14 '13 at 07:26
  • @user95711, it probably already does it, since it is, after all, a pidfile. However, I've updated the answer to show how a script would do it. – paxdiablo Jun 14 '13 at 07:38