Latest Version: 0.9.6.2
  Dashboard > Pylons Cookbook > Home > Scripts for paster serve
  Pylons Cookbook Log In | Sign Up   View a printable version of the current page.  
  Scripts for paster serve
Added by Max Ischenko, last edited by Mike Orr on Jul 08, 2008  (view change)
Labels: 

Introduction

For a production environment you probably don't want to have to manually start and stop your Pylons server every time the machine reboots. Which means you need a robust solution to ensure the application is always running and is automatically restarted if it exists for some reason.

This recipe focuses on writing these scripts by hand. For alternate solutions, check these recipes:

Creating Init Scripts

Create a file /etc/init.d/pylons which looks like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#!/bin/sh -e

case "$1" in
  start)
    paster serve --daemon --pid-file=/MyProject/paster.pid --log-file=/MyProject/paster.log /MyProject/development.ini start
    ;;
  stop)
    paster serve --daemon --pid-file=/MyProject/paster.pid --log-file=/MyProject/paster.log  /MyProject/development.ini stop
    ;;
  restart)
    paster serve  --daemon --pid-file=/MyProject/paster.pid --log-file=/MyProject/paster.log /MyProject/development.ini restart
    ;;
  *)
    echo $"Usage: $0 {start|stop|restart}"
    exit 1
esac

You should adjust the paths to point to the correct places for your .pid, .log and .ini files.

Issuing one of the above commands without the start, stop or restart is the same as using start. The full range of options is documented at http://pythonpaste.org/script/#running-the-server

Make the file executable

chmod 755 /etc/init.d/pylons

Then for each of the directories /etc/rc2.d /etc/rc3.d /etc/rc4.d and /etc/rc5.d you should run the following commands:

cd /etc/rc2.d
ln -s ../init.d/pylons S20pylons

Different platforms have slightly different runlevels so you should run the above commands for whichever directories are appropriate on your platform.

You should now be able to start, stop and restart the server with the following commands and you should find the server loads when the system loads and stops when the system is shutdown:

/etc/init.d/pylons start
/etc/init.d/pylons stop
/etc/init.d/pylons restart

Each time you start the server the MyProject/paster.pid and MyProject/paster.log files will be created and each time you stop the server they will be deleted. The MyProject/paster.pid file contains the process ID of the server so if you restart the server, the number stored in the file will change.

If the server is not running you will not be able to restart it, you'll have to use the /etc/init.d/pylons start command first.

Setting Up A Cron Job

Although the paster server will now start and stop when the server loads and shuts down it would be nice to have a have a cron job that checks it is alive every few minutes and can restart it if necessary.

Issue this command to edit your crontab:

crontab -e

Then add the following entry:

*/1 * * * * paster serve --daemon --pid-file=/MyProject/paster.pid --log-file=/MyProject/paster.log /MyProject/development.ini

This will try to start the daemon every minute. There is an explanation of these settings at this site http://www.unixgeeks.org/security/newbie/unix/cron-1.html and you are obviously free to change how often the server is restarted to suit your needs. There is no reason to expect the server would crash so every minute should be more than enough.

When using cron jobs it is usually a good idea to specify a MAILTO address so that the output of the commands is emailed to you. This is useful if the command you run outouts an error message when it fails. Run crontab -e again and then add this at the very top, replacing it with your own email address:

MAILTO=james@example.com

If you don't like the default editor chosen for you to edit your list of cron jobs you can change it by setting the EDITOR variable. If you are using the bash shell you do so like this:

export EDITOR=vim

This doesn't work for me because of the enviorment, i rather use this:

#!/bin/sh -e

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
case "$1" in
  start)
    paster serve --daemon --pid-file=/MyProject/paster.pid --log-file=/MyProject/paster.log /MyProject/development.ini start
    ;;
  stop)
    paster serve --daemon --pid-file=/MyProject/paster.pid --log-file=/MyProject/paster.log  /MyProject/development.ini stop
    ;;
  restart)
    paster serve  --daemon --pid-file=/MyProject/paster.pid --log-file=/MyProject/paster.log /MyProject/development.ini restart
    ;;
  *)
    echo $"Usage: $0 {start|stop|restart}"
    exit 1
esac

Posted by Domen Kožar at Jul 18, 2008 13:03 | Permalink

Sorry, my bad:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#!/bin/sh -e

project="PUT_STUFF_HERE/pylons/helloworld"
cd $project

case "$1" in
start)
paster serve --daemon --pid-file=paster.pid --log-file=paster.log --reload development.ini start
;;
stop)
paster serve --daemon --pid-file=paster.pid --log-file=paster.log  development.ini stop
;;
restart)
paster serve  --daemon --pid-file=paster.pid --log-file=paster.log development.ini restart
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
esac

Posted by Domen Kožar at Jul 18, 2008 13:05 | Permalink
Site running on a free Atlassian Confluence Open Source Project License granted to Pylons. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.3.3 Build:#645 Feb 13, 2007) - Bug/feature request - Contact Administrators
Top