Kill a backend process

Hello,

I am trying to kill a backend process i know i can type a simple command
kill -9 pid and my process will be killed.

But the task is that there are two commands in the script which run the backend process and when that script is runned then we cannot find the PID of the two commands

So how can we kill them ....i Tried using 2> such that output on screen goes to the file name mentioned but the may be i m using a wrong syntex....

tail -f inputfile >> o/pfile & 2> pid

where pid is the file where i want the pid of this command such that i can kill the same....

Pls suggest ....

Thanks in advance
Aparna

Try to use killall command

Hello,

I think this will kill all the processes .

Regards,

Aparna

try

ptree <PPID>

& then kill PID's in first column.

regards
abhijeet

Hello Abhijeet,

Can u pls explain what " kill all in first coulmn symbolize......"

Regards,

Aparna

Try:

tail -f inputfile >>o/pfile  2>&1 &

Hi Abhijit,
Would you please explain what does ptree do.Its not found in the man pages.Is asks CORRECT>tree 23023 (y|n|e|a)? .

Regards,
Gideon.

ptree Print the process trees containing the specified pids
or users, with child processes indented from their
respective parent processes. An argument of all digits
is taken to be a process-id, otherwise it is assumed
to be a user login name. Default is all processes.

get the PPID of the script/process name & do ptree <PPID>
you will find the PID's of all processes (parent as well as child ).Do NOT stop the system daemons.

But the task is that there are two commands in the script which run the backend process and when that script is runned then we cannot find the PID of the two commands

So how can we kill them

Since there are two processes running in the script,they will have varying PID's ,so you try killing parent PID first & then the parent shell itself.

regards
abhijeet

Yes there is where the problem lies how to kil pid and ppid of two commands and that is why i was stucked up ... Pls provide some suggestions .....

Thanks
Aparna

Hello,

Still i am stuck up in this
even the command suggested
tail -f inputfile >>o/pfile 2>&1 &

is not working i mean i m not able to get the pid....

PLs suggest

Regards,
Aparna

Actually, redirecting the stderr of the command that you run is not going to help. What you might have to do is redirect the stderr of the shell that is executing the commands. I am not too sure about that though.

So here is a work-around.

#!/usr/bin/ksh

tail -f /var/adm/messages > /tmp/tmp.tmp &
tail -f /var/adm/messages > /tmp/tmp.tmp &
ps -ef|nawk -v awkpid=$$ '!/ps/&&!/nawk/ {if($3==awkpid) print $2 }'|while read pid; do
        ps -ef|grep $pid|grep -v grep 
done

Instead of the ps and grep commands inside the while loop, you can have the code that kills the processes or whatever.

Hello,

Thanks for the reply !!! The script wroked it gives me output of PID's.

but can u pls explain what the nawk is doing i mean i m new to nawk it would be a great knowledge sharing for me!!!

#!/usr/bin/ksh

tail -f /var/adm/messages > /tmp/tmp.tmp &
tail -f /var/adm/messages > /tmp/tmp.tmp &
ps -ef|nawk -v awkpid=$$ '!/ps/&&!/nawk/ {if($3==awkpid) print $2 }'|while read pid; do
ps -ef|grep $pid|grep -v grep
done

Regards,
Aparna

In nawk, using -v allows you to pass a shell variable to the awk command as an awk variable. I am passing the $$ (pid of the shell script in this case) as the awk variable.
Then inside the nawk, the !/ps/ and !/nawk/ make sure that the ps and the nawk commands do not match (it will not really matter if they do). The awk itself, checks for a process that has the process id of the shell script as its parent process id (the third field in the ps -ef command is the ppid). In this case, the two tail -f commands will show up.
Hope that helps.

Thanks a lot for the explanation

Regards,

Aparna