Killing the Wrong Background Process


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Killing the Wrong Background Process
# 1  
Old 08-29-2009
Killing the Wrong Background Process

BASH on Solaris

Hi All,

I posted a problem whereby I was looking to Kill a background (calling Send)mail) process after a certain time had elapsed.

A User Scottn very kindly provided a useful function to do this as below


CheckAndKill()
{

sleep "$EMAIL_TIMEOUT_THEN_KILL"
[ "$(ps | awk '$1 == '$1)" ] && echo "Killing process $1" && kill -9 "$1"

}

Howvere I am calling this in the wrong way as below:

send_email "Warning Notification" $warning_msg" | tee -a "$LOG_PATH"/email_output.lst &
CheckAndKill $! &

When I check the shell output I can see that I am killing the wrong process ie I am killing the "tee" not the actual send_email function.

The $! passed into CheckAndKill() is clearly picking on the process id of the tee and not the send_email function
Any ideas how to correctly kill the send_email function?

Kind Regards
Satnam
# 2  
Old 08-29-2009
You could modify what you have to use pkill instead of kill so that you can kill a process by name rather than pid and pass it certain attributes (like user id or terminal id) to restrict which process it kills.

----------------------------------------------

If you continue to call it the way you are now, if you extract the PPID when calling ps, you can pkill with process name and PPID.

Last edited by Vi-Curious; 08-29-2009 at 10:15 AM.. Reason: Add PPID info
# 3  
Old 08-29-2009
Is there no way to make use of the existing ode and derive the correct process id before I pass it into ChekandKill()?

Kind regards
S
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Output when killing a background process

I played a bit around with the Terminal and I observed something. When I start and kill a background process, there is some kind of output. After I invoked the command to start the process the first message " 13063" is directly displayed. However, after killing the process, the second message "+... (3 Replies)
Discussion started by: Chuck Morris
3 Replies

2. Shell Programming and Scripting

Killing the process ID's

Hi , I have a list of application process id's. Is there a way to kill all the process listed below using the script, except the once which are starting with " Genesis " adm 1522 ABC_Process.tra adm 1939 Genesis_Process.tra adm 2729 Genesis_Archive.tra adm 3259 xyz_Process.tra (5 Replies)
Discussion started by: murali1687
5 Replies

3. Shell Programming and Scripting

How to put FTP process as a background process/job in perl?

Hi, I am using net::ftp for transferring files now i am trying in the same Linux server as a result ftp is very fast but if the server is other location (remote) then the file transferred will be time consuming. So i want try putting FTP part as a background process. I am unaware how to do... (5 Replies)
Discussion started by: vanitham
5 Replies

4. UNIX for Dummies Questions & Answers

Script to start background process and then kill process

What I need to learn is how to use a script that launches background processes, and then kills those processes as needed. The script successfully launches the script. But how do I check to see if the job exists before I kill it? I know my problem is mostly failure to understand parameter... (4 Replies)
Discussion started by: holocene
4 Replies

5. Shell Programming and Scripting

Finding the age of a unix process, killing old processes, killing zombie processes

I had issues with processes locking up. This script checks for processes and kills them if they are older than a certain time. Its uses some functions you'll need to define or remove, like slog() which I use for logging, and is_running() which checks if this script is already running so you can... (0 Replies)
Discussion started by: sukerman
0 Replies

6. Shell Programming and Scripting

Killing process!!!!

Hi friends, i m in big trouble.... i have one script which connects two server ...like below.. script1.sh ------------------------------------- bash test.sh & eval x=$@ export x=`echo $x` #echo $x # ssh user@8.2.5.6 bash /mbbv/location/script.sh $x|sed '/Binary file/d'... (1 Reply)
Discussion started by: Shahul
1 Replies

7. UNIX for Dummies Questions & Answers

killing the process

Hi, First, I am running a scipt.While the script is running I realize that I dont want the script to be run so I am killing the script externally.Before the process gets terminated or killed it should delete all the temporary files created by the script.How to do this?Can anyone help me? ... (3 Replies)
Discussion started by: arthi
3 Replies

8. Shell Programming and Scripting

Killing of a process and send a mail if the process doesnot come up within 2 minutes

Hi Friends, I am new to this forum as well as new to shell scripting. I have a problem here and i need someone to solve this. Let us consider there are two processes(abc & def).There is a script which kills these two processes(i.e killtheprocess abc). Here abc is the argument . There is a... (1 Reply)
Discussion started by: Prince89
1 Replies

9. UNIX for Dummies Questions & Answers

killing a process

I can kill running processes on my linux red hat system using ctrl-c but cannot do it from command line of another terminal using kill -2 pid. Although I can kill them from command line using kill -9 pid and other signals. I would like to do it using the kill -2 pid. Thanks for your suggestions (6 Replies)
Discussion started by: bbhayana
6 Replies

10. UNIX for Advanced & Expert Users

killing a process pid

What option is used with kill to cause the server to reread its config file. (16 Replies)
Discussion started by: jo calamine
16 Replies
Login or Register to Ask a Question