I started working with a great web server called caddy and started working on an rc.d script to get it running as a service. I put together a script that mostly worked seen here. The bug in this original script was that the service would start on boot/reboot, but it didn’t play well with the start/stop/restart/etc… commands. I went through more information and came up with the following. It works well for starting, stopping and restarting the server process, BUT the script fails if I add ${caddy_flags} to the start command. Example:
One other thing is that whether the script works or not, nothing prints in the command_msg where I have ${rc_pid}.
I’ve tried it several ways, but there is something that I am missing. Any suggestions would be welcome. Here’s the full script:
Code:
caddy_start() {
${daemon_cmd} ${daemon_cmd_args} ${command} ${caddy_flags}
}
One other thing is that whether the script works or not, nothing prints in the command_msg where I have ${rc_pid}.
I’ve tried it several ways, but there is something that I am missing. Any suggestions would be welcome. Here’s the full script:
Code:
#!/bin/sh
# PROVIDE: caddy
# REQUIRE: LOGIN
# KEYWORD: shutdown
# Documentation=https://caddyserver.com/docs
# add php-fpm to REQUIRE to run php/fastcgi processes
# Add the following lines to /etc/rc.conf to enable caddy:
#
# OPTION DEFAULTS
#
# caddy_enabl (bool): I set to Yes by default (26),
# because frankly, it's my script. :smile:
# caddy_user: caddysrv:caddysrv (user:group): www was unable to start
# caddy because it doesn't have a user/home directory.
# Assuming that is by design, $caddy_user serves this
# function [caddy start/stop/restart].
# caddy_bool (bool): true
# caddy_email (mailto): No default - set to your contact info
# caddy_config (path): My default: `/srv/www/Caddyfile`
# Set to your webserver PATH
# caddy_flags (str): -agree=true
# -email="you@mailserver.com"
# -conf=/your/webroot/path/Caddyfile
# -pidfile=/path/to/var/run/caddy.pid
. /etc/rc.subr
name="caddy"
rcvar=caddy_enable
# Set defaults
: ${caddy_enable:="YES"}
: ${caddy_user="caddysrv"}
: ${caddy_terms="true"}
: ${caddy_email="me@gmail.com"}
: ${caddy_config="/srv/www/Caddyfile"}
: ${caddy_flags="-agree=$caddy_terms -email=$caddy_email -conf=$caddy_config"}
pidfile="/srv/var/run/${name}.pid"
command="/usr/local/sbin/${name}"
daemon_cmd="/usr/sbin/daemon"
daemon_cmd_args="-cf -p ${pidfile} -u ${caddy_user}"
command_msg="Starting caddy with ${caddy_email} in directory ${caddy_config}. The PID is ${rc_pid}."
sig_stop="QUIT"
sig_reload="USR1"
start_cmd="${name}_start"
start_precmd="${name}_prestart"
caddy_prestart() {
touch ${pidfile}
chown ${caddy_user} ${pidfile} && chmod 775 ${pidfile}
}
caddy_start() {
${daemon_cmd} ${daemon_cmd_args} ${command}
echo ${command_msg}
}
load_rc_config "$name"
run_rc_command "$1"