login community faq

I have a python script that needs to run constantly (it sleeps every few secs so it doesn't hog resources) the link below seems like it will do the job but what's the best way to do the stop/restart side of things? a cross platform or cross linux os would be good.

http://community.webfaction.com/questions/6157/watchdog-script-to-keep-process-running-with-cron

asked Sep 07 '12 at 09:09

ewanmd's gravatar image

ewanmd
1


I wrote a script which will start/stop/restart gunicorn/django here which records the PID and runs only if it is not running. It can be modified for other python apps.

answered Sep 07 '12 at 17:22

johns's gravatar image

johns ♦♦
340427

Hi Johns,

That is exactly what I need thanks, trouble getting it to run my script though, doesn't write a pid file so stop/restart don't work. Can anyone point me in the right direction please?

!/bin/sh

app_admin

Usage: ./app_admin { start | stop | restart }

app_admin is a management script for gunicorn_django.

It is designed to work on the WebFaction platform with minimal effort.

The script requires gunicorn installed and enabled within your apps INSTALLED_APPS setting.

See http://gunicorn.org/ for instructions on gunicorn_django's use and installtion.

Activate virtual environment. These 2 lines may be disabled/deleted if you do not use virtualenv.

WORKON_HOME=/home/anyone/virt_env . $WORKON_HOME/bin/activate

The servers IP adderss, this should be 127.0.0.1.

ADDRESS='127.0.0.1'

The port of your Django app. This will be located within the control panel in the applications details.

SERVER_PORT='0000'

Set PYTHON to '/usr/local/bin/python2.X' for a regular deployment and the path of the binary in the virtualenv if you are using one.

PYTHON="python"

The path to gunicorn_django

GUNICORN="/home/<user name="">/bin/gunicorn_django"

The project location, settings.py, urls.py etc....

PROJECTLOC="/home/<user name="">/webapps/<app name="">/myproject"

The default args for gunicorn_django see http://gunicorn.org/configure.html#contents

DEFAULT_ARGS="--workers=3 "

Do not edit below this line

BASE_CMD=" $HOME/workspace/rawDecoder/rawDecoder.py" SERVER_PID="$HOME/tmp/myprogram.pid" start_server () { if [ -f $1 ]; then if [ "$(ps -p cat $1 | wc -l)" -gt 1 ]; then echo "A server is already running on ${ADDRESS}:${2}" return fi fi #cd $PROJECTLOC echo "starting ${ADDRESS}:${2}"

$BASE_CMD --daemon --bind=$ADDRESS:$SERVER_PORT --pid=$1 }

stop_server (){ if [ -f $1 ] && [ "$(ps -p cat $1 | wc -l)" -gt 1 ]; then echo "stopping server ${ADDRESS}:${2}" kill -9 cat $1 rm $1 else if [ -f $1 ]; then echo "server ${ADDRESS}:${2} not running" else echo "No pid file found for server ${ADDRESS}:${2}" fi fi }

case "$1" in 'start') start_server $SERVER_PID $SERVER_PORT ;; 'stop') stop_server $SERVER_PID $SERVER_PORT ;; 'restart') stop_server $SERVER_PID $SERVER_PORT sleep 2 start_server $SERVER_PID $SERVER_PORT ;; *) echo "Usage: $0 { start | stop | restart }" ;; esac

exit 0

(Sep 09 '12 at 07:27) ewanmd ewanmd's gravatar image

The above script might need to be edited a bit to fit your own application. If you cannot figure out how to fix this, please feel free to open a ticket - we can take a look at it for you.

(Sep 09 '12 at 09:32) todork todork's gravatar image
Your answer
If you have an answer to the above question, then use the form below. Otherwise, use the appropriate 'add new comment' button above to post your feedback.
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

×228
×50
×15
×3
×1

Asked: Sep 07 '12 at 09:09

Seen: 607 times

Last updated: Sep 09 '12 at 09:32

Plans & prices    Sign up    Why WebFaction?    Contact us    Affiliate program    Support    Legal    Jobs    Blog    Control panel login
Powered by OSQA
© Copyright 2003-2012 Swarma Limited - WebFaction is a service of Swarma Limited