LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-26-2008, 05:35 AM   #1
akhtar.bhat
Member
 
Registered: Jan 2008
Posts: 34

Rep: Reputation: 15
Question Kill a process


Dear All,

I have a script which after executing is not stoping when i press ctrl+c.
Now i want to Append the script in such a way when i press ctrl+c while execution of the program it should take it as arguements and should kill the script/running program forcefully.

I know the command to kill the the program Forcefully (kill -9 `ps -a | grep "ping_script" | awk '{print $1}'` )


Please help me to get out of this problem.
Advance Thanks,
Akhtar Bhat.
 
Old 12-28-2008, 08:26 AM   #2
hob
Senior Member
 
Registered: Mar 2004
Location: Wales, UK
Distribution: Debian, Ubuntu
Posts: 1,075

Rep: Reputation: 45
If this is a shell script, then you need to define the code that you want to run on exit as a function, and then set "trap" to catch the signals:

Quote:
# Function to exit the script if a termination signal is sent.
exit_withinterrupt() {
echo "ERROR: The script was canceled before completing."
}

# Set the script to catch Ctrl-C and other termination signals and run the exit function.
trap exit_withinterrupt SIGHUP SIGINT SIGQUIT SIGTERM
To handle crashes, use trap [function] ERR.

Hope that helps.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get the PID of the process giving kill signal to a process? hariprd Programming 2 11-27-2008 03:10 AM
Shell Script : Kill a running process when another process starts ashmew2 Linux - General 3 08-20-2008 03:47 AM
kill a process baddah Linux - General 3 12-02-2007 06:20 PM
cannot kill process (kill -9 does not work) mazer13a Linux - General 1 05-27-2005 02:32 PM
kill process dvong3 Linux - Newbie 8 07-08-2003 09:27 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration