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.
yosmcAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

0
veedarCommented:
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.
0
TintinCommented:
To run it in the background and be able to log off, you need to use nohup, eg:

nohup /path/to/mkill &
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

TintinCommented:
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 :-)
0
yosmcAuthor Commented:
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.
0
yosmcAuthor Commented:
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.
0
yosmcAuthor Commented:
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?
0
TintinCommented:
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.
0
yosmcAuthor Commented:
Same goes for the " &" I guess?
0
TintinCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.