Sponsored Content
Top Forums Shell Programming and Scripting Avoid running unnecessary repetitive ps command Post 303011083 by Don Cragun on Friday 12th of January 2018 04:01:49 PM
Old 01-12-2018
Quote:
Originally Posted by MadeInGermany
post#4 is unprecise (like post#1), for example will find pid 22 when searching for 2.
While the overall sense is not clear for me, this is certainly *not* intended.
Fix:
Code:
ps -ef | awk -vGPID="$gpid" '($2 == GPID || $3 == GPID)'

Doesn't give this the same result as the code in post#3 ?
Smilie
No. It doesn't give the same result as the code in post #3. With slightly modified versions of these scripts:
./tester:
Code:
#!/bin/ksh
gpid=${1:-$$}
ps -ef | tee ps.out | awk -v gpid="$gpid" '
{	line[NR] = $0
	pid[NR] = $2
	ppid[NR] = $3
}
$2 == gpid || $3 == gpid {
	PrintPid[$2]
	PrintPid[$3]
}
END {	for(i = 1; i <= NR; i++)
		if(pid[i] in PrintPid || ppid[i] in PrintPid)
			print line[i]
}'

Note that this captures the output from ps in ps.out which will be used as input for the other two scripts instead of rerunning ps. The above script produces the output:
Code:
  501   766   763   0  7Dec17 ttys003    0:01.40 -ksh
  501 27188   766   0 12:40PM ttys003    0:01.01 du /
  501 27189   766   0 12:40PM ttys003    0:00.01 /bin/ksh ./tester
    0 27190 27189   0 12:40PM ttys003    0:00.00 ps -ef
  501 27191 27189   0 12:40PM ttys003    0:00.00 tee ps.out
  501 27192 27189   0 12:40PM ttys003    0:00.00 awk -v gpid 27189

during a period where the command du / > du.out was running in the background while this script was running.

With this version of SkySmart's code modified to read from the ps.out produced by the above script:
./SkySmart:
Code:
#!/bin/ksh
gpid=${1:-$$}
APIDS=$(echo $(awk -v gpid="${gpid}" '$2 == gpid || $3 == gpid {print $2,$3}' ps.out) | sed 's~ ~|~g')
AllProcs=$(awk -v allpids="${APIDS}" '$2 ~ allpids || $3 ~ allpids {print $0}' ps.out | sed '/^$/d')
printf '%s\n' "$AllProcs"

and run with the operand 27189, we get the output:
Code:
  501   766   763   0  7Dec17 ttys003    0:01.40 -ksh
  501 27188   766   0 12:40PM ttys003    0:01.01 du /
  501 27189   766   0 12:40PM ttys003    0:00.01 /bin/ksh ./tester
    0 27190 27189   0 12:40PM ttys003    0:00.00 ps -ef
  501 27191 27189   0 12:40PM ttys003    0:00.00 tee ps.out
  501 27192 27189   0 12:40PM ttys003    0:00.00 awk -v gpid 27189

which is identical to the output produced by the code in post #3. But with a similarly modified version of MadeInGermany's code:
./MadeInGermany:
Code:
#!/bin/ksh
gpid=${1:-$$}
awk -v GPID="$gpid" '($2 == GPID || $3 == GPID)' ps.out

and run with the operand 27189, we only get the output:
Code:
  501 27189   766   0 12:40PM ttys003    0:00.01 /bin/ksh ./tester
    0 27190 27189   0 12:40PM ttys003    0:00.00 ps -ef
  501 27191 27189   0 12:40PM ttys003    0:00.00 tee ps.out
  501 27192 27189   0 12:40PM ttys003    0:00.00 awk -v gpid 27189

MadeInGermany's simpler code doesn't catch the parent, sibling, or grandchild processes of the process specified by $gpid. It only catches the process specified by $gpid and its children.

I do, however, agree that SkySmart's code works by accident when $gpid specifies a PID that isn't small enough to accidentally match several other unintended PIDs of unrelated running processes.
These 2 Users Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Can I avoid the standard output from kill command

I am sending a kill comand to kill a process inside a SH script but I don`t want the user to notice it so I donīt want the message "1222 killed" to appear. I`ve tried to redirect the standard output to /dev/null 2>&1 and also tried to use "nohup" but none of them was succesfull. Can anyone... (1 Reply)
Discussion started by: pguinal
1 Replies

2. IP Networking

unnecessary route entry?

Good day :) I recently checked some stuff on my gateway and discovered what I believe is an unneeded route entry. # route -n Kernel IP routing table Destination Gateway Genmask ... Iface 287.265.45.0 0.0.0.0 255.255.255.0 ... eth0 192.168.0.0 0.0.0.0 ... (3 Replies)
Discussion started by: J.P
3 Replies

3. UNIX for Dummies Questions & Answers

How to avoid historying my command

Dear all, Normally unix automatically record up to 500 the command lines whatever I put in. Does anyone knows how I can avoid this record, in another word, I dont want system remember what I typed in thanks (2 Replies)
Discussion started by: ting123
2 Replies

4. UNIX for Dummies Questions & Answers

Running UNIX commands remotely in Windows box from Unix box – avoid entering password

I am able to run the UNIX commands in a Windows box from a UNIX box through "SSH" functionality. But whenever the SSH connection is established between UNIX and Windows, password for windows box is being asked. Is there a way to avoid asking password whenever the SSH connection is made? Can I... (1 Reply)
Discussion started by: D.kalpana
1 Replies

5. Shell Programming and Scripting

awk command to avoid loops

Hi... I need a help in using the awk command or any other solution to avoid the usage of loops. My question is : I have a input like this : field1|field2|field3|field4|field5|field6|field7|field8|field9 ex : 4000|testing|scenario|14450|500|320|450|200|100 where the... (2 Replies)
Discussion started by: vijayarajvp
2 Replies

6. Shell Programming and Scripting

Avoid script running multiple times by filelock

Sometimes we need a single instance of a script to run at a time. Meaning, the script itself should detects whether any instances of himself are still running and act accordingly. When multiple instances of one script running, it’s easy to cause problems. I’ve ever seen that about 350 instances... (4 Replies)
Discussion started by: edenCC
4 Replies

7. UNIX for Dummies Questions & Answers

how to avoid time command output

Hi, I have 2 queries 1 .when I run some unix command, I am getting the output of "time" at std output (screen) for eg zegrep <pattern> *.v.gz I almost found the reason but not sure, if the no of files matching *.v.gz is more then I am getting the time command output at the... (5 Replies)
Discussion started by: selvaka
5 Replies

8. Shell Programming and Scripting

How to avoid running duplicated task?

Hi Gurus, I have requirement to run different task based on input value. the sample file as below: file1 (contains code need to be run) code aaa1 aaa2 bbb ccc ddd file2 (contains all codes and job name) code job1 job2 aaa1, job_aa1, job_a2 aaa2, job_aa2, job_a2 aaa3,... (5 Replies)
Discussion started by: ken6503
5 Replies

9. Shell Programming and Scripting

How to avoid error with ln command?

mkdir logs mkdir: Failed to make directory "logs"; File existsTo avoid this error i use the -p argument so it creates a folder only if it is does not exists like you see below. mkdir -p logs In the similar manner i wish to avoid this error with ln command ln -s /tmp/myfolder var ln: cannot... (4 Replies)
Discussion started by: mohtashims
4 Replies

10. UNIX for Beginners Questions & Answers

How to avoid arguments inside Nawk command?

Hi, Here is my command print $2 was meant to select the second column however, it is getting substituted with the second argument that was passed to the script. Can you please tell me how can I resolve this ? (6 Replies)
Discussion started by: mohtashims
6 Replies
All times are GMT -4. The time now is 01:34 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy