Solved

Trying to run a script permanently in the background

Posted on 2007-03-28
10
592 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Fine Tune your automatic Updates for Ubuntu / Debian
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.

911 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

21 Experts available now in Live!

Get 1:1 Help Now