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 I run these command in a terminal it works, so what is the difference? Thank you |
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... |
Hup ?
|
Quote:
$ 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. |
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/ |
Quote:
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. |
Quote:
|
Quote:
Quote:
Quote:
|
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 & |
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. |