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
# 8  
Old 08-06-2012
Sorry, posts crossed and I now see that this is Informatica which is one of the most complicated packages known to man.

Providing so little detail about your environment and the real script is frustrating.


Quote:
test daemon that runs every 2 seconds.
What command do you type (or what crontab entry?) do you use to run this script? Exact commands please.



Quote:
AS you can see below, within 2 secs at 5:44:06 PM, the child instance of the script with pid 5713 suddenly dies and we are back to just 1 instance again (pid 12369).
I do not see. There is no evidence posted which supports any part of this statement.

Last edited by methyl; 08-06-2012 at 06:10 PM..
# 9  
Old 08-06-2012
To run an external command, such as pmcmd, the shell must fork(), temporarily creating a copy of itself. This copy is a child whose parent is the original shell. Then the child execs, loading the new executable into its process.

Regards,
Alister

Last edited by alister; 08-06-2012 at 10:30 PM..
# 10  
Old 08-09-2012
Hi Allister, thanks for your reply. However upon further close investigation I noticed that the problem is not with pmcmd but with SQLPLUS command that I am calling within the script using backtick.

I broke down my script further and localized the problem to the oracle 'sqlplus' call I am making inside the script to run SQL queries. Sqlplus is Oracle's commandline tool for running database queries. Here is a short script I wrote to simulate the issue.
As you can see, the script does nothing much other than run in a loop. Within that loop it sleeps for 5 seconds and then calls a simple SQL DML operation through the Oracle Sqlplus utility with backticks. It also prints some messages to a temp file specifically for better understanding of the sequence of steps run.

Code:
#!/usr/bin/ksh
. /app/informatica/data/IB_PTT_CDDS_PROD01/Scripts/setCDDS_ETL.env
while :
do
        echo "Main script Started sleeping..........."  >>/tmp/checktestdbremove.out
        sleep 5
        echo "Main script Finished sleeping..........." >>/tmp/checktestdbremove.out
        echo "Main started caling sqlplus" >>/tmp/checktestdbremove.out
        myvar=`sqlplus -s ETL_RW/ETL_RW2011@PNY00600<<EOF
        select count(*) from tablename;
        exit;
EOF`
        echo "Main finished calling sqlplus myvar=$myvar" >>/tmp/checktestdbremove.out
done
exit 0

Here is another script that runs in a loop, which I wrote just to track the processes running as the first script is runnning. It continuously looks for process testdbqueryforum.ksh usingg ps -ef output and prints it

Code:
#!/usr/bin/ksh
>/tmp/checktestdbremove.out
while :
do
date >>/tmp/checktestdbremove.out
ps -ef|grep testdbqueryforum.ksh|grep -v grep >>/tmp/checktestdbremove.out
done

Here is an extract of the output file written by the 1st and 2nd script above which gives an insight to the issue I want to understand about.
As you can see from the output pasted further below following is the sequence of events:
(1) I invoke the main script testdbqueryforum.ksh using the command sh /tmp/testdbqueryforum.ksh &(2) The main script sleeps for 5 seconds. At this time there is just one process called testdbqueryforum.ksh running with pid 9847
(3) After 5 seconds main script finishes sleeping and calls sqlplus command
(4) As soon as the sqlplus is invoked by the main script, we see 2 processes running with the name testdbqueryforum.ksh. One is pid 9847 and another is child process 10991. 10991 in turn calls another process sqlplus -s which I have not captured in this output but tracked offline
(5) Once the sqlplus process finishes running the SQL query, it returns the sql output to the main process which prints "Main finished calling sqlplus myvar= 14754"
(6) After this main script again goes to sleeping for 5 seconds and again you notice we are back to only one process with name testdbqueryforum.ksh running with pid 9847


Log output


Code:
Main script Started sleeping...........
Thu Aug  9 12:40:29 EDT 2012
cdds      9847 19491  0 12:39 pts/11   00:00:00 sh /tmp/testdbqueryforum.ksh
Thu Aug  9 12:40:29 EDT 2012
cdds      9847 19491  0 12:39 pts/11   00:00:00 sh /tmp/testdbqueryforum.ksh
Thu Aug  9 12:40:29 EDT 2012
cdds      9847 19491  0 12:39 pts/11   00:00:00 sh /tmp/testdbqueryforum.ksh
----
-----
-----
Thu Aug  9 12:40:34 EDT 2012
cdds      9847 19491  0 12:39 pts/11   00:00:00 sh /tmp/testdbqueryforum.ksh
Thu Aug  9 12:40:34 EDT 2012
cdds      9847 19491  0 12:39 pts/11   00:00:00 sh /tmp/testdbqueryforum.ksh
Thu Aug  9 12:40:34 EDT 2012
Main script Finished sleeping...........
Main started caling sqlplus
cdds      9847 19491  0 12:39 pts/11   00:00:00 sh /tmp/testdbqueryforum.ksh
cdds     10991  9847  0 12:40 pts/11   00:00:00 sh /tmp/testdbqueryforum.kshThu Aug  9 12:40:34 EDT 2012
cdds      9847 19491  0 12:39 pts/11   00:00:00 sh /tmp/testdbqueryforum.ksh
cdds     10991  9847  0 12:40 pts/11   00:00:00 sh /tmp/testdbqueryforum.ksh
Thu Aug  9 12:40:34 EDT 2012
cdds      9847 19491  0 12:39 pts/11   00:00:00 sh /tmp/testdbqueryforum.ksh
cdds     10991  9847  0 12:40 pts/11   00:00:00 sh /tmp/testdbqueryforum.ksh
Thu Aug  9 12:40:34 EDT 2012
Main finished calling sqlplus myvar=    14754
Main script Started sleeping...........
Thu Aug  9 12:40:35 EDT 2012
cdds      9847 19491  0 12:39 pts/11   00:00:00 sh /tmp/testdbqueryforum.ksh
Thu Aug  9 12:40:35 EDT 2012
cdds      9847 19491  0 12:39 pts/11   00:00:00 sh /tmp/testdbqueryforum.ksh
Thu Aug  9 12:40:35 EDT 2012
----

----
----

I am perfectly fine with ps command and so that is not an issue. What I primarily interesting in knowing are the underlying system calls that the OS is making which is resulting in a child process with the same name as the parent process and then how come it is alive until the SQLPLUS command finishes executing and any other clarification on this behaviour.


thanks
Moderator's Comments:
Mod Comment Please view this code tag video for how to use code tags when posting code and data.
# 11  
Old 08-09-2012
That's probably caused by the backticks and not a cause for concern. It runs another shell to run sqlplus inside.

Here's a tip for avoiding 'grep -v grep' -- make the pattern so it doesn't match itself.

Code:
grep "[t]estdbqueryforum.ksh"

It doesn't change the meaning of the regex, but it will no longer match itself.
# 12  
Old 08-09-2012
Quote:
Originally Posted by waavman
Hi Allister, thanks for your reply. However upon further close investigation I noticed that the problem is not with pmcmd but with SQLPLUS command that I am calling within the script using backtick.
From post #3
Quote:
Originally Posted by alister
Most likely, it's running a command in a subshell.
As Corona688 mentioned, what you're seeing is not a cause for concern.


Quote:
Originally Posted by waavman
I am perfectly fine with ps command and so that is not an issue. What I primarily interesting in knowing are the underlying system calls that the OS is making which is resulting in a child process with the same name as the parent process and then how come it is alive until the SQLPLUS command finishes executing and any other clarification on this behaviour.
Command substitution, `cmd ...` and $(cmd ...), works by creating a subshell to run commands. That subshell, which is initially a copy of its parent and shares the same name in the ps process list, will exist until its commands have completed. Why? Because that's how the shell works. It waits for synchronous (not in the background) commands to terminate before it itself exits.

How is the subshell created? UNIX process creation uses fork (or some optimized variant) to create a new process. This process begins life as an identical copy of its parent. For that process to become a different program, to load a new executable image, it needs to then call one of the exec family syscalls (usually execve).

On linux, the fork-variant used by the shell is probably clone.

If you're interested in monitoring system call activity, you might want to look into strace. It usually generates a lot of output. You can focus on the process creation system calls with -e trace=process. If you want the trace to follow the syscall activity of child processes (like subshells), use -f.

I rarely use Linux, so perhaps there are better monitoring tools these days. strace gets the job done for my simple needs.

Regards,
Alister

Last edited by alister; 08-09-2012 at 03:26 PM..
# 13  
Old 08-09-2012
Thanks for the explanation.
However to confirm that this behaviour is identical when I call any other program from within the script using backtick, I replaced the call to SQLPLUS with a call to a simple shell script that just sleeps for 2 secs and then prints hello. Surprisingly now, despite calling the shell script from within testdbqueryforum.ksh using backtick, I donot see two instances of the script (parent and child) when the 2nd process is spawned like how we noticed when invoking SQLPLUS with backticks. Do you know why there would be this inconsistency in behaviour.

Here is my modified testdbqueryforum.ksh script

Code:
#!/usr/bin/ksh
. /app/informatica/data/IB_PTT_CDDS_PROD01/Scripts/setCDDS_ETL.env
while :
do
        echo "Main script Started sleeping..........."  >>/tmp/checktestdbremove.out
        sleep 5
        echo "Main script Finished sleeping..........." >>/tmp/checktestdbremove.out
        echo "Main Started calling justsleepscript" >>/tmp/checktestdbremove.out
        justsleepretval=`/tmp/justsleepcommandremove.ksh`        echo "Main Finished calling justsleepscript returnvalue=$justsleepretval" >>/tmp/checktestdbremove.out
done


This is what the script justsleepcommandremove.ksh contains

Code:
#!/usr/bin/ksh
sleep 2
echo "hello"

Here is the output from the log when running the 1st script using sh /tmp/testdbqueryforum.ksh &.
As you can see from the output below there are no two processes with the name /tmp/testdbqueryforum.ksh when the sleep script (instead of SQLPLUS) is invoked from within the main script using backticks
Code:
Main script Started sleeping...........
cdds       567 19491  0 17:25 pts/11   00:00:00 sh /tmp/testdbqueryforum.ksh
Thu Aug  9 17:25:25 EDT 2012
cdds       567 19491  0 17:25 pts/11   00:00:00 sh /tmp/testdbqueryforum.ksh
Thu Aug  9 17:25:25 EDT 2012
cdds       567 19491  0 17:25 pts/11   00:00:00 sh /tmp/testdbqueryforum.ksh
Thu Aug  9 17:25:25 EDT 2012
cdds       567 19491  0 17:25 pts/11   00:00:00 sh /tmp/testdbqueryforum.ksh
-----
----
-----
Thu Aug  9 17:25:30 EDT 2012
cdds       567 19491  0 17:25 pts/11   00:00:00 sh /tmp/testdbqueryforum.ksh
Thu Aug  9 17:25:30 EDT 2012
cdds       567 19491  0 17:25 pts/11   00:00:00 sh /tmp/testdbqueryforum.ksh
Thu Aug  9 17:25:30 EDT 2012
Main script Finished sleeping...........
Main Started calling justsleepscript
cdds       567 19491  0 17:25 pts/11   00:00:00 sh /tmp/testdbqueryforum.ksh
Thu Aug  9 17:25:30 EDT 2012
cdds       567 19491  0 17:25 pts/11   00:00:00 sh /tmp/testdbqueryforum.ksh
Thu Aug  9 17:25:30 EDT 2012
cdds       567 19491  0 17:25 pts/11   00:00:00 sh /tmp/testdbqueryforum.ksh-----
-----
-----
Thu Aug  9 17:25:32 EDT 2012
cdds       567 19491  0 17:25 pts/11   00:00:00 sh /tmp/testdbqueryforum.ksh
Thu Aug  9 17:25:32 EDT 2012
Main Finished calling justsleepscript returnvalue=hello
Main script Started sleeping...........
cdds       567 19491  0 17:25 pts/11   00:00:00 sh /tmp/testdbqueryforum.ksh
Thu Aug  9 17:25:32 EDT 2012
cdds       567 19491  0 17:25 pts/11   00:00:00 sh /tmp/testdbqueryforum.ksh
Thu Aug  9 17:25:32 EDT 2012
------
----
---

Your inputs on the reason for inconsistency in behaviour when invoking another shell script instead of sqlplus using backticks would be very much appreciated.

thanks

Last edited by methyl; 08-11-2012 at 08:26 PM.. Reason: Please use code tags ; Correct spelling mistakes for readabilty
# 14  
Old 08-09-2012
Just because ps never saw it doesn't mean that it did not occur. Use strace to be sure. And for the definitive answer, look at your shell's source code to learn exactly how it implements command substitution.

Regards,
Alister
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