Solved

Trying to run a script permanently in the background

Posted on 2007-03-28
10
601 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Zimbra on Amazon Linux help 7 115
plsql job on oracle 18 79
sed/awk/tail: how to read 3'de last line 4 44
Shrink Linux Swap File Size CentOS 10 31
How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

730 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