# Can't stop/restart program started by rc.d script (“is not running”)



## vicmarconi (May 16, 2021)

Hi folks,

I've created this rc script to start gunicorn as a daemon on boot following this article (practical rc scripting).


```
#!/bin/sh

# PROVIDE: gunicorn_appleclue
# REQUIRE: DAEMON

. /etc/rc.subr

name=gunicorn_appleclue
rcvar=gunicorn_appleclue_enable

pidfile="/var/run/gunicorn/${name}.pid"

command="/usr/sbin/daemon"
command_args="-p ${pidfile} -r -t django_appleclue /home/victor/.venv/appleclue/bin/gunicorn -b 0.0.0.0:8001 -w 4 --pythonpath /home/victor/applications/appleclue/appleclue-web appleclue.wsgi"

load_rc_config $name
run_rc_command "$1"
```

It's working in a sense that after booting, the service is up. But the problem I'm facing is when trying stop/restart the service.


```
sudo service gunicorn_appleclue stop
gunicorn_appleclue not running? (check /var/run/gunicorn/gunicorn_appleclue.pid).

ls -la /var/run/gunicorn/gunicorn_appleclue.pid
-rw-------  1 root  wheel  3 May 15 14:25 /var/run/gunicorn/gunicorn_appleclue.pid
```

So, gunicorn is running and the pidfile is where it asked me. Is there anything I'm missing?


Thanks


----------



## covacat (May 17, 2021)

you may need something like 

command_interpreter="/usr/local/bin/python3.7"
if ps shows your process like python /some/script you need that

debug sh -x /usr/local/etc/rc.d/your_script status


----------



## SirDice (May 17, 2021)

Try `-P ${pidfile}` instead of `-p ${pidfile}`.


----------



## swills@ (May 17, 2021)

Try:


```
procname="/home/victor/.venv/appleclue/bin/gunicorn"
```

You may also need 
	
	



```
command_interpreter="/usr/local/bin/python3.7"
```
 as mentioned.


----------



## vicmarconi (May 17, 2021)

First of all, thank you both.
Indeed changing p to P (uppercase) did work.

I'm going to read about _command_interpreter_ and _procname_, because just uppercasing was enough, but I'm afraid I might be missing something else and stumble upon other errors.


----------



## swills@ (May 17, 2021)

FWIW, by using 
	
	



```
-P
```
, you're saving the pid of the 
	
	



```
daemon
```
 command and checking that it's running, rather than the actual application. This means that the output of 
	
	



```
service unicorn_appleclue status
```
 will return the wrong PID, as will reading the pid file, so if you were using that PID for other things, like say signaling to rotate logs or something similar, you'll be signaling the wrong process. May not matter in your case, but for other future readers it may.


----------



## astyle (May 19, 2021)

Just my 2 cents here, but sometimes, you need something like 
	
	



```
# /usr/local/etc/rc.d/my_service onerestart
```
 instead of 
	
	



```
# /usr/local/etc/rc.d/my_service restart
```
. Also, 
	
	



```
# /usr/local/etc/rc.d/my_service onestart
```
 instead of 
	
	



```
# /usr/local/etc/rc.d/my_service start
```
.


----------



## SirDice (May 19, 2021)

Services will refuse to start with `service <myservice> start` if there's no corresponding `<myservice>_enable="YES"` in /etc/rc.conf. `service <myservice> onestart` skips that check.


----------



## koverskeid (Oct 25, 2022)

I faced the same problem when trying to daemonize a shell script.

The start command actually started the service
The status command reported that the service was not running
The stop command complained that the service was not running
Running another start command withouth killing the service reported that the service was already running with the correct pid.

I simply added

```
command_interpreter="bin/sh"
```
 to the rc script file and it worked


----------

