# rc init script failing to start a process



## fwyKKCkQze2z (Dec 20, 2017)

Hi

I am trying to make a startup script (in /usr/local/etc/rc.d/) start several processes. Somehow the script only manages to start some of the processes. Note that this is my first somewhat real attempt at writing a startup script.

Here is (hopefully) the relevant part of the script.


```
...
yaf_start()
{
        echo "[YAF] Starting yaf (re0)..."
        /usr/local/bin/yaf -c /usr/local/etc/yaf-re0.init --become-user <user> --become-group <group> &
        echo "[YAF] Starting yaf (re1)..."
        /usr/local/bin/yaf -c /usr/local/etc/yaf-re1.init --become-user <user> --become-group <group> &
        echo "[YAF] Starting yaf (re2)..."
        /usr/local/bin/yaf -c /usr/local/etc/yaf-re2.init --become-user <user> --become-group <group> &
        echo "[YAF] Starting filedaemon..."
    /usr/local/bin/filedaemon --in "/var/spool/silk/yaf/ipfix-re*" --nextdir "/var/spool/silk/destination" --lock
}
...
```

After a reboot of the system, often (always?) `filedaemon` is not running, although `yaf` is. A `/usr/local/etc/rc.d/yaf restart` cures this.

I tried the following entry in /etc/rc.conf

```
rc_debug="yes"
```
but failed to find anything that looked relevant.

Can someone give me a hint how I can debug/investigate this? If possible avoiding to reboot the system (too often) as it is in production...


----------



## usdmatt (Dec 21, 2017)

What does the whole script look like and output of `service -r`.


----------



## fwyKKCkQze2z (Dec 21, 2017)

```
$ service -r
/etc/rc.d/growfs
/etc/rc.d/sysctl
/etc/rc.d/hostid
/etc/rc.d/zvol
/etc/rc.d/dumpon
/etc/rc.d/ddb
/etc/rc.d/geli
/etc/rc.d/gbde
/etc/rc.d/ccd
/etc/rc.d/swap
/etc/rc.d/fsck
/etc/rc.d/root
/etc/rc.d/mdconfig
/etc/rc.d/hostid_save
/etc/rc.d/mountcritlocal
/etc/rc.d/zfsbe
/etc/rc.d/zfs
/etc/rc.d/var
/etc/rc.d/cleanvar
/etc/rc.d/FILESYSTEMS
/etc/rc.d/kldxref
/etc/rc.d/kld
/etc/rc.d/addswap
/etc/rc.d/adjkerntz
/etc/rc.d/atm1
/etc/rc.d/hostname
/etc/rc.d/ip6addrctl
/etc/rc.d/netoptions
/etc/rc.d/random
/etc/rc.d/sppp
/etc/rc.d/ipfilter
/etc/rc.d/ipnat
/etc/rc.d/ipfs
/etc/rc.d/serial
/etc/rc.d/iovctl
/etc/rc.d/netif
/etc/rc.d/ppp
/etc/rc.d/ipfw
/usr/local/etc/rc.d/silk
/usr/local/etc/rc.d/yaf
/etc/rc.d/ldconfig
/etc/rc.d/devd
/etc/rc.d/ipsec
/etc/rc.d/atm2
/etc/rc.d/pfsync
/etc/rc.d/pflog
/etc/rc.d/pf
/etc/rc.d/stf
/etc/rc.d/routing
/etc/rc.d/netwait
/etc/rc.d/resolv
/etc/rc.d/local_unbound
/etc/rc.d/nsswitch
/etc/rc.d/routed
/etc/rc.d/rtsold
/etc/rc.d/accounting
/etc/rc.d/newsyslog
/etc/rc.d/syslogd
/etc/rc.d/ntpdate
/etc/rc.d/rpcbind
/etc/rc.d/nfsclient
/etc/rc.d/nisdomain
/etc/rc.d/ypserv
/etc/rc.d/ypbind
/etc/rc.d/ypset
/etc/rc.d/amd
/etc/rc.d/atm3
/etc/rc.d/auditd
/etc/rc.d/auditdistd
/etc/rc.d/tmp
/etc/rc.d/cleartmp
/etc/rc.d/ctld
/etc/rc.d/dmesg
/usr/local/etc/rc.d/tcsd
/etc/rc.d/hastd
/usr/local/etc/rc.d/dhcp6s
/etc/rc.d/static_ndp
/etc/rc.d/static_arp
/etc/rc.d/bridge
/etc/rc.d/route6d
/etc/rc.d/defaultroute
/etc/rc.d/iscsid
/etc/rc.d/iscsictl
/etc/rc.d/keyserv
/etc/rc.d/nfsuserd
/etc/rc.d/gssd
/etc/rc.d/quota
/etc/rc.d/mountd
/etc/rc.d/nfsd
/etc/rc.d/statd
/etc/rc.d/lockd
/etc/rc.d/pppoed
/etc/rc.d/pwcheck
/etc/rc.d/virecover
/etc/rc.d/ypldap
/etc/rc.d/DAEMON
/usr/local/etc/rc.d/dhcp6c
/etc/rc.d/NETWORKING
/etc/rc.d/mountcritremote
/etc/rc.d/devfs
/etc/rc.d/ipmon
/etc/rc.d/kdc
/etc/rc.d/mdconfig2
/usr/local/etc/rc.d/named
/usr/local/etc/rc.d/named.orig
/usr/local/etc/rc.d/named.patched
/etc/rc.d/watchdogd
/etc/rc.d/savecore
/etc/rc.d/archdep
/etc/rc.d/abi
/etc/rc.d/SERVERS
/usr/local/etc/rc.d/tpmd
/usr/local/etc/rc.d/spawn-fcgi
/usr/local/etc/rc.d/snmptrapd
/usr/local/etc/rc.d/snmpd
/etc/rc.d/apm
/etc/rc.d/apmd
/etc/rc.d/bootparams
/etc/rc.d/hcsecd
/etc/rc.d/bthidd
/etc/rc.d/local
/etc/rc.d/lpd
/etc/rc.d/motd
/etc/rc.d/mountlate
/etc/rc.d/nscd
/etc/rc.d/ntpd
/etc/rc.d/powerd
/etc/rc.d/rarpd
/etc/rc.d/rctl
/etc/rc.d/sdpd
/etc/rc.d/rfcomm_pppd_server
/etc/rc.d/rtadvd
/etc/rc.d/rwho
/etc/rc.d/timed
/etc/rc.d/ugidfw
/etc/rc.d/utx
/etc/rc.d/yppasswdd
/usr/local/etc/rc.d/ansshfirewall
/usr/local/etc/rc.d/dhcp6relay
/usr/local/etc/rc.d/isc-dhcpd
/usr/local/etc/rc.d/isc-dhcpd6
/etc/rc.d/LOGIN
/usr/local/etc/rc.d/racoon
/usr/local/etc/rc.d/php-fpm
/usr/local/etc/rc.d/openvpn
/usr/local/etc/rc.d/nullmailer
/usr/local/etc/rc.d/mrouted
/usr/local/etc/rc.d/lighttpd
/usr/local/etc/rc.d/git_daemon
/usr/local/etc/rc.d/fail2ban
/usr/local/etc/rc.d/dbus
/usr/local/etc/rc.d/avahi-daemon
/usr/local/etc/rc.d/avahi-dnsconfd
/etc/rc.d/zfsd
/etc/rc.d/ypxfrd
/etc/rc.d/ypupdated
/etc/rc.d/ubthidhci
/etc/rc.d/syscons
/etc/rc.d/swaplate
/etc/rc.d/sshd
/etc/rc.d/sendmail
/etc/rc.d/cron
/etc/rc.d/jail
/etc/rc.d/localpkg
/etc/rc.d/securelevel
/etc/rc.d/othermta
/etc/rc.d/nfscbd
/etc/rc.d/msgs
/etc/rc.d/moused
/etc/rc.d/mixer
/etc/rc.d/kpasswdd
/etc/rc.d/kfd
/etc/rc.d/kadmind
/etc/rc.d/ipropd_slave
/etc/rc.d/ipropd_master
/etc/rc.d/inetd
/etc/rc.d/hostapd
/etc/rc.d/gptboot
/etc/rc.d/geli2
/etc/rc.d/ftpd
/etc/rc.d/ftp-proxy
/etc/rc.d/bsnmpd
/etc/rc.d/blacklistd
/etc/rc.d/bgfsck
/etc/rc.d/autounmountd
/etc/rc.d/automountd
/etc/rc.d/automount
```

This is the whole script. Note that I spotted a typo and changed

```
: ${silk_enable:=no}
```
to

```
: ${yaf_enable:=no}
```


```
#!/bin/sh

# PROVIDE: yaf
# REQUIRE: silk
# BEFORE:  LOGIN
# KEYWORD: nojail

. /etc/rc.subr

name="yaf"
rcvar="yaf_enable"
start_cmd="${name}_start"
stop_cmd="${name}_stop"

load_rc_config $name
: ${silk_enable:=no}

yaf_start()
{
        echo "[YAF] Starting yaf (re0)..."
        /usr/local/bin/yaf -c /usr/local/etc/yaf-re0.init --become-user <user> --become-group <group> &
        echo "[YAF] Starting yaf (re1)..."
        /usr/local/bin/yaf -c /usr/local/etc/yaf-re1.init --become-user <user> --become-group <group> &
        echo "[YAF] Starting yaf (re2)..."
        /usr/local/bin/yaf -c /usr/local/etc/yaf-re2.init --become-user <user> --become-group <group> &
        echo "[YAF] Starting filedaemon..."
    /usr/local/bin/filedaemon --in "/var/spool/silk/yaf/ipfix-re*" --nextdir "/var/spool/silk/destination" --lock
}

yaf_stop()
{
        echo "[YAF] Stopping yaf (pkill)..."
        /bin/pkill yaf
        echo "[YAF] Stopping filedaemon (pkill)..."
        /bin/pkill filedaemon
}

run_rc_command "$1"
```


----------



## ralphbsz (Dec 23, 2017)

When the machine boots, do you see the "Starting yaf (re0)", ... "Starting filedaemon..." messages show up on the console?  Do you see all of them or only some of them?

Suggestion: Add more debugging to your rc script, so it keep detailed logs in a safe place.  Suggestion:

```
logfile=/tmp/my.log
yaf_start()
{
  echo -n yaf_start function beginning at "" >> $logfile; date >> $logfile
  /usr/local/bin/yaf -c ...re0.init ... &
  echo -n yaf_start function survived starting re0 status is $status "" >> $logfile; date >> $logfile
  /usr/local/bin/yaf -c ...re1.init ... &
  echo -n yaf_start function survived starting re1 status is $status "" >> $logfile; date >> $logfile
  ...
  usr/local/bin/filedaemon ...
  echo -n yaf_start function survived to the end status is $status "" >> $logfile; date >> $logfile
}
```
Then you can read your personal log file, with every line date-stamped, and perhaps figure out what is happening.


----------



## SirDice (Jan 15, 2018)

I would suggest using daemon(8) instead of `/usr/bin/app {....} &`.


----------

