Unix script seems to be momentarily creating child process for unknown reason


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Unix script seems to be momentarily creating child process for unknown reason
# 15  
Old 08-09-2012
Reference post #7.
Please post what Operating System and version you are running and what Shell is /bin/sh. Also please provide an overview of the hardware because we are still concerned that you are bombarding the kernel with ps -ef commands when you are only interested in a few processes. A unix kernel can and will ignore or not complete ps -ef commands when it has better things to do.

I can't see why you run shell scripts with sh myscriptname.ksh rather than making the script executable and running the script with an absolute pathname.

As @alister points out. If you are spawning a short-duration shell script a ps snapshot may not see it at all. Besides which @alister has explained sub-shells very well.
# 16  
Old 08-09-2012
I seriously hope you're doing all this digging for the sake of curiosity and education rather than trying this hard to get your process management script to work. The easy and correct method to solve this is using a PID file, as said posts ago.

There is still a difference. Originally you use backticks and a here-doc. Your test does not use a here-doc. I cannot explain how or when, but ksh does eliminate the need for some subshells. The last command in a pipeline comes in mind. Here is that example:

Code:
$ sh -c 'echo hello world | read var; echo "$var"'

$ ksh -c 'echo hello world | read var; echo "$var"'
hello world

Again ksh saves a subshell here:

Code:
$ sh -c 'var=`ps auxwf |grep [s]h`; echo "$var"'
mute     22212  0.0  0.4   4140  1160 pts/3    S+   01:47   0:00      \_ sh -c var=`ps auxwf |grep [s]h`; echo "$var"
mute     22213  0.0  0.2   4140   580 pts/3    S+   01:47   0:00          \_ sh -c var=`ps auxwf |grep [s]h`; echo "$var"
$ ksh -c 'var=`ps auxwf |grep k[s]h`; echo "$var"'
mute     22217  0.0  0.5   4576  1464 pts/3    S+   01:47   0:00      \_ ksh -c var=`ps auxwf |grep k[s]h`; echo "$var"

Trying with or without a here-doc is left as an exercise to the reader.

Oh but wait!! Your script ends with ksh, but you don't even use ksh to execute it... Smilie
# 17  
Old 08-10-2012
NutronScott you are absolutely right. This is more of a self education because now I know that I have to ask my team to invoke this script via nohup scriptname.ksh or ksh scriptname.ksh instead of sh scriptname.ksh. As you mentioned it was a wrong idea to invoke a ksh script with sh. But since this script is related to a corporate application that includes several informatica workflows, these trigger scripts are run by different teams across the world within the firm based on failures / maintenance releases that happen. As a result it is difficult control how different members of the team trigger these scripts. Anyway by digging deep into this issue in this forum, and based on what you have said, sh does invoke additional subshell compared to ksh and my tests with strace that alister suggested also revealed the same. BTW the reason I used HEREDOC in the first example is because I was using sqlplus and so it needs a delimiter to know when the oracle DML operation is ending. Whereas in the 2nd example there was no ORacle command involved and so I didnot need a HEREDOC. The version of Linux I am using is SuSE 10.3

For your info, here is an output from the strace command strace -e trace=process -f sh /tmp/testdbqueryforstrace.ksh
where testdbqueryforstrace.ksh is my sample script listed earlier that contains just an sqlplus command in backticks
As you can see first an execve is called to run sh /tmp/testdbquerforstrace.ksh. This happens even in ksh. So it is a common step
Then distinction with sh is in the next step where this process clones a child process (pid 9806) which is again sh /tmp/testdbqueryforstrace.ksh
This child process then again clones another child process (pid 9808) which is the sqlplus -s upon which parent process 9806 is suspended
When sqlplus command exits, 9808 is detached and 9806 resumes. When /tmp/testdbqueryforstrace.ksh exits 9806 is detached and its parent process sh /tmp/testdbqueryforstrace.ksh which is invoked through execve resumes.

Code:
execve("/bin/sh", ["sh", "/tmp/testdbqueryforstrace.ksh"], [/* 57 vars */]) = 0
arch_prctl(ARCH_SET_FS, 0x2ad59aa0bae0) = 0
clone(Process 9806 attached
child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x2ad59aa0bb70) = 9806
[pid  9806] clone(Process 9808 attached
child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x2ad59aa0bb70) = 9808
[pid  9806] wait4(-1, Process 9806 suspended
 <unfinished ...>
[pid  9808] execve("/app/oracle//bin/sqlplus", ["sqlplus", "-s", "ETL_RW/ETL_RW2011@PNY00600"], [/* 106 vars */]) = 0
[pid  9808] arch_prctl(ARCH_SET_FS, 0x2b803321bae0) = 0
[pid  9808] exit_group(0)               = ?
Process 9806 resumed
Process 9808 detached
[pid  9806] <... wait4 resumed> [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0, NULL) = 9808
[pid  9806] --- SIGCHLD (Child exited) @ 0 (0) ---
[pid  9806] wait4(-1, 0x7fffd99cb7c4, WNOHANG, NULL) = -1 ECHILD (No child processes)
[pid  9806] exit_group(0)               = ?
Process 9806 detached
--- SIGCHLD (Child exited) @ 0 (0) ---
wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WNOHANG, NULL) = 9806
wait4(-1, 0x7fffd99cb994, WNOHANG, NULL) = -1 ECHILD (No child processes)
exit_group(0)                           = ?

Then I ran the same strace command using ksh. Here is the ouput for strace command strace -e trace=process -f ksh /tmp/testdbqueryforstrace.ksh
As you can see the first step is the same as in using sh where the execve is invoked to spawn a new program ksh /tmp/testdbqueryforstrace.ksh
Distinction in the 2nd step when vfork is used instead of clone. And in addition to that difference, the vfork is used to spawn a child process (pid 5180) which is actually sqlplus -s command and not another ksh /tmp/testdbqueryforstrace.ksh. Its parent is directly the one created through execve (pid 5163). 5163 is suspended when sqlplus runs. When sqlplus ends, 5180 is detached and 5163 resumes.
When 5163 exits there is no more parent process waiting on it unlike in sh, because 5163 itself is the parent. So there is one level of less cloning here

Code:
execve("/usr/bin/ksh", ["ksh", "/tmp/testdbqueryforstrace.ksh"], [/* 57 vars */]) = 0
arch_prctl(ARCH_SET_FS, 0x2abb87300ae0) = 0
vfork(Process 5180 attached (waiting for parent)
Process 5180 resumed (parent 5163 ready)
)                                 = 5180
[pid  5163] wait4(-1, Process 5163 suspended
 <unfinished ...>
[pid  5180] execve("/app/oracle//bin/sqlplus", ["sqlplus", "-s", "ETL_RW/ETL_RW2011@PNY00600"], [/* 108 vars */]) = 0
[pid  5180] arch_prctl(ARCH_SET_FS, 0x2ae86ab5fae0) = 0
[pid  5180] exit_group(0)               = ?
Process 5163 resumed
Process 5180 detached
<... wait4 resumed> [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WSTOPPED|WCONTINUED, NULL) = 5180
--- SIGCHLD (Child exited) @ 0 (0) ---
wait4(-1, 0x7ffff8eae56c, WNOHANG|WSTOPPED|WCONTINUED, NULL) = -1 ECHILD (No child processes)
wait4(-1, 0x7ffff8eae56c, WNOHANG|WSTOPPED|WCONTINUED, NULL) = -1 ECHILD (No child processes)
exit_group(0)                           = ?

While the above test does show how an additional cloning happens in Linux when using sqlplus and invoking through sh, the gray area that still is when running strace on a 2nd example where the script calls another sleepscript in backticks instead of sqlplus, strace output showed even when invoking with sh, clone was called only once not twice. I guess I have to just live with that inconsistency in behaviour for now.

Last edited by Corona688; 08-10-2012 at 06:07 PM..
# 18  
Old 08-10-2012
It may be avoiding a clone or fork by using a shell builtin. This is not an "inconsistency" -- it is a performance gain.

What exactly must you 'live with'? In what way is the code not actually functioning?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Url check creating child process and generating false alerts

Hi All Below code is working as expected but creating too many child processes when the url is not up and every minute that process is sending false email alerts any help with the logic not to generate child process and not to send duplicate alerts app="https://url" appresult=$(wget... (2 Replies)
Discussion started by: srilinux09
2 Replies

2. Shell Programming and Scripting

SSH is failing due to unknown reason

Hi, I have setup keys between user1@server1 and user2@server2 however, the ssh is failing. server1 is Linux 3.10.0-514.6.2.el7.x86_64 #1 SMP whereas server2 is 5.10 Generic_150400-40 sun4v sparc sun4v I have checked port 22 to be open and keys to be correct. I also find the permissions... (3 Replies)
Discussion started by: mohtashims
3 Replies

3. Shell Programming and Scripting

Kill all child process of a script

Hi guys i have a problem with a script... this script creates differents GUI with YAD... well i want that when i press the "Cancel" button on this graphical interface all the child process and even the same script should be killed #!/bin/bash function gui_start { local choice="" ... (4 Replies)
Discussion started by: maaaaarco
4 Replies

4. 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

5. Shell Programming and Scripting

creating an automation process in unix .

hi i need shell script in ksh for the automation process in informtica. The automation process is like this . i have a folder in unix . when this folder gets updated (like if a file or files is/are added to the folder) an event in informatica is triggered and after the process is done in... (2 Replies)
Discussion started by: kumar8887
2 Replies

6. Shell Programming and Scripting

script to get child process for a process

!/bin/sh pid=$(ps -Aj | grep MSTRSvr | grep -v grep | awk '{print $1}') sid=$(ps -Aj | grep MSTRSvr | grep -v grep | awk '{print $3}') ps -s "$sid" I am not able to get the desired output it says process list error if i use watch ps -s "$sid" it considers only the first session id (5 Replies)
Discussion started by: schippada
5 Replies

7. Shell Programming and Scripting

Dropping Records for unknown reason in awk script

Hi, I have written the following it is pretty sloppy but I don't see any reason why I should be losing 54 records from a 3.5 million line file after using it. What I am doing: I have a 3.5 million record file with about 80,000 records need a correction. They are missing the last data from... (8 Replies)
Discussion started by: mkastin
8 Replies

8. UNIX for Dummies Questions & Answers

UNIX System Call for creating process

Hell Sir, This is chanikya Is there any System call which behaves just like fork but i dont want to return back two times to the calling func. In the following ex iam creating a child process in the called func but the ex prints two times IN MAIN. ex :- calling() { fork(); } ... (2 Replies)
Discussion started by: chanikya
2 Replies

9. Programming

creating child process

i want to create 3 child processes from the same parent using folk. I know how to use folk but my child processes did not come from the same parent. Any suggestion what i did wrong ? (12 Replies)
Discussion started by: Confuse
12 Replies

10. UNIX for Dummies Questions & Answers

Script to kill all child process for a given PID

Is there any build in command in unix to kill all the child process for a given process ID ? If any one has script or command, please let me know. Thanks Sanjay (4 Replies)
Discussion started by: sanjay92
4 Replies
Login or Register to Ask a Question