FreeBSD-init: Difference between revisions

From wikinotes
 
Line 41: Line 41:
|}
|}
</blockquote><!-- Notes -->
</blockquote><!-- Notes -->
= Init Scripting =
<blockquote>
{{ NOTE |
Checkout the following initscripts (in my saltstack repostiory) for simple examples:
* taskwarrior's taskd
* minecraft
}}
{| class="wikitable"
|-
| <code>https://www.freebsd.org/doc/en/articles/rc-scripting/rcng-dummy.html</code> || Official Documentation
|-
| <code>/etc/rc.subr</code> || Library of rc-script functions
|}
== Debugging ==
<blockquote>
You can create 'debug mode' for a particular init script by
adding the following lines to it's init script (just below the shebang),
then rebooting and checking <code>/tmp/output.txt</code>.
<source lang="bash">
set -xv
exec 1>/tmp/output.txt 2>&1
</source>
</blockquote><!-- debugging -->
== Daemonize Non-Daemon Programs ==
<blockquote>
FreeBSD provides the program '''daemon''' which manages forking processes
along with their pidfile. Simply use this as your command instead. Note that you
will need to manage your status/stop manually if running the program this way.
<source lang="bash">
pidfile=/var/run/program_name.pid
is_running(){
    if [ -f $pidfile ]; then
        pgrep `cat $pidfile`
        return $0
    fi
    return 1
}
program_status(){
    if is_running; then
        echo "${name} is running"
    else
        echo "${name} is not running"
    fi
}
program_stop() {
    if is_running; then
        echo "stopping ${name}"
        kill -15 `cat pidfile`
    else
        echo "${name} is not running"
    fi
}
status_cmd=program_status
stop_cmd=program_stop
command="/usr/sbin/daemon"
command_args="-u $command_user -f -p $pidfile    $program_name -x -y -z"
</source>
</blockquote><!-- Daemonize Non-Daemon Programs -->
</blockquote><!-- Init Scripting -->

Latest revision as of 18:42, 22 October 2022

The FreeBSD init system is called rc.

Documentation

official tutorial https://docs.freebsd.org/en/articles/rc-scripting/#rcng-dummy
man rc.subr https://www.freebsd.org/cgi/man.cgi?query=rc.subr
man rc https://www.freebsd.org/cgi/man.cgi?query=rc
man rcorder https://www.freebsd.org/cgi/man.cgi?query=rcorder

Locations

/etc/rc.d/* system rc scripts
/usr/local/etc/rc.d/* user rc scripts
/etc/rc.subr available rc script methods

Notes

freebsd rc usage
freebsd rc syntax
freebsd rc troubleshooting