Operating System - HP-UX
1751975 Members
4418 Online
108784 Solutions
New Discussion юеВ

Re: how to kill all process by a script

 
Techsystemquery
Advisor

how to kill all process by a script

i can see

#ps -ef | grep compress.sh |wc -l
500

I want to kill all the 500 process and release memory in HP UX server
9 REPLIES 9
James R. Ferguson
Acclaimed Contributor

Re: how to kill all process by a script

Hi:

So you have 500 processes with the name 'compress.sh' and you want to kill all of them?

# PIDS=$(UNIX95= ps -C compress.sh -opid=)

# kill ${PIDS}

This will find only processes named 'compress.sh'; capture the corresponding pid in a list in the PIDS variable and kill those processes.

Note the use of 'UNIX95=' with whitespace before the 'ps' command and without any semicolon. This arms the UNIX95 (XPG4) behavior only for the duration of the command line.

Using the UNIX95 '-C ' allows exact matching to a process basename rather than doing fuzzy (and potentially disastrous) matching with 'grep'.

Regards!

...JRF...
Techsystemquery
Advisor

Re: how to kill all process by a script

how can i kill using ps command in a single line ?

can i use xargs kill -15 ?
James R. Ferguson
Acclaimed Contributor

Re: how to kill all process by a script

Hi (again):

> how can i kill using ps command in a single line ?

# kill $(UNIX95= ps -C compress.sh -opid=)

...or if you need:

# kill -9 $(UNIX95= ps -C compress.sh -opid=)

The advantage to first capturing the list and then issuing the 'kill' is that you can build the list once and apply successively harder hammers:

# kill -hup ${PIDS}
# kill -term ${PIDS}
# kill -9 ${PIDS}

Use the 'kill -9' only as a last resort.

Regards!

...JRF...
Steven E. Protter
Exalted Contributor

Re: how to kill all process by a script

Shalom,

You can use any switch you want, start with the default, and work your way up to -9.

Try 15 if you you want.

If the -9 tries to kill the init process, process ID 1, nothing bad should happen, the request should be ignored. On unstable systems, I've seem entire systems suddenly implode.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
James R. Ferguson
Acclaimed Contributor

Re: how to kill all process by a script

Hi:

> SEP: You can use any switch you want, start with the default, and work your way up to -9. Try 15 if you you want.

A 'kill' without a switch defaults to 'kill -15' which is a SIGTERM or 'kill -TERM'.

In the case of daemons, a standard practice is for the daemon to re-read its configuration file or otherwise re-initialize itself when it receives a HANGUP signal ('kill -HUP').

Some signals can be trapped, which means that the program receiving them has the ability to ignore or invoke a signal handler (a short block of code) to perform some specific action like removing temporary files before exiting.

A 'kill -KILL' ('kill -9') *cannot* be trapped. That is, there is nothing that a program can do to ignore it nor to invoke any signal handler. Using this capriciously may cause programs to leave temporary files lying about (or worse) leave shared memory segments orphaned and holding precious memory. Using a 'kill -9' with an 'fbackup' session represents a classic case.

A 'kill -0' ('kill -NULL') can be used to test a pid's validity,

> SEP: If the -9 tries to kill the init process, process ID 1, nothing bad should happen, the request should be ignored. On unstable systems, I've seem entire systems suddenly implode.

As far as I know, a 'kill -9 1' (which is an untrappable kill of the init daemon (pid=1)) is a "might-as-well-reboot". Daemons have 'init' as their parent process so terminating 'initd' terminates those.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: how to kill all process by a script

>how can I kill using ps command in a single line?

You shouldn't worry about this. Your bosses don't pay you by the line. :-)
If you're scripting, it is better to be more understandable.

>JRF: As far as I know, a "kill -9 1" is a "might-as-well-reboot".

No, kill(2) says it will NOT honor a SIGKILL for PID 1:
pid can equal 1 unless sig is SIGKILL or SIGSTOP.
So SEP was right about it being ignored.
Techsystemquery
Advisor

Re: how to kill all process by a script

Thanks a lot
Raj D.
Honored Contributor

Re: how to kill all process by a script

# kill -9 `ps -ef | grep compress.sh |awk '{print $2}' | xargs` #Cheers
" If u think u can , If u think u cannot , - You are always Right . "
James R. Ferguson
Acclaimed Contributor

Re: how to kill all process by a script

Hi (again):

> Dennis: kill(2) says it will NOT honor a SIGKILL for PID 1...
So SEP was right about it being ignored.

Ah, thank you for the correction. Yes, SEP was right. The manpages for kill(1) infer this but the manpages for kill(2) clearly state this as you noted. I stupidly failed to read the section-2 pages :-(

Regards!

...JRF...