# rc script issues



## Synth Meat (Feb 17, 2016)

I'm having two issues with following rc script (some commands trimmed for brevity):


```
#!/bin/sh

# PROVIDE: sfile
# REQUIRE: NETWORKING SERVERS DAEMON LOGIN
# KEYWORD: shutdown

. /etc/rc.subr

name=sfile
rcvar=sfile_enable

load_rc_config $name

: ${sfile_enable=NO}
: ${sfile_user=synthmeat}
: ${sfile_group=synthmeat}

sfile_node=/usr/local/bin/node
sfile_workdir=/home/synthmeat/work/showdown-file
sfile_script=build/main.js
sfile_logfile=/home/synthmeat/logs/showdown-file.log
sfile_pidfile=/home/synthmeat/pids/showdown-file.pid

start_cmd=sfile_start

sfile_start()
{
    if [ -e ${sfile_pidfile} ]
    then
        if [ `pgrep -F ${sfile_pidfile}` ]
        then echo 'showdown file server is already running'
        else
            echo 'starting showdown file server'
            cd ${sfile_workdir}
            nohup ${sfile_node} ${sfile_script} > ${sfile_logfile} 2>&1 &
        fi
    else
        echo 'starting showdown file server'
        cd ${sfile_workdir}
        nohup ${sfile_node} ${sfile_script} > ${sfile_logfile} 2>&1 &
    fi
}

run_rc_command "$1"
```

First is that processes started from it are always by the user that started the service (say, "sudo service sfile start" will start processes as root). I thought ${name}_user in script or in rc.conf should override the user running it?

Second is at boot, in that, though it does start the script, no processes get started by it, unlike when ran in some login shell. sfile_enable="YES" is set in /etc/rc.conf as well as rc_debug, which gives me no particular relevant information.


----------



## Juha Nurmela (Feb 17, 2016)

$PATH problem?

Instead of `nohup`, you should use daemon(8) and set the user/pidfile/etc explicitly.
Also, `if [ -e pidfile ] && pgrep -F pidfile` would simplify the 3 cases to 2.

Juha


----------



## Synth Meat (Feb 17, 2016)

From what I can tell now, I'm:
- not using "command", which is required for {$name}_user
- overriding start, which basically leaves me out of any goodies of _user or command

I appear to have fundamental misunderstandings here, I'll RTFM. Thanks though.


----------



## Juha Nurmela (Feb 17, 2016)

I've used this as a boilerplate. It's not proper right, but works, sort of.

Juha


```
#!/bin/sh

# PROVIDE: arvuutin
# REQUIRE: LOGIN
# KEYWORD: shutdown

. /etc/rc.subr

name="arvuutin"
user="nobody"

pidfile="/var/run/${name}.pid"
rcvar="${name}_enable"
start_cmd=${name}_start
command="daemon"

arvuutin_start()
{
  /usr/sbin/daemon -cf -P $pidfile -u $user some random command line
}

load_rc_config $name
run_rc_command "$1"
```


----------

