[KSH/Bash] Starting a parent process from a child process?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [KSH/Bash] Starting a parent process from a child process?
# 1  
Old 03-29-2010
[KSH/Bash] Starting a parent process from a child process?

Hey all, I need to launch a script from within 2 other scripts that can run independently of the two parent scripts... Im having a hard time doing this, if anyone knows how please let me know.

More detail.

ScriptA (bash), ScriptB (ksh), ScriptC (bash)

ScriptA, launches ScriptB
ScirptB, launchs ScriptC

From ScriptC.. I need to kill ScriptA and ScriptB make some changes.. then luanch ScriptA again.

So Im trying to luanch ScriptC from ScriptB like so:

Code:
/tmp/ScriptC >> /tmp/log.log 2>&1 &
disown %1

That's not working because when I kill ScriptA and ScriptB... ScriptC gets a term signal and bails out. What am I missing here?
# 2  
Old 03-29-2010
Start the scripts in background and with "nohup". see the man page of "nohup" for details.

If you start them this they will no longer stop when the parent process is killed. This mechanism was originally for making sure a process would be persistent even if its terminal process got killed (hence the name: "no [termination upon] h[ang]up").

Btw., it might be a clever idea to control the processes in question with a PID file in such an environment: when starting them create a temporary file with the PID in it, which can be used to kill the process. This file will also be indicative of a process be running or not.

I hope this helps.

bakunin
# 3  
Old 03-29-2010
Thanks bakunin, Ill give nohup a shot. Smilie
# 4  
Old 03-29-2010
Quote:
Originally Posted by bakunin
Start the scripts in background and with "nohup". see the man page of "nohup" for details.

If you start them this they will no longer stop when the parent process is killed.
That probably won't help, unless the controlling terminal is lost or a SIGHUP is being sent with kill. I assume that he's using either ^C (SIGINT) at the keyboard or a typical kill command (SIGTERM); nohup will not prevent a process from receiving either of these.

Your pid file suggestion is a good one though. Something along the lines of:
Code:
$ cat a.sh
#!/bin/sh

echo $$ > a.pid
./b.sh &
#Do whatever script a is supposed to do


$ cat b.sh
#!/bin/sh

echo $$ > b.pid
./c.sh &
# Do whatever script b is supposed to do


$ cat c.sh
#!/bin/sh

# Do whatever script c is suposed to do
kill $(cat a.pid b.pid)
# Make needed changes
./a.sh &

Although any external executables running asynchronously at the time that the parent shell receives SIGTERM from kill will linger until they're done. If that's an issue, then a.sh would need to be started asynchronously and any signals sent to its process group (which b, c, a, b ... and further iterations in the cycle would inherit).

Regards,
Alister

Last edited by alister; 03-29-2010 at 05:32 PM..
# 5  
Old 03-29-2010
Quote:
Originally Posted by alister
That probably won't help, unless the controlling terminal is lost or a SIGHUP is being sent with kill.
Hmm, he said that he starts scriptA, which starts scriptB, which in turn starts scriptC and scriptC should kill A and B without being killed itself - exactly this is what nohup can do for him, no?

bakunin
# 6  
Old 03-29-2010
The death of a process, even a parent process, does not trigger the kernel to send SIGHUP to any processes. That only happens when the controlling terminal/pseudo terminal is hung up/closed. If SIGHUP is not the signal being sent, nohup will not prevent a process dying if that is the default action for whatever signal it is actually being sent (a SIGINT or SIGTERM will still kill a process, unless they're explicitly trapped by the receiving process).

Default behavior when a SIGHUP signal is sent to a process is for the kernel to terminate the process. nohup simply sets the SIGHUP signal to be ignored and then execs its argument (the command you want to run, which then inherits this signal handling modification from nohup ... all other singal handling is unmodified).

The heart of OpenBSD's nohup implementation:
Code:
	/*
	 * The nohup utility shall take the standard action for all signals
	 * except that SIGHUP shall be ignored.
	 */
	(void)signal(SIGHUP, SIG_IGN);

	execvp(argv[1], &argv[1]);

from src/usr.bin/nohup/nohup.c - view - 1.14

Regards,
Alister

Last edited by alister; 03-29-2010 at 07:00 PM..
# 7  
Old 03-29-2010
Quote:
Originally Posted by alister
The death of a process, even a parent process, does not trigger the kernel to send SIGHUP to any processes.
OK, now i understand, where you come from. We misunderstood each other, because we were talking about different things. In ksh it is standard that SIGHUP is sent to any child process in background once the father process gets killed. If script A calls script B by a line like "b.sh &" B will be terminated once A is killed. (I believe this is a main difference to the BASH shell, which handles the case differently.) Using a line like "nohup b.sh &" would prevent B from being killed if A is terminated.

Yes, this behavior is not necessarily the same for processes in general, as you have correctly pointed out. In (Korn) shell programming and for processes created by the ksh this is the case though.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

System should not kill the child process when parent id is 1

HI i would like to know how i can simulate a shell scripts for my requirement. example Server name child Process id Parent Process id Vpesh 16013 15637 Server name child Process id Parent Process id Vpesh 16014 15637 Server name child... (1 Reply)
Discussion started by: vpesh
1 Replies

2. Shell Programming and Scripting

forking a child process and kill its parent to show that child process has init() as its parent

Hi everyone i am very new to linux , working on bash shell. I am trying to solve the given problem 1. Create a process and then create children using fork 2. Check the Status of the application for successful running. 3. Kill all the process(threads) except parent and first child... (2 Replies)
Discussion started by: vizz_k
2 Replies

3. Emergency UNIX and Linux Support

signal between parent process and child process

Hello, everyone. Here's a program: pid_t pid = fork(); if (0 == pid) // child process { execvp ...; } I send a signal (such as SIGINT) to the parent process, the child process receive the signal as well as the parent process. However I don't want to child process to receive the... (7 Replies)
Discussion started by: jackliang
7 Replies

4. Programming

Parent process starts before the child using signal, in C

Hi, i want that the parent process start before the child, this code doesn't work, if the child start before the parent it wait for signal, then the father send the signal SIGALRM and the child catch it and call printf; else the father call printf and send the signal to the child that call its... (1 Reply)
Discussion started by: blob84
1 Replies

5. UNIX for Dummies Questions & Answers

Command to find parent and child process?

Hi, I have a script that calls other scripts/commands which may or may not spawn other process. From my understanding, when I do a ps -ef, the highest numbered process ID is supposed to be the parent ID of all the other related child processes, is this correct? In most or all... (3 Replies)
Discussion started by: newbie_01
3 Replies

6. Programming

IPC - pipes between parent and child process

Hi guys, I'm having some problem here, I'm studying pipes, and i want to create a shell in C and at this point a don't want to use semaphores, instead I want to use tricks. Straight to the doubt: I've a parent and a child process, and both of them has some code to execute, and the child process... (5 Replies)
Discussion started by: pharaoh
5 Replies

7. Shell Programming and Scripting

How to make the parent process to wait for the child process

Hi All, I have two ksh script. 1st script calls the 2nd script and the second script calls an 'C' program. I want 1st script to wait until the 'C' program completes. I cant able to get the process id for the 'C' program (child process) to make the 1st script to wait for the second... (7 Replies)
Discussion started by: sennidurai
7 Replies

8. Shell Programming and Scripting

starting a bash session as child process to another bash session from a process

Hi I want to do something that might sound strange. I have a code that in written in C and is executed at startup (it's a custom process). It occasionally calls some bash scripts. The process doesn't have any terminal associated with it. One thing I don't know how to do is to start a... (5 Replies)
Discussion started by: alirezan
5 Replies

9. UNIX for Advanced & Expert Users

How to find all the child processes of a parent process

Hi I am trying to see if there are some options in ps command or if there is a shell script which basically shows you all the processes spawned by a parent process , then all the processes of its child processes and so on down the hierarchy may be like a tree structure. It might be a generic... (6 Replies)
Discussion started by: clifford
6 Replies

10. Programming

parent and child process question?

Hi everybody, I'm trying to understand how a parent and child processes interact. This function( below) basically measures the fork time from the perspective of the parent only. what i would like to know is how to measure the time from the perspective of parent and child (ie: inserting... (0 Replies)
Discussion started by: tosa
0 Replies
Login or Register to Ask a Question