LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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-16-2009, 01:18 PM   #1
mashiox
LQ Newbie
 
Registered: Oct 2008
Distribution: Slackware 13
Posts: 21

Rep: Reputation: 0
Catching SIG of running process with bash


Hello all!

I'm writing a script that will catch when an outside process crashes (SIGHUP, right?) without having to loop into infinity.
With that in mind, I came across the trap utility and thought if could be used to monitor another process other than it's own. But from what I've read, I'm thinking it might be more limited than what I initially believed.

Could someone point me in the right direction?

This is what I have on trap so far:
http://linux.die.net/man/1/trap
http://tldp.org/LDP/Bash-Beginners-G...ect_12_02.html
 
Old 11-16-2009, 02:00 PM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
hmm well.

I've played an awful lot with trap in the past.
I've used it so much that I rarely bother with it now.
it's ok for EXIT.

you can't catch a signal from another process - full stop.
they don't propogate.

the shell trap system is very very weak.
for instance they ain't inherited by functions in the same script.
remember shell scripting has limits.

if you look at waitpid(3) you'll see that there is a bitmask
which shows how the process exited. I'm not sure if this can be
examined in a shell with the $?.
on my system NOHUP produces 129, TERM = 127 so maybe it can.

the perl system allows you to query it.
or write a little C program to run it.

Last edited by bigearsbilly; 11-16-2009 at 02:12 PM.
 
Old 11-16-2009, 03:17 PM   #3
mashiox
LQ Newbie
 
Registered: Oct 2008
Distribution: Slackware 13
Posts: 21

Original Poster
Rep: Reputation: 0
So if I use waitpid, the process I'm watching out for crashes needs to be a child process of the script?
 
Old 11-16-2009, 05:26 PM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
no, waitpid is a C function.

and SIGHUP is not a crash.

what exactly are you trying to achieve?
 
Old 11-17-2009, 06:27 AM   #5
mashiox
LQ Newbie
 
Registered: Oct 2008
Distribution: Slackware 13
Posts: 21

Original Poster
Rep: Reputation: 0
Here it is:

This script will start by starting another process.
It will grab it's PID using pgrep
Then I want it to wait until the process it started at the beginning has crashed or not.
When it crashes, I want the script to restart the process.
Otherwise, keep waiting for it to crash.

Concise enough? Let me know if I got vague anywhere.
 
Old 11-17-2009, 06:39 AM   #6
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Hi

It could be done easily without pids, signals or traps. But you will have to figure out what is a crash and what is a normal exit. If the process writes some pid file or something you could maybe just check if it still exists?

Code:
while true
do
   start-process-command &
   wait
   if [ the-process-did-a-clean-exit ]
   then
       break
   fi
done
 
Old 11-17-2009, 08:25 AM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Why not just:

Code:
while !start-process-command 
do :
done
You said before
Quote:
Originally Posted by mashiox
without having to loop into infinity.
But I can't see why a loop is bad here. If by crash you mean only termination from SIGHUP, maybe what you want here is nohup?
 
Old 11-17-2009, 07:17 PM   #8
mashiox
LQ Newbie
 
Registered: Oct 2008
Distribution: Slackware 13
Posts: 21

Original Poster
Rep: Reputation: 0
We'll I'd be a fool not to toy around with what I've been given.
As far as loops go, I though if it was constantly checking for a contingency it would run the processor into the ground.

Thinking about it, that was silly to think that would be the case in this situation. But we'll see. I likely need to employ the rule of optimization more rather than trying to get it 100% right the first few time.

Also adderek on unix.com suggested I use fork to make the program a child process, and monitor it that way. Does fork have unintended consequences/issues?
 
Old 11-17-2009, 07:33 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
The processor thrashing issue depends on how soon after a failure you need(!) the program to restart.
If you can manage for a few minutes, then you could use cron to run a watchdog script that checks/restarts the program every n minutes.
 
Old 11-18-2009, 01:27 AM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by chrism01 View Post
The processor thrashing issue depends on how soon after a failure you need(!) the program to restart.
If you can manage for a few minutes, then you could use cron to run a watchdog script that checks/restarts the program every n minutes.
Or you could put a sleep in the loop.
 
Old 11-18-2009, 04:18 AM   #11
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
what is this vital program?

out of interest.
 
Old 11-18-2009, 10:04 PM   #12
mashiox
LQ Newbie
 
Registered: Oct 2008
Distribution: Slackware 13
Posts: 21

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by bigearsbilly View Post
what is this vital program?

out of interest.
An RPG game server.
Keep the daemon up, keep the players from squawking too loudly.
The whole concept for this I got from my father explaining to me his wastewater SCADA system he's been working on for the better part of a decade.
While he's not the developer, just the end-user and a wastewater supervisor at a small town's public works, the idea fit for UNIX system control.
And yes, I have heard of webmin. I just love my shell much much more.

But the bigger picture is eventually I'd like to code this in a C language, and go much larger and much more extensible. Checking the system for errors, anticipating and fixing problems, and communication to the sysop on duty. (I got sendmail working, yay!)
But for prototyping, I'll stick with bash. She's been good to me.
 
Old 11-19-2009, 10:54 AM   #13
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by ntubski View Post
Why not just:

Code:
while !start-process-command 
do :
done
You said before

But I can't see why a loop is bad here. If by crash you mean only termination from SIGHUP, maybe what you want here is nohup?
Maybe this, just to be safe:
Code:
while [[ -x "$( which "start-process-command" )" ]] && !start-process-command 
Kevin Barry
 
Old 11-30-2009, 11:13 PM   #14
mashiox
LQ Newbie
 
Registered: Oct 2008
Distribution: Slackware 13
Posts: 21

Original Poster
Rep: Reputation: 0
I did some digging and came up with this.

Code:
tcore=`pgrep process-name`

startcore() {
`/path/to/application`
} $tcore while [ -z "$tcore" ] do
startcore
done
Gonna play with some more functions...
 
  


Reply



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
[SOLVED] [bash]Pick up process running in back up by another terminal... RaptorX Linux - General 5 09-02-2009 01:29 PM
Bash script to check if process is running and send email depam Linux - Newbie 2 04-08-2009 12:11 AM
Obtain ip address and check for running process via Bash Script? xconspirisist Programming 10 09-12-2008 01:18 PM
ptrace(PTRACE_CONT, sig) kills app even if sig is blocked ocstjf Linux - Kernel 0 04-21-2008 03:17 PM
Bash signal catching meblost Programming 3 11-22-2004 07:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:39 AM.

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