LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-20-2004, 05:12 AM   #1
shy
Member
 
Registered: Dec 2002
Location: Russia
Distribution: ASP linux
Posts: 94

Rep: Reputation: 15
Signal handling from bash (SOLVED)


Is there any possibility to catch a signal in bash script?
I would like to do something from dying script (when the system starts to reboot).

Last edited by shy; 12-10-2004 at 03:07 PM.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 11-20-2004, 07:52 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Re: Signal handling from bash

Quote:
Originally posted by shy
Is there any possibility to catch a signal in bash script?
I would like to do something from dying script (when the system starts to reboot).
Yes, that's possible.
What ever you want to do when receiving the TERM signal, make sure you do it quickly.
Code:
#!/bin/bash

on_die()
{
	# print message
	#
	echo "Dying..."

	# Need to exit the script explicitly when done.
	# Otherwise the script would live on, until system
	# realy goes down, and KILL signals are send.
	#
	exit 0
}


# Execute function on_die() receiving TERM signal
#
trap 'on_die' TERM

# Loop forever, reporting life each second
#
SEC=0
while true ; do
	sleep 1
	SEC=$((SEC+1))
	echo "I'm PID# $$, and I'm alive for $SEC seconds now!"
done

# We never get here.
exit 0
 
3 members found this post helpful.
Old 11-20-2004, 08:41 AM   #3
shy
Member
 
Registered: Dec 2002
Location: Russia
Distribution: ASP linux
Posts: 94

Original Poster
Rep: Reputation: 15
Thank you!
 
  


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
Threads in signal handling shilpig Programming 3 08-20-2009 03:14 AM
Signal handling......... rajsun Programming 2 06-28-2005 08:10 AM
how to detect that I'm handling signal igor.kopriva Programming 2 10-01-2004 05:46 AM
Signal Handling with LinuxThrreads TedMaul Programming 1 05-01-2004 01:56 PM
Signal handling Mohsen Programming 2 07-30-2003 06:55 AM


All times are GMT -5. The time now is 02:58 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration