Solved rc.d script for caddy server - not quite working

justrjlewis

New Member


Messages: 8

I followed the tutorial for practical rc.d scripting to enable caddy web server on my DO FreeBSD 10.2 droplet. It's very simple, which is probably why it's not quite working as expected. So let me start by posting my questions, then I'll show my code.

  1. The pre-commit check list says: 2.Does the file have a $FreeBSD$ tag? -- no clue what that is...?? Google searching led to something about VLAN, but I couldn't wrap my head around what that was either, or how to apply it to my caddy.in file.
  2. I was trying to find/think of a way to get the service start to run as the user rather than root, but I couldn't configure that in such a way as to work. I tried putting su in the command. One thing I didn't try, but thought of, was using some type of prestart function, but wasn't really sure where to go with that. 6.28 Says something about specifying the users and groups in the Makefile, but I wasn't clear on what to do with the whole makefile thing so I just added the caddy binary and my script file to the path and edited the rc.conf. I get that that's not exactly what was supposed to happen, but I'm not sure how to proceed. Any help here would be great.
  3. All that being said, my service does sort of work. Bugs?
  1. It doesn't run in the background so nothing happens after the server starts and you can't ssh into the server without stopping it so that the OS can finish its' tasks...not exactly what I was going for.
  2. Again - service is running as the root user.
I think those are all of my questions at the moment.

Here's my script (I couldn't use %%PREFIX%% since I didn't install it):

Code:
#!/bin/sh

# PROVIDE: caddy
# REQUIRE: LOGIN
# KEYWORD: shutdown

# Documentation=https://caddyserver.com/docs
# php-fpm is required if you're going to run php/fastcgi processes

# Add the following lines to /etc/rc.conf to enable caddy:
#
# caddy_enabl (bool):   I set to Yes by default (19),
#                       because frankly, it's my script. :smile:
# caddy_config (path):  Set to `/srv/www/Caddyfile` - my default.
#                       Set to your webserver PATH
# caddy_flags (str):    -agree=true
#                       -conf=/your/webroot/path/Caddyfile

. /etc/rc.subr

name="caddy"
rcvar=caddy_enable

load_rc_config "$name"

: ${caddy_enable:="YES"}
: ${caddy_config="/srv/www/Caddyfile"}
: ${caddy_flags="-agree=true -conf=$caddy_config"}

command="/usr/local/sbin/${name}"

run_rc_command "$1"
 

leebrown66

Well-Known Member

Reaction score: 175
Messages: 445

Are you writing a port, or are you just trying to get an rc script working at startup? You reference the Practical rc.d scripting in BSD guide, but then start quoting from the Porters Handbook, so it's a little confusing. If you already have your application installed and just want to get an rc script going, ignore the Porters Handbook. That's the assumption I'm going with here.

To run a command as another user, typically you would su -l myuser /usr/bin/whoami which executes the command /usr/bin/whoami as user myuser, BUT as you want it to run in the background ...

In an rc script, to run something in the background, use daemon(8), something like
Code:
command="/usr/sbin/daemon -c -f -u myuser -p $pidfile /usr/local/sbin/${name}"
Note the use of a pidfile (re-read the Practical rc.d scripting) which will be required to stop it later on; daemon(8) will also change the user, so no need to bother with su(1). An example can be seen here.

Hopefully that should get you going.
 
OP
justrjlewis

justrjlewis

New Member


Messages: 8

Thank you so much! That's kind of where I myself got confused... When the Practical scripting article starts talking about connecting your script to the rc.d framework(pg 7), the opening paragraph kind of led me off on a tangent that had me going "?". I will get re-reading/scripting; steer clear of the port stuff and see what happens.
 

SirDice

Administrator
Staff member
Administrator
Moderator

Reaction score: 13,042
Messages: 39,676

leebrown66 already gave an excellent answer. Just wanted to add to this:
The pre-commit check list says: 2.Does the file have a $FreeBSD$ tag? -- no clue what that is...?? Google searching led to something about VLAN, but I couldn't wrap my head around what that was either, or how to apply it to my caddy.in file.
You can safely skip this bit. It's a specific keyword that inserts a line into the file. It can be used for example to get a revision number. Subversion will automatically replace $FreeBSD$ with something like this:
Code:
$FreeBSD: head/net/openntpd/files/openntpd.in 384833 2015-04-27 13:58:14Z naddy $
If you don't submit your code you don't have to add it.
 
OP
justrjlewis

justrjlewis

New Member


Messages: 8

oh! That's apart of my "ports" road trip, but I do recall seeing that line on php-fpm and the other packaged scripts.
Thank you as well. I'm enjoying learning to work with FreeBSD.

I got the service working and made a gist for it on github.

Here's the actual code at this time for anyone who wants a peek:

Code:
#!/bin/sh

# PROVIDE: caddy
# REQUIRE: LOGIN
# KEYWORD: shutdown

# Documentation=https://caddyserver.com/docs
# add php-fpm to REQUIRE for php/fastcgi processes

# Add the following lines to /etc/rc.conf to enable caddy:
#
# caddy_enabl (bool):   I set to Yes by default (26), 
#                       because frankly, it's my script. :smile:
# caddy_flags (str):    -agree=true
#                       -email="you@mailserver.com" 
#                       -conf=/your/webroot/path/Caddyfile
# 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].

#Opts that need to be set in THIS file:
# caddy_email (mailto): Default: me@gmail.com
# caddy_config (path):  My default: /srv/www/Caddyfile
#                       Set to your webserver PATH 

. /etc/rc.subr

name="caddy"
rcvar=caddy_enable
load_rc_config "$name"

# Set defaults
: ${caddy_enable:="YES"}
: ${caddy_email="me@gmail.com"}
: ${caddy_config="/srv/www/Caddyfile"}
: ${caddy_flags="-agree=true -email="$caddy_email" -conf=$caddy_config"}
: ${caddy_user="caddysrv"}

pidfile="/var/run/${name}.pid"
sig_stop="QUIT"
sig_reload="USR1"

command="/usr/local/sbin/${name}"
start_cmd="${name}_start"

caddy_start() {
  echo "Starting caddy server."
  /usr/sbin/daemon -cf -p ${pidfile} -u ${caddy_user} ${command} ${caddy_flags}
  echo "All done!"
}

run_rc_command "$1"
 

leebrown66

Well-Known Member

Reaction score: 175
Messages: 445

Congratulations! Glad you got it working to your satisfaction and that you are enjoying FreeBSD.

You can mark this thread solved using the "Thread" drop-down at the top right IIRC.
 
Top