Avoid running unnecessary repetitive ps command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Avoid running unnecessary repetitive ps command
# 1  
Old 01-11-2018
Avoid running unnecessary repetitive ps command

i have the following code:

Code:
                APIDS=$(echo $(ps -ef | awk -v gpid="${gpid}" '$2 == gpid || $3 == gpid {print $2,$3}') | sed 's~ ~|~g')
                AllProcs=$(ps -ef | awk -v allpids="${APIDS}" '$2 ~ allpids || $3 ~ allpids {print $0}' | sed '/^$/d')

it seems the above APIDS variable was created only to gather IPs. and then it is fed to the real command in the AllProcs variable.

can this be shortened into one command and also in an efficient way?
# 2  
Old 01-11-2018
Quote:
Originally Posted by SkySmart
can this be shortened into one command and also in an efficient way?
At some point in this bla | foo | kitchen | sink-, ahem, -command i lost the ability to picture a possible outcome. Could you please explain what this supposed to achieve? Or let it run on the system it was designed for and show some sample outcome.

One point i immediately saw was that ps -ef | awk ... | sed ... must be nonsense because either use awk or use sed and furthermore probably both are superfluous because the output of ps cat be tailored using the -o option. See the man page for details.

I hope this helps.

bakunin
# 3  
Old 01-11-2018
Maybe something like this would work:
Code:
ps -ef | 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]
}'

but, of course, ps -ef output may vary somewhat from operating system to operating system and you haven't told us what OS you're using. And, if you're using a Solaris/SunOS system, you'll need to change awk to /usr/xpg4/bin/awk or nawk.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 01-12-2018
After sorting out some lome logical errors(e.g. the allpids matching), I guess you want a listing of the gpid process and its subprocesses. Try
Code:
ps -ef | awk -vGPID="$gpid" '$2 " " $3 ~ GPID'

This User Gave Thanks to RudiC For This Post:
# 5  
Old 01-12-2018
Quote:
Originally Posted by SkySmart
i have the following code:

Code:
                APIDS=$(echo $(ps -ef | awk -v gpid="${gpid}" '$2 == gpid || $3 == gpid {print $2,$3}') | sed 's~ ~|~g')
                AllProcs=$(ps -ef | awk -v allpids="${APIDS}" '$2 ~ allpids || $3 ~ allpids {print $0}' | sed '/^$/d')

it seems the above APIDS variable was created only to gather IPs. and then it is fed to the real command in the AllProcs variable.

can this be shortened into one command and also in an efficient way?
So it looks to me that you are trying to list all the processes with the PID or PPID of "${gpid}"

Consider this:
Code:
ps --ppid "${gpid}" -opid=

This will list the PIDs of all processes whose PPID is the gpid. If you have it,
Code:
pgrep -P${gpid} -d,

will do the same but give them in a comma-delimited list. So perhaps this?
Code:
ps -f --pid$(pgrep -P${gpid} -d,),${gpid}

If you don't have pgrep then you have to turn the output of my earlier ps command into a comma-delimited list and use that.

Caveat: GNU ps, GNU pgrep.

Andrew
This User Gave Thanks to apmcd47 For This Post:
# 6  
Old 01-12-2018
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
These 2 Users Gave Thanks to MadeInGermany For This Post:
# 7  
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:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
Login or Register to Ask a Question