Solved

Unix script question

Posted on 2014-12-01
5
233 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
  • 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now