Sponsored Content
Top Forums Shell Programming and Scripting Avoid running unnecessary repetitive ps command Post 303011015 by Don Cragun on Thursday 11th of January 2018 07:15:50 PM
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:
 

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
nohup(1)							   User Commands							  nohup(1)

NAME
nohup - run a command immune to hangups SYNOPSIS
/usr/bin/nohup command [argument...] /usr/bin/nohup -p [-Fa] pid [pid...] /usr/bin/nohup -g [-Fa] gpid [gpid...] /usr/xpg4/bin/nohup command [argument...] DESCRIPTION
The nohup utility invokes the named command with the arguments supplied. When the command is invoked, nohup arranges for the SIGHUP signal to be ignored by the process. When invoked with the -p or -g flags, nohup arranges for processes already running as identified by a list of process IDs or a list of process group IDs to become immune to hangups. The nohup utility can be used when it is known that command will take a long time to run and the user wants to log out of the terminal. When a shell exits, the system sends its children SIGHUP signals, which by default cause them to be killed. All stopped, running, and back- ground jobs will ignore SIGHUP and continue running, if their invocation is preceded by the nohup command or if the process programmati- cally has chosen to ignore SIGHUP. /usr/bin/nohup Processes run by /usr/bin/nohup are immune to SIGHUP (hangup) and SIGQUIT (quit) signals. /usr/bin/nohup -p [-Fa] Processes specified by ID are made immune to SIGHUP and SIGQUIT, and all output to the controlling terminal is redirected to nohup.out. If -F is specified, nohup will force control of each process. If -a is specified, nohup will change the signal disposition of SIGHUP and SIGQUIT even if the process has installed a handler for either signal. /usr/bin/nohup -g [-Fa] Every process in the same process group as the processes specified by ID are made immune to SIGHUP and SIGQUIT, and all output to the controlling terminal is redirected to nohup.out. If -F is specified, nohup will force control of each process. If -a is specified, nohup will change the signal disposition of SIGHUP and SIGQUIT even if the process has installed a handler for either signal. /usr/xpg4/bin/nohup Processes run by /usr/xpg4/bin/nohup are immune to SIGHUP. The nohup utility does not arrange to make processes immune to a SIGTERM (terminate) signal, so unless they arrange to be immune to SIGTERM or the shell makes them immune to SIGTERM, they will receive it. If nohup.out is not writable in the current directory, output is redirected to $HOME/nohup.out. If a file is created, the file will have read and write permission (600, see chmod(1)). If the standard error is a terminal, it is redirected to the standard output, oth- erwise it is not redirected. The priority of the process run by nohup is not altered. OPTIONS
The following options are supported: -a Always changes the signal disposition of target processes. This option is valid only when specified with -p or -g. -F Force. Grabs the target processes even if another process has control. This option is valid only when specified with -p or -g. -g Operates on a list of process groups. This option is not valid with -p. -p Operates on a list of processes. This option is not valid with -g. OPERANDS
The following operands are supported: pid A decimal process ID to be manipulated by nohup -p. pgid A decimal process group ID to be manipulated by nohup -g. command The name of a command that is to be invoked. If the command operand names any of the special shell_builtins(1) utilities, the results are undefined. argument Any string to be supplied as an argument when invoking the command operand. EXAMPLES
Example 1: Applying nohup to pipelines or command lists It is frequently desirable to apply nohup to pipelines or lists of commands. This can be done only by placing pipelines and command lists in a single file, called a shell script. One can then issue: example$ nohup sh file and the nohup applies to everything in file. If the shell script file is to be executed often, then the need to type sh can be eliminated by giving file execute permission. Add an ampersand and the contents of file are run in the background with interrupts also ignored (see sh(1)): example$ nohup file & Example 2: Applying nohup -p to a process example$ long_running_command & example$ nohup -p `pgrep long_running_command` Example 3: Applying nohup -g to a process group example$ make & example$ ps -o sid -p $$ SID 81079 example$ nohup -g `pgrep -s 81079 make` ENVIRONMENT VARIABLES
See environ(5) for descriptions of the following environment variables that affect the execution of nohup: LANG, LC_ALL, LC_CTYPE, LC_MES- SAGES, PATH, NLSPATH, and PATH. HOME Determine the path name of the user's home directory: if the output file nohup.out cannot be created in the current directory, the nohup command will use the directory named by HOME to create the file. EXIT STATUS
The following exit values are returned: 126 command was found but could not be invoked. 127 An error occurred in nohup, or command could not be found Otherwise, the exit values of nohup will be those of the command operand. FILES
nohup.out The output file of the nohup execution if standard output is a terminal and if the current directory is writable. $HOME/nohup.out The output file of the nohup execution if standard output is a terminal and if the current directory is not writable. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: /usr/bin/nohup +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ |CSI |enabled | +-----------------------------+-----------------------------+ /usr/xpg4/bin/nohup +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWxcu4 | +-----------------------------+-----------------------------+ |CSI |enabled | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ SEE ALSO
batch(1), chmod(1), csh(1), ksh(1), nice(1), pgrep(1), proc(1), ps(1), sh(1), shell_builtins(1), signal(3C), proc(4), attributes(5), envi- ron(5), standards(5) WARNINGS
If you are running the Korn shell (ksh(1)) as your login shell, and have nohup'ed jobs running when you attempt to log out, you will be warned with the message: You have jobs running. You will then need to log out a second time to actually log out. However, your background jobs will continue to run. NOTES
The C-shell (csh(1)) has a built-in command nohup that provides immunity from SIGHUP, but does not redirect output to nohup.out. Commands executed with `&' are automatically immune to HUP signals while in the background. nohup does not recognize command sequences. In the case of the following command, example$ nohup command1; command2 the nohup utility applies only to command1. The command, example$ nohup (command1; command2) is syntactically incorrect. SunOS 5.10 16 Nov 2001 nohup(1)
All times are GMT -4. The time now is 08:57 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy