# Custom script won't start at boot



## jewsofeast (Apr 21, 2011)

Hi,

I've created a custom script /usr/local/etc/rc.d/LaBrea.sh in order to enable LaBrea at boot. I have made it executable. 


```
#!/bin/sh

# $FreeBSD$
#
# PROVIDE: LaBrea
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# Add the following line to /etc/rc.conf[.local] to enable LaBrea
#
# LaBrea_enable (bool):	Set to "NO" by default.
#				Set it to "YES" to enable LaBrea.
# LaBrea_user (str):	User account to run with.
# LaBrea_flags (str):	Custom flags for LaBrea.

. /etc/rc.subr

name=LaBrea
rcvar=`set_rcvar`

command="/usr/local/bin/LaBrea -z -s -o -b -p 2000000 -i nfe0"
pidfile="/var/run/${name}.pid"
HOME=$(/usr/sbin/pw usershow -7 -n "${LaBrea_user}" | /usr/bin/cut -d: -f6)

start_cmd="${name}_start"


LaBrea_start()
{
	local pid

	pid=$(check_pidfile $pidfile $command)

	if [ -n "${pid}" ]; then
		echo "${name} already running? (pid=${pid})."
		return 1
	fi

	echo -n "Starting ${name}"
	/usr/bin/touch ${pidfile}
	/usr/sbin/chown ${LaBrea_user} ${pidfile}
	/usr/sbin/daemon -f -p ${pidfile} -u ${LaBrea_user} ${command} 
${LaBrea_flags}
	echo '.'
}

load_rc_config $name

: ${LaBrea_enable="YES"}
: ${LaBrea_user="david"}

run_rc_command "$1"
```

The script fails during boot with this error -


```
Starting LaBrea
usage: chown [-fhvx] [-R [-H | -L | -P]] owner[:group] file ...
       chown [-fhvx] [-R [-H | -L | -P]] :group file ...
daemon: illegal option -- z
usage: daemon [-cf] [-p pidfile] [-u user] command arguments ...
```

If you can could you please help me sort this out.

Best,

dave


----------



## SirDice (Apr 21, 2011)

My guess is that $LaBrea_user is empty.


----------



## jewsofeast (Apr 21, 2011)

Hi, I have already created a user, forgot to mention it earlier. Any other ideas.


----------



## SirDice (Apr 21, 2011)

It doesn't matter if the user exist it's the variable that's probably empty. I.e. nothing is assigned to it.

Run your script like so:
`# sh -x /usr/local/etc/rc.d/LaBrea.sh`

That will show you what happens.


----------



## jewsofeast (Apr 21, 2011)

Ok, This is what I get -


```
..............
+ sourced_files=:/etc/rc.conf::/etc/rc.conf.local:
+ [ -r /etc/rc.conf.local ]
+ _rc_conf_loaded=true
+ [ -f /etc/rc.conf.d/LaBrea ]
+ [ -n '' ]
+ eval echo '$LaBrea_enable_defval'
+ echo
+ _defval=''
+ [ -n '' ]
+ : YES
+ run_rc_command ''
+ _return=0
+ rc_arg=''
+ [ -z LaBrea ]
+ shift 1
+ rc_extra_args=''
+ _rc_prefix=''
+ eval '_override_command=$LaBrea_program'
+ _override_command=''
+ command='/usr/local/bin/LaBrea -z -s -o -b -p 2000000 -i nfe0'
+ _keywords='start stop restart rcvar '
+ rc_pid=''
+ _pidcmd=''
+ _procname='/usr/local/bin/LaBrea -z -s -o -b -p 2000000 -i nfe0'
+ [ -n '/usr/local/bin/LaBrea -z -s -o -b -p 2000000 -i nfe0' ]
+ [ -n /var/run/LaBrea.pid ]
+ _pidcmd='rc_pid=$(check_pidfile /var/run/LaBrea.pid /usr/local/bin/LaBrea -z -s -o -b -p 2000000 -i nfe0 )'
+ [ -n 'rc_pid=$(check_pidfile /var/run/LaBrea.pid /usr/local/bin/LaBrea -z -s -o -b -p 2000000 -i nfe0 )' ]
+ _keywords='start stop restart rcvar  status poll'
+ [ -z '' ]
+ rc_usage start stop restart rcvar status poll
+ echo -n 'Usage: /usr/local/etc/rc.d/LaBrea.sh [fast|force|one]('
Usage: /usr/local/etc/rc.d/LaBrea.sh [fast|force|one](+ _sep=''
+ echo -n start
start+ _sep='|'
+ echo -n '|stop'
|stop+ _sep='|'
+ echo -n '|restart'
|restart+ _sep='|'
+ echo -n '|rcvar'
|rcvar+ _sep='|'
+ echo -n '|status'
|status+ _sep='|'
+ echo -n '|poll'
|poll+ _sep='|'
+ echo ')'
)
+ exit 1
grumpy#
```


----------



## SirDice (Apr 21, 2011)

Add *start* like you normally would when starting a service.


----------



## jewsofeast (Apr 21, 2011)

Okay, I start it with -
[cmd=]LaBrea -z -s -o -b -p 2000000 -i nfe0[/cmd]

Now I will add 
[cmd=]LaBrea -z -s -o -b -p 2000000 -i nfe0 start[/cmd]

Is it correct?


----------



## SirDice (Apr 21, 2011)

No, I meant adding start as an argument to the rc script.


----------



## jewsofeast (Apr 21, 2011)

I can now start it after adding 


```
start_cmd="LaBrea -z -s -o -b -p 2000000 -i nfe0"
```

But it says -

```
pw: no such user `'
```


----------



## SirDice (Apr 21, 2011)

That's because the variable $LaBrea_user is still empty.


----------



## jewsofeast (Apr 21, 2011)

Sir Dice, thanks, if you can could you please suggest how to set it right.


----------



## SirDice (Apr 21, 2011)

Move these two to the top of the script:

```
: ${LaBrea_enable="YES"}
: ${LaBrea_user="david"}
```

And change the default to NO.


----------



## jewsofeast (Apr 21, 2011)

Sir Dice, thanks! It works. Have a great evening.


----------



## jewsofeast (Apr 22, 2011)

I had to add -


```
rcvar=${name}_enable
```
as per FreeBSD Porter's Handbook.


----------



## jewsofeast (Apr 22, 2011)

The script starts fine but the boot process stops after initiating the script -


```
Initiated on interface nfe0
nfe0: promiscuous mode enabled
Current average bw: 0 (bytes/sec)
Current average bw: 0 (bytes/sec)
42/0 packets (received/dropped) by filter
nfe0: promiscuous mode disabled
Exiting...
```

I had to stop it in order to get to the desktop. What do I do to get this right.


----------



## jewsofeast (Apr 28, 2011)

I added '&' to the end of start_cmd, the boot goes on without any obstacles.


----------

