?
Solved

Unix script question

Posted on 2014-12-01
5
Medium Priority
?
241 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 2000 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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month9 days, 23 hours left to enroll

762 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