LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to use nohup and disown in a script? (https://www.linuxquestions.org/questions/programming-9/how-to-use-nohup-and-disown-in-a-script-759197/)

jf.argentino 10-02-2009 08:29 AM

How to use nohup and disown in a script?
 
Hello,

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?

Thank you

JulianTosh 10-02-2009 09:32 AM

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 &"

jf.argentino 10-02-2009 09:49 AM

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...

jf.argentino 10-05-2009 02:47 AM

Hup ?

lutusp 10-05-2009 03:04 AM

Quote:

Originally Posted by jf.argentino (Post 3705070)
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.

JulianTosh 10-05-2009 03:05 AM

to keep your application running after you log off, you use the nohup command to start it. It can be used to log your output as well.

This link might be useful to start it as an actual daemon...

https://www.linuxquestions.org/quest...daemon-264970/

catkin 10-05-2009 03:35 AM

Quote:

Originally Posted by jf.argentino (Post 3705070)
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"

konsolebox 10-05-2009 03:47 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.

catkin 10-05-2009 05:30 AM

Quote:

Originally Posted by konsolebox (Post 3708105)
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.

konsolebox 10-05-2009 05:46 AM

Quote:

Originally Posted by catkin (Post 3708181)
I had to look it up but the %1 is correct as detailed in the GNU Bash Reference.

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.

chrism01 10-06-2009 01:41 AM

The classic way to watchdog an app is to put the watchdog in cron, set to run every N minutes eg 5.
You then start the app with

nohup /some_dir/app >/some_dir/app.log 2>&1 &

and you can then logout no problem.
Incidentally the paths can be different eg

nohup /home/app/app > /var/log/app/app.log 2>&1 &

jf.argentino 10-06-2009 07:02 AM

Hello everybody,
Thank you for your answers, I'm very busy for some days, I'll try all of these ASAP, and keep you in touch with the results...


All times are GMT -5. The time now is 06:50 PM.