Freebsd rc syntax

From wikinotes
Revision as of 18:38, 22 October 2022 by Will (talk | contribs) (Created page with " == Example == <blockquote> {{ expand | Simple Annotated Skeleton file | <source lang="bash"> #!/bin/sh # PROVIDE: myservice # allows other services to require 'myservice' # REQUIRE: DAEMON # before this service starts, DAEMON must be provided # KEYWORD: shutdown # this service must be killed before the system shuts down . /etc/rc.subr # source rc.subr, generic rc-script functions </sour...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Example

Simple Annotated Skeleton file

#!/bin/sh

# PROVIDE: myservice                   # allows other services to require 'myservice'
# REQUIRE: DAEMON                      # before this service starts, DAEMON must be provided
# KEYWORD: shutdown                    # this service must be killed before the system shuts down

. /etc/rc.subr                         # source rc.subr, generic rc-script functions
# General Commands

name="myservice"                        # mandatory unique-name for script
rcvar="myservice_enable"                # name of variable in /etc/rc.conf used to enable service
# Read User-Configuration in /etc/rc.conf
# Set Variables that can be used in rc.conf

load_rc_config $name                             # load variables from /etc/rc.conf

: ${myservice_enable:=NO}                       # rc.conf variables, and their defaults
: ${myservice_config=/etc/myservice.conf}
: ${myservice_user=will}
# Environment Variables
${name}_env="VAR=value VAR2=value" # you can set environment variables for the command this way
# Builtin Variables thave affect CLI command to be run
${name}_user=myuser                                    # user to run command as (using su)
pidfile=/var/run/myservice/myservice.pid                # pidfile path
command=/usr/local/bin/myservice                       # path to binary
command_args="-d -p ${pidfile}"                         # arguments to use against $command
command_interpreter=/usr/local/bin/python2.7           # use different interpreter than /bin/sh
# Other Builtin Variables

# Command is not run if files do not exist
required_files="/etc/${name}.conf /usr/share/misc/${name}.rules"
# Start/Stop/Restart/... Functions

start_cmd="${name}_start"            # override default /etc/rc.subr start function with the function ${name}_start
stop_cmd=":"                         # use default /etc/rc.subr stop_cmd function

start_precmd="echo start"            # runs before 'start'
start_postcmd="echo end"             # runs after successful 'start'

dummy_start()                        # define your own functions
{
    echo "Nothing started."
}
# run Command

run_rc_command "$1"                   # based on above conts, run command using /etc/rc.subr function