Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Keep a shell script running after disconnect

  1. #1
    Join Date
    Mar 2010
    Posts
    7

    Default Keep a shell script running after disconnect

    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

  2. #2
    Join Date
    Mar 2010
    Posts
    7

    Lightbulb May have answered my own question

    Actually...looks like I need to get a dedicated IP.

    http://helpdesk.bluehost.com/index.p...article/000478

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

  3. #3
    Join Date
    Apr 2008
    Posts
    23

    Default

    Quote Originally Posted by phatfish View Post
    Actually...looks like I need to get a dedicated IP.

    http://helpdesk.bluehost.com/index.p...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?

  4. #4
    Join Date
    Apr 2010
    Posts
    2

    Default

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

  5. #5
    Join Date
    Mar 2010
    Posts
    7

    Default

    Quote Originally Posted by Oyjord View Post
    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?


    Quote Originally Posted by ax8l View Post
    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)
    Last edited by phatfish; 04-07-2010 at 09:19 PM. Reason: Changed formating

  6. #6
    Join Date
    Mar 2010
    Posts
    7

    Default

    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

  7. #7
    Join Date
    Mar 2010
    Posts
    7

    Default

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

  8. #8
    Join Date
    Mar 2010
    Posts
    7

    Thumbs down

    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:
    Last edited by phatfish; 04-08-2010 at 12:02 AM. Reason: made more clear

  9. #9
    Join Date
    Mar 2010
    Posts
    7

    Default

    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.

  10. #10
    Join Date
    Mar 2009
    Posts
    9

    Default

    Quote Originally Posted by phatfish View Post
    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.
    Last edited by pewp; 05-01-2010 at 11:06 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •