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
# 1  
Old 08-05-2012
Unix script seems to be momentarily creating child process for unknown reason

Hi,
I have a unix script that basically has a while loop inside which it checks Oracle database for certain records. If it finds the records, it does some processing and then goes back to the while loop. If it doesnot find any matching records, then it sleeps for 30 seconds and then goes back to the while loop to again check for existence of a new records.
We are supposed to have only one instance of this script running. The script is kicked off at the beginning of the week with the command sh scriptname.ksh &
It doesnot need to be started again until after server reboot the following week.
To ensure that not more than one instance of the script runs, we have a monitoring program that alerts us whenever multiple instances of the script are running due to either multiple users mistakenly starting a new instance of the script or some other reaosn. After seeing occassional alerts from this monitoring system that multiple instances of this script were running, I decided to see why the monitoring system was alerting us on multiple instances. So I wrote a temporary script that runs in an infinite loop and runs the command ps -ef|grep scriptname |grep -v grep every 2 secs. After analyzing the output of this test daemon that I wrote, I found that once in a while (eveyr 15 or 20 minutes), ps -ef returns two instances of the script.
(1) One instance has parent pid equal to 1
(2) The 2nd instance of this script has pid equal to the parent pid of the 1st process
Within a couple of seconds after that, my daemon shows that the 2nd instance has died and again there is only one instance.
So why does this 2nd instance get triggered by the parent process momentarily thereby misleading our monitoring system to believe that there are multiple instances of the script running.
Here is an output of the ps -ef run by my test daemon that runs every 2 seconds. As you can see from the output below, at 5:44:04 pm Aug 5, you can see 2 instances of the script running (in bold). 1st instance is parent script which has pid 12369. 2nd instance is Child script which has pid 5713 and whose parent pid is the pid of the parent script 12369. I have no idea how the child script with pid 5713 got triggered . myscriptname.ksh doesnot call itself. As mentioned before, it just runs in a while loop waiting for database changes and does some processing if there are database changes but it never calls itself anywhere. 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). This happens occassionally (maybe once every 15 minutes) but I donot know why. If you can explain the reason for this behavior of the parent process momentarily invoking a child process which calls the same script as the parent script and which dies within 2 secs, your help would be highly appreciated.
The script is running on SUSE Linux v10.3

Code:
Sun Aug  5 17:44:00 EDT 2012
ownerid     12359     1  0 04:10 ?        00:00:08 sh myscriptname.ksh
Sun Aug  5 17:44:02 EDT 2012
ownerid     12359     1  0 04:10 ?        00:00:08 sh myscriptname.ksh
Sun Aug  5 17:44:04 EDT 2012
ownerid      5713 12359  0 17:44 ?        00:00:00 sh myscriptname.ksh
ownerid     12359     1  0 04:10 ?        00:00:08 sh myscriptname.ksh
Sun Aug  5 17:44:06 EDT 2012
ownerid     12359     1  0 04:10 ?        00:00:08 sh myscriptname.ksh
Sun Aug  5 17:44:08 EDT 2012
ownerid     12359     1  0 04:10 ?        00:00:08 sh myscriptname.ksh

thanks

Moderator's Comments:
Mod Comment edit by bakunin: Please use code tags next time for your code and data.

Last edited by bakunin; 08-07-2012 at 04:54 PM..
# 2  
Old 08-05-2012
when you use a pipe | the shell creates a subshell.
maybe your script should touch a file in /tmp and refuse to run again if that file exists. or write out a pid file, which you can test with kill -0. using ps | grep for process management is, as you see, very inadequate
# 3  
Old 08-05-2012
Most likely, it's running a command in a subshell. If you want a more accurate assessment, post the code.

Regards,
Alister
# 4  
Old 08-06-2012
After timing the momentary occurrance of these multiple process instance, I localized this problem to one line in the script where it calls an Informatica ETL repository commands using Informatica's UNIX command-line utility PMCMD.
I have the following in my script.

Code:
while: 
do
script code before pmcmd command
............
............
informaticapath/pmcmd startworkflow -sv ETL_SERVICENAME -d ETL_DOMAINNAME -u ETL_USERID -p ETL_PASSWORD -f ETL_FOLDERNAME -wait ETL_WORKFLOWNAME

script code after the pmcmd command
...............
..............
done


To summarize what is going on above, each time the pmcmd command is called within the while loop, takes about 5 minutes to complete processing about 1000 messages. After it has completed it goes back to the while loop and if there are more new messages to be processed, gets invoked again and so on. Note that the -wait option in PMCMD tells pmcmd to return to the shell only after it completes executing the ETL workflow. So until pmcmd completes, the shell will not execute the subsequent line in the script.

What I noticed is everytime pmcmd finishes executing and returns control to the shell to execute the subsequent line in the shell script, briefly for a second I see 2 instances of this script running as shown in the sample below (a parent process which is the currently running script and a child process that momentarily appears and then vanishes). Within a second the child process that just appeared, vanishes.

Code:
ownerid 5713 12359 0 17:44 ? 00:00:00 sh myscriptname.ksh
ownerid 12359 1 0 04:10 ? 00:00:08 sh myscriptname.ksh

Based on the -wait option to the PMCMD command it returns control to the shell only after it has finished executing the ETL WORKFLOW specified in the pmcmd command. After completion, at the time of returning control to the shell, why is the child process momentarily created ? Is this done by pmcmd ? Any inputs would be appreciated.

thanks

Moderator's Comments:
Mod Comment edit by bakunin: same as above, please use CODE-tags

Last edited by bakunin; 08-07-2012 at 04:55 PM..
# 5  
Old 08-06-2012
I don't understand how you expect us to give you a clue, beyond what has already been said about subshells, when we haven't seen the code. Since it is a loop, the culprit could lurk before or after pmcmd.

Regards,
Alister
# 6  
Old 08-06-2012
I just removed everything before and after the pmcmd and this is all I have now. And it still spawns a new process momentarily for a second just after the pmcmd exits


Code:
#!/usr/bin/ksh
while:
do

informaticapath/pmcmd startworkflow -sv ETL_SERVICENAME -d ETL_DOMAINNAME -u ETL_USERID -p ETL_PASSWORD -f ETL_FOLDERNAME -wait ETL_WORKFLOWNAME

done


Last edited by bakunin; 08-07-2012 at 04:56 PM..
# 7  
Old 08-06-2012
PLease post what hardware you have (processors, memory etc.) and what Operating System you are running.
The ps command is unreliable (by design) on overloaded systems, but it usually misses processes or returns nothing rather than doubling up. We can't really comment withous seeing the script.

The construct ps -ef | grep scriptname | grep -v grep is horrendously inefficient and the most likely command to be rejected by the kernel on a heavily-loaded system.

Why not get the script to create a "PID file" just containing the PID of the script. Delete the file when the script ends. This is is technique used by many system scripts. You put the PID in the file in case you need to check whether that single PID is running (rather than trawling the entire process list). For general interlock, just check whether the file exists or not. Prevention is better than cure.
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