PDA

View Full Version : Keep a shell script running after disconnect



phatfish
03-21-2010, 10:19 PM
Hi there,

I have a simple shell script that I want to keep running indefinitely without leaving a putty window open. I've tried several different methods (non-specific to bluehost) and have had mixed results.

Both when I use nohup or trying to double fork the process into the background the script seems to run for a while (perhaps a min or two) after I close my putty session and then mysteriously stops (I've been using ps -u to check this). These are the commands I've been using for that:

nohup script.sh &
sh -c script.sh >/dev/null 2>&1 &"

Either one of these works fine as long as I leave my putty window open.

The method I've had the best luck has been by using the screen command. With this my script will run for a number of days before the process is killed/stoped/etc.

Screen
script.sh

I can x out of my putty window, but the process will remain active for a number of days before I discover that I can no longer connect to my screen.

I get something like this when I try to reconnect with screen -r

There is a screen on:
20351.pts-1.box654 (Dead ???)
Remove dead screens with 'screen -wipe'.
There is no screen to be resumed.

I wonder if this is all part of some automated bluehost cleanup or some such activity to kill processes left going after a user logs out. I've thought about trying to use a cron job to check if my script isn't running, and start it if it isn't, but this seems like overkill. There must be some way to simply start a process and have it continue to run. Any ideas?

It looks like people trying to use teamspeak are running into the same problem where their process will simply stop running.

http://www.bluehostforum.com/showthread.php?t=19240
http://www.bluehostforum.com/showthread.php?t=19851

phatfish
03-21-2010, 10:43 PM
Actually...looks like I need to get a dedicated IP.

http://helpdesk.bluehost.com/index.php/kb/article/000478

I'll try that and see if I continue to experience problems.

Oyjord
03-27-2010, 03:03 PM
Actually...looks like I need to get a dedicated IP.

http://helpdesk.bluehost.com/index.php/kb/article/000478

I'll try that and see if I continue to experience problems.

I have a dedicated IP, and my TS server keeps shutting down nonetheless.

Did you find a solution, by chance?

ax8l
04-05-2010, 02:38 PM
try to set a cronjob to test if the script is running and if it isnt then restart it...

phatfish
04-07-2010, 09:13 PM
I have a dedicated IP, and my TS server keeps shutting down nonetheless.

Did you find a solution, by chance?

This command seemed like it was working with the dedicated ip. Mine was running for a good week when I logged in with the actual IP and not my domain (use putty to connect to the static ip).

sh -c script.sh >/dev/null 2>&1 &"

But alas, my script just stopped running last night it seems.:mad:

How often is yours shutting down?



try to set a cronjob to test if the script is running and if it isnt then restart it...


*sigh* I guess this is my next route. I really don't understand why this is necessary however. Perhaps it is normal for scripts to just stop running on their own from time to time, but I still get the feeling bluehost might just assume something shouldn't be running and kill it (or kill it after 5 min if you don't have a dedicated IP)

phatfish
04-07-2010, 09:54 PM
This seems to be working. Just need to put in into a cron job now.




#! /bin/bash
if ps | grep -v grep | grep script.sh > /dev/null
then
echo "everything is cool"
else
echo "script.sh done died!" | mail -s "the website is down" you@domain.com
sh -c "/home/bin/script.sh >/dev/null 2>&1 &"
if ps | grep -v grep | grep script.sh > /dev/null
then
echo "script is back up" | mail -s "the website is back up" you@domain.com
else
echo "script.sh is still dead!" | mail -s "the website is still down" you@domain.com
fi
fi

phatfish
04-07-2010, 10:19 PM
...would help if I spelled my cron job right the first time.:rolleyes:


Looks good, we'll see how it goes long term. Running:

/home/bin/cronscript.sh >/dev/null 2>&1

phatfish
04-08-2010, 12:01 AM
Boo! My new checkscript to check and then restart the mainscript is working great...but its running every 5-15 min right now. Fn terrible. My mainscript is def getting killed by something. :mad:

phatfish
04-19-2010, 04:54 PM
Update:

So on the night of 4/7 I had the script die and get restarted by my cron job 8 times about 5-15 min apart.

Script has been running nonstop, then my cron job caught and restarted the script just once this morning.

Best I can tell, my process must be getting killed during maintenance or something similar since there doesn't seem to be any pattern from what I can see. ...would certainly be nice if blue host informed you about this maintenance and sent an email about killing a process you had running...oh well.

The uptime I'm getting is acceptable with the cron job in place.

pewp
05-01-2010, 09:47 AM
Update:

So on the night of 4/7 I had the script die and get restarted by my cron job 8 times about 5-15 min apart.

Script has been running nonstop, then my cron job caught and restarted the script just once this morning.

Best I can tell, my process must be getting killed during maintenance or something similar since there doesn't seem to be any pattern from what I can see. ...would certainly be nice if blue host informed you about this maintenance and sent an email about killing a process you had running...oh well.

The uptime I'm getting is acceptable with the cron job in place.

I've been having the same issue with TS and it seemed to have started to happen after they did the CPU throttling change. Something tells me they are killing processes for whatever reason.

Tisane
05-21-2010, 06:39 AM
I am experiencing the same issue. My processes keep mysteriously getting killed. As a test, I ran a very simple PHP daemon that was simply an infinite loop that would sleep for 5 seconds over and over and over. I doubt there was a bug in it that would have caused it to crash, but it seems to have died.

Supreme-Trans
06-04-2010, 12:49 PM
I am currently experiencing this with TS3. The process is killed by the server usually after a hour and a half.

I am trying to create a hourly stop/start script to see if that will solve the problem.

HigPup
06-06-2010, 07:56 AM
I am currently experiencing this with TS3. The process is killed by the server usually after a hour and a half.

I am trying to create a hourly stop/start script to see if that will solve the problem.

I have the exact same problem, but can't seem to get a cron job script to work to restart TS3. IF you find a solution could you post it. :)

HigPup
08-19-2011, 07:59 PM
I am currently experiencing this with TS3. The process is killed by the server usually after a hour and a half.

I am trying to create a hourly stop/start script to see if that will solve the problem.

I got my cron job to work with help with blue host. If you still need help I might be able to lend a hand.