ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I try to make a "watchdog" in bash which monitors an application and restart it if it doesn't run. For this I'm using nohup and disown to detach the application from script but it looks like I don't understand how to use these commands.
Following is my script source:
Code:
#!/bin/bash
if [ -z $1 ]; then
echo "$0" APPLICATION
exit
fi
APPLICATION=$1
if [ ! -x "$APPLICATION" ]; then
echo "$APPLICATION" is not a valid application
exit
fi
WATCH_PERIOD_S=1
while [ 1 ]; do
IS_ALIVE=`ps aux | grep "$APPLICATION" | grep -v "$0" | grep -v "grep"`
if [ -z "$IS_ALIVE" ]; then
echo "$APPLICATION not running, start it"
nohup $APPLICATION &
disown -h %1
fi
sleep $WATCH_PERIOD_S
done
"pstree" show me that the application is still a child of the script file. But
if I run these command in a terminal it works, so what is the difference?
I dont think you need nohup for what you're doing. Simply appending & to the command will accomplish what you're trying to do.
disown simply removes the job from the joblist.
For instance, if you start some tasks like "gedit &" and "seahorse &"
Then run "jobs", you'll see both of them listed. Disown gedit and run jobs again, you'll only see seahorse running, but the gedit app is still visible on the desktop, just not as a controllable job.
In your IS_ALIVE assignment, doing it this way:
IS_ALIVE=$(jobs -p $APPLICATION)
If you get "bash: jobs: <app name>: no such job" then the job has died/completed. If you get a PID, it's still running.
Don't disown it, or the jobs command will not be able to see it anymore. remove the nohup command and simply call "$APPLICATION &"
I just see that I didn't explain what's wrong... My problem is that if I'm stopping the watchdog script, the monitored application stop too.
I'm using "ps" instead of "jobs" for the detection because the application won't be started by the script.
Since I want that the application continue to run even if I'm stopping the script (or if I'm logging out) -> that's why I'm trying to use "nohup" and "disown".
What's disturbing me is that this scheme doesn't work embedded in the script, but if I do the same couple of command directly on a terminal, it works...
I just see that I didn't explain what's wrong... My problem is that if I'm stopping the watchdog script, the monitored application stop too.
I'm using "ps" instead of "jobs" for the detection because the application won't be started by the script.
Since I want that the application continue to run even if I'm stopping the script (or if I'm logging out) -> that's why I'm trying to use "nohup" and "disown".
What's disturbing me is that this scheme doesn't work embedded in the script, but if I do the same couple of command directly on a terminal, it works...
Try implementing this method in a script:
$ nohup (command) > /dev/null 2>&1 &
This works because it captures any outputs (to stdout and stderr) the script might create. This stepis required to disconnect it from its parent.
What's disturbing me is that this scheme doesn't work embedded in the script, but if I do the same couple of command directly on a terminal, it works...
That disturbs me too. AIUI, the nohup does not result in the nohupped process being re-parented to the init process until it loses its "controlling terminal". That is when the terminal is closed.
Can you post what you see that makes you conclude that "if I do the same couple of command directly on a terminal, it works"
hmm.. i don't know this. what does %1 in 'disown -h %1' do? shouldn't that be 'disown -h $!' ?
edit: if removing hup on a child process does not make expected results, try to completely disown it by not including the -h option: 'disown $!'
edit: probably got it.. in disown -h $!, you're probably referring to the nohup command and not the command that was called by nohup.. try not to use nohup.
Last edited by konsolebox; 10-05-2009 at 03:50 AM.
hmm.. i don't know this. what does %1 in 'disown -h %1' do? shouldn't that be 'disown -h $!' ?
edit: if removing hup on a child process does not make expected results, try to completely disown it by not including the -h option: 'disown $!'
edit: probably got it.. in disown -h $!, you're probably referring to the nohup command and not the command that was called by nohup.. try not to use nohup.
I had to look it up but the %1 is correct as detailed in the GNU Bash Reference. OTOH I don't think the disown is necessary to achieve the OP's objectives; AFAIK the disown is harmless but useless.
I might have read that before already but i don't use it so perhaps i just don't remember. $! was so much enough already and is also used by default in most shells.
Quote:
OTOH I don't think the disown is necessary to achieve the OP's objectives; AFAIK the disown is harmless but useless.
The OP said
Quote:
I just see that I didn't explain what's wrong... My problem is that if I'm stopping the watchdog script, the monitored application stop too.
and it's the only thing I know so that the subprocesses won't be included from stopping like for example, when the parent process is sent with a SIGKILL or SIGTERM signal.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.