Solved

Unix script question

Posted on 2014-12-01
5
240 Views
Last Modified: 2014-12-01
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
0
Comment
Question by:mokkan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 40474372
The trap command intercepts signals to the shell script, in your case "HUP INT QUIT TERM STOP". It has to be issued only once.

As soon as one of those signals is caught the command sequence enclosed in single quotes is executed:
- echo a message
- remove a file
- exit with return code 0

You can get a list of possible signals with "kill -l".
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40474399
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.
0
 

Author Comment

by:mokkan
ID: 40474422
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
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40474446
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!
0
 

Author Closing Comment

by:mokkan
ID: 40474539
Thank you very much. I got it now.
0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Fine Tune your automatic Updates for Ubuntu / Debian
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question