Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bash shell quandry 2

Status
Not open for further replies.

gmorton

Programmer
Dec 20, 2004
11
HK
Hi everyone

I've been trying all day to find out how to do the following and I'm having no luck. Any help would REALLY be appreciated.

I have a bash script that generates a log file, taking sar measurements at regular intervals in an endless loop. How can I assign a shortcut key that will call a function that I have written to do some cleen-up tasks before terminating. Control-c terminates my shell but, obviuosly, no clean-up is done. My function is quitter() and i've trieed the following, which doesn't work:

bind -x '"\C-q":quitter'

Thanks
Grant
 
Seem to remember there is a way that you can trap a signal in a bash script. So you should be able to set it running in the background, and then send it a hangup (kill -hup). When it gets the hangup signal, it can close gracefully.

Sadly, I can't remember how to do it...
 
You COULD have it check for the presence of a file as the trigger for it to stop running. This will come at some expense to the file system and may not be terribly desireable. Perhaps you could have it check once a minute? That way you could just "touch stopfile.txt" in some predetermined location - or you could delete the file and have the test condition be to stop if the file disappears.

You COULD also put a perl script wrapper around this with two system calls... I don't know the nature of your script, but you could have perl "system" the first script in the infinite loop and then if you ctrl-c that script then you SHOULD be able to return execution to perl, which could run another script for your cleanup.

A couple of ideas.

Hosting Solutions for Home or Business.
 
You may want to try and look at unix scripting forum... If that doesn't do it you can always try writing a small program to handle it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top