LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 10-02-2009, 08:29 AM   #1
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 493

Rep: Reputation: 50
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
 
Old 10-02-2009, 09:32 AM   #2
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
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 &"
 
Old 10-02-2009, 09:49 AM   #3
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 493

Original Poster
Rep: Reputation: 50
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...
 
Old 10-05-2009, 02:47 AM   #4
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 493

Original Poster
Rep: Reputation: 50
Hup ?
 
Old 10-05-2009, 03:04 AM   #5
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by jf.argentino View Post
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.
 
Old 10-05-2009, 03:05 AM   #6
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
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/
 
Old 10-05-2009, 03:35 AM   #7
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 jf.argentino View Post
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"
 
Old 10-05-2009, 03:47 AM   #8
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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.
 
Old 10-05-2009, 05:30 AM   #9
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 konsolebox View Post
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.
 
Old 10-05-2009, 05:46 AM   #10
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by catkin View Post
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.
 
Old 10-06-2009, 01:41 AM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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 &
 
Old 10-06-2009, 07:02 AM   #12
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 493

Original Poster
Rep: Reputation: 50
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...
 
  


Reply

Tags
nohup, script



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
Nohup and disown usage raghu123 Programming 6 06-10-2009 04:51 AM
disown problem (sh: disown: not found) grayFalcon Linux - Server 2 01-07-2008 06:20 AM
problem with nohup command and nohup file size vbseeker Linux - General 1 09-17-2006 11:36 AM
How to create a bash script to automatically disown a process. jon_k Linux - Software 5 06-19-2005 05:53 AM
i need some script exmples using nohup ykirankumar Linux - Software 1 09-02-2004 05:00 PM

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

All times are GMT -5. The time now is 03:14 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