Solved

Unix script question

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.

867 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

15 Experts available now in Live!

Get 1:1 Help Now