will child processes be created when executing "ps"?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting will child processes be created when executing "ps"?
# 1  
Old 03-21-2011
Question will child processes be created when executing "ps"?

Hi

I'm trying to write some code to confirm there is only one running instance in memory like below:
Code:
/usr/ucb/ps  -auxww | egrep -v 'grep |vi |tail |more |cat ' | egrep ${SCRIPT_NAME} |  egrep -v " \-h| \-help| \-v"

But sometimes i found there is some child processes are are created as below:
Code:
 0     0 28796 27072  0   0 20 1456 1376          R pts/7     0:00 /usr/bin/ksh ./myprogram.ksh
 0     0 28829 28796  0   0 20 1448    ?          R pts/7     0:00 /usr/bin/ksh ./myprogram.ksh
 0     0 28831 28796  0   0 20 1448    ?          R pts/7     0:00 /usr/bin/ksh ./myprogram.ksh

pid28796 is the program i launched, and the lower two are forked as child processes. the number of child process is 2-4.

I definitely sure that there is no "fork" in my script, as such issue is there even if i only keep this command in it. And ptree can never show the detail about it

does anyone know why there are multiple instances?

thanks in adv.

Last edited by Scott; 03-21-2011 at 12:17 PM.. Reason: Code tags, please...
# 2  
Old 03-21-2011
Could you tell us a little more, such as which operating system and any more about the script you are look for. Different operaing systems have variations on the ps command and it would be worth seeing the script to look for anything that might cause child processes.

ps in itself should not cause your script to spawn processes, but it may find itself which can be random depending on how the process gets scheduled in by the OS e.g.
Code:
$ ps -ef|grep myscript
12321 12311 0 11:22:33 pts/0 0:01 /home/fred/myscript
12335 12311 0 11:22:33 pts/0 0:01 ps -ef|grep myscript

You can get out of this random catch by using square brackets to exploit the shell expansion:-
Code:
$ ps -ef|grep myscrip[t]
12321 12311 0 11:22:33 pts/0 0:01 /home/fred/myscript

This will never match itself, but will find all variations of the phrase myscrip followed by anything in the brackets, um, that's a t then.


Let us know and hopefully someone can find the issue.





Robin
Liverpool/Blackburn
UK
# 3  
Old 03-21-2011
rbatte1,

thanks for your reply.

I'm working on Solaris10 system. Below are the full program, which i removed other un-related part:

Code:
#!/usr/bin/ksh
SCRIPT_NAME="myprogram.ksh"
SCRIPT_PID=$$
ENV_DEFAULT_CFG=/opt/myprogram/env.cfg

    #Is another instance running
 echo $$
    IGNORE_OPTS=' \-ug| \-userguide| \-h| \-help| \-v | \-config'
    /usr/ucb/ps  -alxww | egrep -v 'grep |vi |tail |more |cat ' | egrep ${SCRIPT_NAME} | \
                  egrep -v "${IGNORE_OPTS}" |egrep -v ${SCRIPT_PID} | nawk '{print $0}'
				  
    if [[ -n "${pid}" && "${pid}" != "${SCRIPT_PID}" ]];then
        echo "pid=${pid}"
    fi
echo "finished\n"

Basically, i hope there is no output besides the "finished". But sometimes, about 1/4, there are other process found like below:

Code:
 0     0 23568 27072  0   0 20 1456 1360 60021f64340 S pts/7     0:00 /usr/bin/ksh ./myprogram.ksh
 0     0 23594 23568  0   0 20 1448    ?          R pts/7     0:00 /usr/bin/ksh ./myprogram.ksh
 0     0 23596 23568  0   0 20 1448    ?          R pts/7     0:00 /usr/bin/ksh ./myprogram.ksh
 0     0 23597 23568  0   0 20 1448    ?          R pts/7     0:00 /usr/bin/ksh ./myprogram.ksh

Please let me know there is any info needed.

Last edited by pludi; 03-22-2011 at 04:53 AM..
# 4  
Old 03-21-2011
Probably better off using a lockfile with the process ID in it.

Code:
LOCK=/var/run/myscript.pid
# Test if lock file already exists and
# Send process NULL signal to test if it's running
# if so exit - cause job is already running
[ -e $LOCK ] && kill -0 `cat $LOCK` 2> /dev/null && exit 2
 
# Cleanup lockfile on exit, interrupt, quit or terminate
trap "{ rm -f $LOCK ; exit ; }" 0 2 3 15
echo $$ > $LOCK
 
# Rest of your script here


Last edited by Chubler_XL; 03-21-2011 at 10:10 PM..
This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 03-22-2011
Chubler_XL, great thanks and your code will clear my issue.

Also, I still have question about my original issue. just can't explain it. Is there anyone can try it and give me some clue?

Anyway, i think i will try your code to finish my work first.:-)
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Expect: spawn id exp5 not open while executing "expect "$" { send "sudo su -\r" }"

Hi All, i am trying to ssh to a remote machine and execute certain command to remote machine through script. i am able to ssh but after its getting hung at the promt and after pressing ctrl +d i am gettin the out put as expect: spawn id exp5 not open while executing "expect "$" {... (3 Replies)
Discussion started by: Siddharth shivh
3 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Solaris

I attempt to know. Why has been created the "local"directory?

Hello guys. It is common knowledge that the directory /usr/openwin/lib contains standart “locale” directory which contains fonts for difference international locales. But I have found in /usr/openwin/lib one more directory with name «local». It contains the same iso_8859_1 directory with... (1 Reply)
Discussion started by: wolfgang
1 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. UNIX for Dummies Questions & Answers

Kill child processes, when parent is "bash"

Consider this simple command line bash -c 'echo $$ ; sleep 10000'This will print the newly created bash PID and sleep for a long time. If I go to another terminal and do something like ps -flax | grep leepI'll see something like 501 92418 91910 0 0:00.00 ttys000 0:00.00 bash -c echo $$... (5 Replies)
Discussion started by: teras
5 Replies

7. AIX

Aix5.3 "freeze" when mounting newly created fs

Hello, When I tried to mount /appli21 with smit, the terminal "freezed" (ssh connection still active but always showing the same window, no new telnel or ssh connection allowed) and I had to have the machine restarted to be able to connect again. my volum group datavg: VOLUME GROUP: datavg... (8 Replies)
Discussion started by: farmer
8 Replies

8. Programming

"No child processes" and waitpif

Hi everybody, i'm using a signal handler for the SIGCHLD signal. void InstallNewSigChldHandler() { struct sigaction act; struct sigaction oldAct; act.sa_handler = CallWaitChildProcess; sigemptyset(&act.sa_mask); act.sa_flags = SA_NOCLDSTOP; if... (5 Replies)
Discussion started by: Zipi
5 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question