# Trying to set up simple service to run django development server



## thedude22 (Jul 5, 2021)

```
#!/bin/sh
. /etc/rc.subr

name="dashboard"
rcvar="${name}_enable"
load_rc_config ${name}

: ${dashboard_enable:=NO}
: ${dashboard_user:=root}
: ${dashboard_group:=wheel}
: ${dashboard_pidfile:="/var/run/${name}/${name}.pid"}

start_precmd=". /zroot/www/venv/bin/activate"
pidfile=${dashboard_pidfile}
command_interpreter="/zroot/www/venv/bin/python"
command="/zroot/www/dashboard/manage.py"
command_args="runserver 0.0.0.0:8012"                                                                                               1
run_rc_command "$1"
```
It doesn't write to the pid file.  Am I supposed to manually create the file before hand?

It doesn't see that it's running when I check the status, probably because of the pidfile issue.

It also won't start at boot, probably due to the same issue.

What am I missing here?


----------



## SirDice (Jul 5, 2021)

thedude22 said:


> ```
> : ${dashboard_user:=root}
> : ${dashboard_group:=wheel}
> ```


This is giving me heart palpitations.


----------



## thedude22 (Jul 5, 2021)

Sorry bud.  I just set it up temporarily that way to try to get things working.

Believe me, I'm stressing out too, for a whole different reason.


----------



## Tieks (Jul 5, 2021)

thedude22 said:
			
		

> Whatam Imissing here?



At first glance a start_cmd and a stop_cmd. And obviously heart palpitations.


----------



## thedude22 (Jul 5, 2021)

Not missing those.  Believe me.


----------



## thedude22 (Jul 5, 2021)

Are start_cmd and stop_cmd necessary?  I'm following section 5 from this page.









						Practical rc.d scripting in BSD
					

A guide to writing new rc.d scripts and understanding those already written




					docs.freebsd.org


----------



## Tieks (Jul 5, 2021)

Depends on the service. Services that always run will usually be started and stopped at boot/shutdown, these only need <service name>_enabled="YES" in /etc/rc.conf. Since the pid-file is missing Django probably isn't started at all. I don't know Django well enough to tell what is needed for it to run, you know better than me. You found the right guide already, you might want to take a look at the files in /usr/local/etc/rc.d for an example. That is where your rc file should reside, because Django is not part of the base system. You'll find files for apache, dbus, cupsd, saned and the likes there. Take a look at those, some are very short and simple.


----------



## thedude22 (Jul 5, 2021)

Well I can start the service manually.  It just doesn't start at boot even though it is enabled.  Yes the file is located in /usr/local/etc/rc.d

But even when I start the service, nothing happens to the pid file.  If it doesn't exist, it is not created.  If I manually create it beforehand, it is not written to.  But the service works and I can access the dashboard page from a browser.  Also checking the status will say the service is not running, even though it is.  It just seems like the pidfile variable isn't working.  I have seen other people say you have to set the procname variable to get it to work but I tried setting it to the same python path in the command_interpreter variable and that didn't change anything.

I'm just guessing, but I think once I can get pidfile working then maybe it will start at boot, as expected.


----------



## Jose (Jul 5, 2021)

I don't see an option for writing a pid file in the runserver documentation:








						Django
					

The web framework for perfectionists with deadlines.




					docs.djangoproject.com
				




Contrast with the WSGI docs which do have a pid file option:








						Django
					

The web framework for perfectionists with deadlines.




					docs.djangoproject.com
				




I can't prove this, but I think you'll need a `start_cmd` and `stop_cmd` if the program you're trying to daemonize does not create a pid file.


----------



## thedude22 (Jul 5, 2021)

Oh I see.  I thought the pidfile was handled by the rc.subr script.


----------



## Tieks (Jul 5, 2021)

thedude22 said:
			
		

> I think once I can get pidfile working then maybe it will start at boot, as expected



Start at boot and write a pid file are different things. A binary may or may not write a .pid. Django obviously doesn't because it's not there when you run it. I wrote a simple daemon in Perl that logs incoming phone calls. This is the file (/usr/local/etc/rc.d/fbcalls) that I use to start at boot and stop at shutdown:

```
#!/bin/sh

echo -n " fbcalls "

case $1 in
start)
   echo -n "starting.."
   /usr/local/sbin/fbcalls.pl -o
   ;;
stop)
   echo -n "stopping..."
   kill -s TERM `cat /var/run/fbcalls.pid`
   ;;
*)
   echo "usage $0: [ start | stop ]" 2>&1
   ;;
esac
```

It starts at boot saying "fbcalls starting...", you can see why. It will stop at shutdown. Perl script fbcalls.pl writes the .pid file, so it can be used to kill that process at shutdown. Now Django doesn't write a .pid, so we have to do it ourselves. In this example that could be done by adding a line `pgrep -f fbcalls > /var/run/fbcalls.pid` immediately after starting the perl script.


----------



## Jose (Jul 5, 2021)

Alternatively, you could use the daemon(8) utility as described here:





						Supervised FreeBSD rc.d script for a Go daemon | redByte blog
					






					redbyte.eu


----------



## thedude22 (Jul 5, 2021)

Thanks for the suggestions.   I think I'll just set up a proper web server.  I was trying to avoid it because it's just a page for only one user, me, and on my LAN only.  But nbd.


----------

