WebFaction
Community site: login 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 07 Sep '12, 09:09

ewanmd
122
accept rate: 0%


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.

permanent link

answered 07 Sep '12, 17:22

johns
5.4k412
accept rate: 23%

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

(09 Sep '12, 07:27) ewanmd

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.

(09 Sep '12, 09:32) todork
Your answer
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

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×337
×81
×24
×4
×1

question asked: 07 Sep '12, 09:09

question was seen: 6,674 times

last updated: 09 Sep '12, 09:32

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2020 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM