• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Other Writing an rc script for gunicorn & django

forquare

Active Member

Thanks: 80
Messages: 200

#1
Hello all,

I'm looking to deploy a Django application onto a FreeBSD 11.1 server, I've read a number of articles about using www/py36-gunicorn to provide the communications between www/nginx and Django, and brief trials appear successful.

I'm now looking to make a service that I can service myapp start, and have come up and down with the FreeBSD host.

Currently I have something that looks like this:

Code:
#! /bin/sh
#
# $FreeBSD$
#

# PROVIDE: cribb
# REQUIRE: DAEMON
# KEYWORD: shutdown 

#
# Add the following lines to /etc/rc.conf to cribb:
#
#cribb_enable="YES"

. /etc/rc.subr

name="cribb"
rcvar="${name}_enable"
start_cmd="${name}_start"
stop_cmd="${name}_stop"

cribb_start(){
    chdir /opt/cribb
    daemon -r -S -p /opt/var/run/cribb.pid -T cribb -u cribb /usr/local/bin/gunicorn-3.6 --workers 3 --bind unix:/opt/var/run/cribb.sock cribb.wsgi:application
}

cribb_stop(){
    if [ -f /opt/var/run/cribb.pid ]; then
        echo -n "Stopping services: ${name}"
        kill -s INT $(cat /opt/var/run/cribb.pid)
        if [ -f /opt/var/run/cribb.pid ]; then
            rm -f /opt/var/run/cribb.pid
        fi
        if [ -f /opt/var/run/cribb.sock ]; then
            rm -f /opt/var/run/cribb.sock
        fi
        echo "."
    else
        echo "It appears ${name} is not running."
    fi
}
       

load_rc_config ${name}
run_rc_command "$1"
It's the beginnings of a script that is part from the Practical rc.d scripting in BSD, and part "getting it to work enough in practise". I know I need to create some variables to allow values to be read from rc.conf.

I have a few questions surrounding the subject:
  1. www/py36-gunicorn does not daemonise itself, is daemon(8) the correct tool to use in this case?
  2. I'm not sure how to stop a service running with daemon(8), so have put something together, however it doesn't look "proper" to me. Is it anywhere near correct? Or is there a better way?
  3. Is the whole script just awful and generally is there a better way to do this?
  4. Is the a better resource to read about writing service scripts? The article appears somewhat dated and limited, and there are lots of articles I've come across on the web which seem even less knowledgeable.
Many thanks,
Ben
 

SirDice

Administrator
Staff member
Administrator
Moderator

Thanks: 6,101
Messages: 26,992

#2
1) yes. That's perfect.

2) Just send a kill(1) to the PID of the process. Unless your application has a specific command to shutdown, then you should use that. There's no need to clean up the PID file yourself, daemon(8) already takes care of this.
 
Top