FreeBSD - creating a simple rc.d service

in #freebsd2 years ago (edited)

created services before, this is much easier than systemd.

this is all it takes to create a small service that mounts and unmounts a nfs partition.

yes I could put this into fstab.. but I found it can hang the computer on boot if it is not present.. and there is no easy way to 'turn it off' in the fstab without physically editing it.

it only requires one file, a rc.d style service file.

this makes things easier, i can not simply say.. as root

service zengarden start 
service zengarden stop

and add this to my rc.conf

zengarden_enable="YES"

here is the file that does all the work.

/usr/local/etc/rc.d/zengarden

#!/bin/sh

# PROVIDE: zengarden
# REQUIRE: serenity nfs-client
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf to enable the zengarden:
#
# zengarden_enable="YES"
#

. /etc/rc.subr

#export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

name="zengarden"
rcvar="zengarden_enable"
start_cmd="${name}_start" 
stop_cmd="${name}_stop" 

zengarden_start() 
{
    /sbin/mount serenity:/srv/nfs/zg /mnt/zg
}
zengarden_stop() 
{
    umount /mnt/zg
}

load_rc_config $name 
: ${zengarden_enable=NO}
run_rc_command "$1"

using search/replace all of 'zengarden' in this file .. you can create a simple rc.d service that works for you. (be sure you name the file you create the same)

edit the command within start/stop respectively to get the results you want.