# Rc.d script does not start at boot



## wernerdev (Apr 3, 2011)

I am messing with the problem all day, and it's making me crazy.

I am using this script (http://dl.dropbox.com/u/5830692/sickbeard.txt) which I modified and changed a little.

I also use this same script for SABnzbd and CouchPotato. All 3 are Python scripts.


```
#!/bin/sh
#
# PROVIDE: sickbeard
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf.local or /etc/rc.conf
# to enable this service:
#
# sickbeard_enable (bool):	Set to NO by default.
#			Set it to YES to enable it.
# sickbeard_user:  The user account Sick Beard daemon runs as what
#			you want it to be. It uses '_sabnzbd' user by
#			default. Do not sets it as empty or it will run
#			as root.
# sickbeard_dir:	Directory where Sick Beard lives.
#			Default: /usr/local/sickbeard
# sickbeard_chdir:  Change to this directory before running Sick Beard.
#     Default is same as sickbeard_dir.
# sickbeard_pid:  The name of the pidfile to create.
#     Default is sickbeard.pid in sickbeard_dir.

. /etc/rc.subr

name="sickbeard"
rcvar=${name}_enable

load_rc_config ${name}

: ${sickbeard_enable:="NO"}
: ${sickbeard_user:="werner"}
: ${sickbeard_dir:="/usr/home/werner/Applications/sickbeard"}
: ${sickbeard_chdir:="${sickbeard_dir}"}
: ${sickbeard_pid:="${sickbeard_dir}/sickbeard.pid"}

WGET="/usr/local/bin/wget" # You need wget for this script to safely shutdown Sick Beard.
HOST="127.0.0.1" # Set Sick Beard address here.
PORT="8081" # Set Sick Beard port here.
SBUSR="" # Set Sick Beard username (if you use one) here.
SBPWD="" # Set Sick Beard password (if you use one) here.

status_cmd="${name}_status"
stop_cmd="${name}_stop"

command="/usr/sbin/daemon"
command_args="-f -p ${sickbeard_pid} python ${sickbeard_dir}/SickBeard.py -d"

# Ensure user is root when running this script.
if [ `id -u` != "0" ]; then
  echo "Oops, you should be root before running this!"
  exit 1
fi

verify_sickbeard_pid() {
    # Make sure the pid corresponds to the Sick Beard process.
    pid=`cat ${sickbeard_pid} 2>/dev/null`
    ps -p ${pid} | grep -q "python ${sickbeard_dir}/SickBeard.py"
    return $?
}

# Try to stop Sick Beard cleanly by calling shutdown over http.
sickbeard_stop() {
    echo "Stopping $name"
    verify_sickbeard_pid
    kill ${pid}
    if [ -n "${pid}" ]; then
      wait_for_pids ${pid}
      echo "Stopped"
    fi
}

sickbeard_status() {
    verify_sickbeard_pid && echo "$name is running as ${pid}" || echo "$name is not running"
}

run_rc_command "$1"
```

I added these 3 rules to rc.conf to make the scripts start at boot.

```
sabnzbd_enable="YES"
sickbeard_enable="YES"
couchpotato_enable="YES"
```

I tested all 3 scripts at command-line and they all worked.
Then it was time to test them, and I rebooted my system.

Only 2 of them work... x( They all have the same permissions and same owner (root).
I also enabled console logging and there I see the system does not even try to start it.


```
Apr  3 18:47:16 FreeBSD kernel: Starting syslogd.
Apr  3 18:47:16 FreeBSD kernel: Clearing /tmp (X related).
Apr  3 18:47:17 FreeBSD kernel: Removing stale Samba tdb files:
Apr  3 18:47:17 FreeBSD kernel: .
Apr  3 18:47:17 FreeBSD last message repeated 6 times
Apr  3 18:47:17 FreeBSD kernel: done
Apr  3 18:47:17 FreeBSD kernel: Starting nmbd.
Apr  3 18:47:17 FreeBSD kernel: Starting smbd.
Apr  3 18:47:18 FreeBSD kernel: Starting sabnzbd.
Apr  3 18:47:19 FreeBSD kernel: Starting couchpotato.
Apr  3 18:47:19 FreeBSD kernel: Updating motd:
Apr  3 18:47:19 FreeBSD kernel: .
Apr  3 18:47:20 FreeBSD kernel: Configuring syscons:
Apr  3 18:47:20 FreeBSD kernel: keymap
Apr  3 18:47:20 FreeBSD kernel: blanktime
Apr  3 18:47:20 FreeBSD kernel: .
Apr  3 18:47:20 FreeBSD kernel: Starting sshd.
```

I tried deleting the script and make it all over from scratch.
But my thoughts are: If it works from command-line why should it not work at boot.

I hope someone can help me with the mysterious problem.


----------



## wernerdev (Apr 3, 2011)

Just modified one of the 2 working scripts to start the program that doesn't load. And it just worked.

Somehow the Sick Beard rc.d script doesn't get loaded. But why? Is there a config wile which prevents some scripts from loading or something?


----------



## wernerdev (Apr 4, 2011)

Today I installed Transmission-Daemon which added a new init script to "rc.d". And then at the same time something magically happened. The script that didn't work, now works!

But I'm still curious why it didn't want to start in the first place. Anyone knows?


----------



## da1 (Apr 4, 2011)

No idea man, but I had the same issue more than once. One way around it is an entry in crontab with "@reboot" statement.


----------



## kpa (Apr 4, 2011)

Your script may need a REQUIRE keyword with appropriate contents to avoid starting it too early in the boot, for example the net/miniupnpd port has this in its rc(8) script:

```
# REQUIRE: NETWORKING SERVERS DAEMON ldconfig resolv
```


----------



## DutchDaemon (Apr 4, 2011)

Just start it from cron, so everything it needs is already running.


----------



## Cryptic1911 (Apr 5, 2011)

Try tossing in a PATH statement like: 


```
PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"
```

After this line:


```
: ${sickbeard_pid:="${sickbeard_dir}/sickbeard.pid"}
```

I had issues with some of mine not starting properly, that is what fixed it. It tried firing them up before the PATH is defined I guess


----------



## wernerdev (Apr 5, 2011)

@Cryptic1911 I shortened my RC.D script which resulted in Sick Beard not starting anymore. It searched for it's files at / instead of it's own folder. I reported the bug to the developer which said it was going to get fixed soon. But I just tried adding the PATH rule. It solved the problem. Thanks for that.
But it still is a Sick Beard bug, because scripts like SABnzbd and Couch Potato just run fine using the same script.

As for the original problem this topic was created for: I still don't know what happened, but after adding Transmission the problem magically solved itself. I was hoping someone could tell me there was some sort of RC.D cache or something. But it seems it is indeed a weird and unknown problem.

Thanks for the help everyone!


----------



## kriss (Jul 30, 2011)

Can somebody post a rc.d script for Couchpotato?
I have SABnzbd and Sickbeard already running and auto start/shutdown works
But I can't get Couchpotato to start using the script from the first post.


----------



## kriss (Jul 31, 2011)

Never mind.
Already got it.


----------



## XeonNAS (Mar 31, 2014)

Long time after the fact I know but @Cryptic1911 fix to insert the path worked for me in a NAS4Free jail implementation using The Brig...


```
PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"
```


----------

