Link to home
Start Free TrialLog in
Avatar of yosmc
yosmc

asked on

Trying to run a script permanently in the background

I'm having issues with a script on Redhat Linux I want to run permanently (it's a database monitoring script called mkill - http://mtop.sourceforge.net/mkill.html - but probably that's not even all too relevant). Anyway, when I run it in Putty (the Telnet replacement for SSH) it keeps running for ages. Only problem is, I can neither close the putty window nor shut down my computer, which of course I would be happy to do at some point.

I'm aware that, by adding a " &" at the end it'll be run in the background, so then I can close the terminal window, and it'll keep running. Unfortunately, for reasons that are beyond my technical knowledge, it won't keep running for more than a couple of hours. However when I run it without the " &" it'll run flawlessly for days.

In the end it seems like running a script in the background is a different cat than running it straight from the commandline, but like I said, that's beyond my technical knowledge. Any pointers how I can fix this issue would be highly appreciated.
Avatar of PhilHow
PhilHow

It is probably a configuration setting on the server that is causing the background jobs to be limited in duration.  IF you have control of the server, you should look at the docs ansett whether it is fixable.  

Failing that, or a s a workaround until you can change the configuaration; run a bash script using cron to fire up once a minute, and if your app is not running start it.

If you look at your mkill link you will see that there is a "--daemonize" option. Running mkill with that option should do what you want.
ASKER CERTIFIED SOLUTION
Avatar of Tintin
Tintin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Whoops, spoke to soon.  Didn't read about he --daemonize option for mkill.  So in fact, it was extremely relevant to mention the tool you were trying to run :-)
Avatar of yosmc

ASKER

I'm sorry, I should have mentioned that the --daemonize option doesn't exist (it's just a mockery kinda ;). I guess that was either planned for the next version but then development was abandoned, or it was a feature of the previous version but there was some problem with it so it got kicked out again. (Not sure if there's some other way to daemonize a process if the function isn't built-in.)

So since it seemed the easiest to test for now, I tried the nohup version, I'll keep you updated what happens.

PhilHow, there's one problem with the cron approach - the script doesn't terminate, it just hangs. That is, after a few hours it just quits doing its job, but I can still see it in the processlist. It is supposed to update the log file every 25 seconds though, so I guess it would be possible to check the "last changed" date of the logfile, kill off the process when the last update was longer than 30 seconds ago and then start the process anew. (Not that I would know how to code that, but at least that would probably be the way to go.)

I do have control of the server, so I can make any changed necessary, provided that I know *what* to change. ;) Not sure what you mean by "docs ansett" (is it a typo?) - please clarify.
Avatar of yosmc

ASKER

Update: Seems like I'm on the right track. After trying nohup and closing the putty window, the log file more or less exploded. Turns out that the script had a timed sleep loop that could be short-circuited via keystrokes to stdin, and closing the window made the script believe that a key was constantly pressed. So I commented that line out.

More in a day or two.
Avatar of yosmc

ASKER

Awesome, this is running just great now.

Small bonus question - if I put the command into a file so it'll run on bootup, do I stick with the "nohup" and the " &" or does it go without?
The nohup command is running commands in the background from your terminal session without them being terminated, ie: if you aren't starting a command from a terminal session, then you don't need it.
Avatar of yosmc

ASKER

Same goes for the " &" I guess?
Specifying & to put the process in the background depends a little on how the process behaves.  If it doesn't detach itself by becoming a daemon, then you'll need to leave it in.