LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 10-21-2012, 03:23 PM   #16
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 1,361

Rep: Reputation: 584Reputation: 584Reputation: 584Reputation: 584Reputation: 584Reputation: 584

Without that trap on the TERM signal in the function, there is a race condition that determines whether you see the "terminated" message. If the parent shell exits before it sees the SIGCHLD that the child has exited, then it will not print that message. But, if the child exits first and the parent sees the abnormal return status, then you will get that message. Forcing the child to give a normal return code avoids the issue.

You will see the "terminated" message every time if you put a "sleep 2" in the parent right after the kill.
 
2 members found this post helpful.
Old 10-21-2012, 04:55 PM   #17
Rupadhya
Member
 
Registered: Sep 2012
Location: Hoffman Estates, IL
Distribution: Fedora 20
Posts: 167

Rep: Reputation: Disabled
I think the post by rknickols above suggested putting a
Code:
 trap "exit 0" TERM
in the top of the blinker function. What that will do is if the TERM signal is fired, leave where you are executing in blinker and do an exit 0. Then the infinite loop of blinker terminates and exits with a 0.

Since the TERM was fired by the kill command, the script then returns to the line below that and continues from there.
- Raj
 
Old 10-21-2012, 05:55 PM   #18
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 1,361

Rep: Reputation: 584Reputation: 584Reputation: 584Reputation: 584Reputation: 584Reputation: 584
You need to recognize that you have two copies of the script executing in separate processes. One is running in the background executing the infinite loop in the blinker() function; the other is in the foreground running the mainline portion of the script. These are scheduled independently. When the mainline process sends the signal to the background process, there are two possibilities for the sequence of operations:
  1. It could be that the mainline process gets interrupted on return from the kill command (which is a shell builtin command, BTW), and the background process next executes, sees the TERM signal, and exits. For that case, when the mainline shell next executes, it will receive a SIGCHLD signal, check the termination status, and print a message if the termination is abnormal.
  2. Alternatively, the mainline process might continue after the kill and exit before the child terminates, leaving the blinker loop running in an orphan process, which would now be adopted by the init process (PID 1). When the background process is next scheduled, it will see the TERM signal and exit. The init process will reap the status of the terminated process (If it didn't, you would have created a zombie.), but will ignore the exit code. Thus, no message.
If you insert the sleep call that I suggested for testing, you ensure that Case 1 occurs.
 
2 members found this post helpful.
Old 10-21-2012, 06:02 PM   #19
Rupadhya
Member
 
Registered: Sep 2012
Location: Hoffman Estates, IL
Distribution: Fedora 20
Posts: 167

Rep: Reputation: Disabled
That was very well explained! I get a bit confused when a new script is spawned by another! I have done different techniques to determine that a child process has finished, but I like the above script.

- Raj
 
Old 10-21-2012, 10:33 PM   #20
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Original Poster
Rep: Reputation: 285Reputation: 285Reputation: 285
Thanks everyone. Have a good day!
 
  


Reply

Tags
effects, function, script, shell scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] queries about [:digit:] and cp --verbose effect: GNU shell MMaddoxx Linux - Newbie 4 11-09-2011 02:57 PM
Does setting of BASH as the default shell has any effect? msgforsunil Linux - Newbie 7 10-22-2006 10:41 PM
help with shell script - fill an array with values from a file delmoras Linux - General 1 07-17-2006 11:19 AM
Script to Fill In a Website Chryzmo Programming 1 12-15-2005 04:39 PM


All times are GMT -5. The time now is 03:13 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration