Link to home
Start Free TrialLog in
Avatar of mokkan
mokkan

asked on

Unix script question

I'm trying to understand this line. I can only undertand that lock file is removing, but I am not sure what exactly this entire line is doing.  Can anyone explain ?


trap 'echo " Removing the lock "; rm -f ${lock_file} 2> /dev/null ; exit 0' HUP INT QUIT TERM STOP
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Your signals:

HUP (hangup)  means that the controlling terminal/line has been closed. It is often used to instruct background processes to reload their configuration.
INT (interrupt) is usually initiated with <Ctrl><C> to interrupt the process.
QUIT means quit the process and write a core dump.
TERM (terminate) is sent to a process to request its termination and is practically identical to INT.
STOP  stops a process and allows for later resumption.
Avatar of mokkan
mokkan

ASKER

Thank you for the quick reply. IF I undertand correctly, this line will be executed only when this program exit with 0.

For an example here    is the sample.ksh.  The trap  will execute only if the program exit with 0.


while [ true ]
do
  echo "my pid is : $pid"
  ls -l ${lock_file}
  if [ ! -f ${lock_file} ]
  then
    echo "lock file : ${lock_file} no  Terminating..."
    exit 0
  fi
  sleep 2
done
As I wrote: The command sequence is called as soon as one of the mentioned signals is caught.
"exit 0" is part of that command sequence and is is in no way related to the actual behaviour (e. g. exit value) of the shell.

There is a special signal "EXIT" which is sent when the script exits (well, a bit before the real exit) and can be caught with "trap". Your "trap" setting does not catch EXIT!
Avatar of mokkan

ASKER

Thank you very much. I got it now.