Solved

Trying to run a script permanently in the background

Posted on 2007-03-28
10
588 Views
Last Modified: 2012-08-13
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.
0
Comment
Question by:yosmc
10 Comments
 
LVL 5

Expert Comment

by:PhilHow
ID: 18813074
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
 
LVL 15

Expert Comment

by:veedar
ID: 18813121
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
 
LVL 48

Accepted Solution

by:
Tintin earned 500 total points
ID: 18813257
To run it in the background and be able to log off, you need to use nohup, eg:

nohup /path/to/mkill &
0
 
LVL 48

Expert Comment

by:Tintin
ID: 18813258
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
 

Author Comment

by:yosmc
ID: 18814622
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:yosmc
ID: 18817728
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
 

Author Comment

by:yosmc
ID: 18831481
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
 
LVL 48

Expert Comment

by:Tintin
ID: 18833586
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
 

Author Comment

by:yosmc
ID: 18833620
Same goes for the " &" I guess?
0
 
LVL 48

Expert Comment

by:Tintin
ID: 18833668
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This is the error message I got (CODE) Error caused by incompatible libmp3lame 3.98-2 with ffmpeg I've googled this error message and found out sometimes it attaches this note "can be treated with downgrade libmp3lame to version 3.97 or 3.98" …
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now