rc.d script for Node.js application reddit / freebsd ---------------------------------------------------------------------- KawabungaXDG · 2022-11-21 04:03 Hey, y'all! Recently, I moved my media server from Debian to FreeBSD for learning purposes. It's been a great ride: the FreeBSD documentation is very helpful. I am having a little bit of trouble trying to put together my first rc.d script and would be quite happy if you guys could give me a hand. The task is quite simple: I would like to run [this](https://github.com/sct/overseerr) Node.js based application as a supervised service. Basically, calling a `yarn start` as a specific user from a specific directory. Here is what I got so far: #!/bin/sh # # PROVIDE: overseerr # REQUIRE: networking # KEYWORD: . /etc/rc.subr name="overseerr" desc="Overseerr daemon" rcvar="overseerr_enable" overseerr_chdir="/usr/local/${name}" overseerr_user="${name}" overseerr_command="/usr/local/bin/yarn" overseerr_args="--cwd ${overseerr_chdir} start" pidprefix="/var/run/${name}" pidfile="${pidprefix}/${name}.pid" command="/usr/sbin/daemon" command_args="-P ${pidfile} -R 10 -f -u ${overseerr_user} ${overseerr_command} ${overseerr_args}" mkdir -m 777 -p $pidprefix load_rc_config $name : ${overseerr_enable:=no} run_rc_command "$1" I am not quite sure if this is the right way to do it. I looked at some sample scripts for Node.js apps on Github, they do look similar, but mine refuses to work. After barely managing to capture some logs while running it attached to my current shell session, I figured out that I need to somehow change the service working directory to `/usr/local/${name}`. Something similar to systemd's unit file `WorkingDirectory`. **TL;DR:** How to change a rc.d script working directory? Is there something similar to systemd's `journalctl` for init services? TIA. ---------------------------------------------------------------------- celestrion · 2022-11-21 07:10 They're shell scripts, so the `cd` command will change the directory. Historically, it's the daemon's job to change to a sensible directory (`/tmp`, `/var/tmp`, or simply `/`) early in its runtime, not the enclosing environment's job. ---------------------------------------------------------------------- timvandijknl · 2022-11-21 09:34 You could just use `py39-supervisor`, I use it to run multiple minecraft servers, for example. You just tell it which command to run, which user and which "home folder", and it will do it. [http://supervisord.org/](http://supervisord.org/) ---------------------------------------------------------------------- jozefvanerka · 2022-11-21 14:54 Either this or one can use `forever` npm package and use a script from Randy: #!/bin/sh # Copyright (c) 2015, Randy Westlund. All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. # This is a FreeBSD rc script for starting a node process and keeping it alive. # It requires forever.js to be installed: 'npm install -g forever'. # In addition to node_enable, the following rc variables should be defined: # node_msg The name of your program, printed at start. Defaults to "node". # node_dir The directory where your node files live. Must be defined. # node_logdir The directory for logfiles. Defaults to ${node_dir}/logs. # node_user Passed to noded process as process.env.USER. Defaults to "www". # node_group Passed to noded process as process.env.GROUP. Defaults to "www". # node_user and node_group may be used to drop node's root # privileges after binding to ports. # node_app Application main script. Defaults to "/bin/www" (relative # to node_user's home # node_forever forever binary file path. Defaults to "/usr/local/bin/forever". # node_local_forever use local forever binary # (ie. node_user's home/node_modules/.bin/forever) # node_forever_log forever log file. Defaults to /var/log/forever.log. # PROVIDE: node # REQUIRE: LOGIN mongod # KEYWORD: shutdown . /etc/rc.subr name="node" rcvar="${name}_enable" start_precmd="${name}_prestart" start_cmd="${name}_start" stop_cmd="${name}_stop" # node executable command="/usr/local/bin/${name}" pidfile="/var/run/node/${name}.pid" # forever needs a path for each command PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr /local/bin:/root/bin # get rc vars load_rc_config $name : ${node_enable:="no"} : ${node_msg:="node"} : ${node_logdir:="${node_dir}/logs"} : ${node_user:="www"} : ${node_group:="www"} : ${node_app:="/bin/www"} : ${node_forever:="/usr/local/bin/forever"} : ${node_local_forever:="no"} : ${node_forever_log:="/var/log/forever.log"} case ${node_local_forever} in [Yy][Ee][Ss]) node_forever="/home/${node_user}/node_modules/.bin/forever" ;; *) ;; esac # make sure we're pointing to the right place required_dirs="${node_dir}" required_files="${node_dir}/${node_app}" # any other checks go here node_prestart() { echo "$node_msg starting" } node_start() { # pass USER and GROUP to node so it can drop privs NEW_USER="${node_user}" \ NEW_GROUP="${node_group}" \ su -m ${node_user} -c "${node_forever} start -a -l ${node_forever_log} -o ${node_logdir}/node.log \ -e ${node_logdir}/node_err.log --minUptime 3000 --pidFile ${pidfile} \ --workingDir ${node_dir} --uid ${node_user} ${node_dir}/${node_app}" } node_stop() { # kill node nicely -- node should catch this signal gracefully su -m ${node_user} -c "${node_forever} stop --killSignal SIGTERM `cat ${pidfile}`" } run_rc_command "$1" ---------------------------------------------------------------------- jozefvanerka · 2022-11-21 14:55 Oh and for this, \`/etc/rc.conf\` entries are needed: ```# nodeapp1 node_enable="YES" node_msg="nodeapp1" node_dir="/home/nodeapp1" node_chdir="/home/nodeapp1" node_user="nodeapp1" node_group="nodeapp1" node_app="/nodeapp1/index.js" node_forever_log="/home/nodeapp1/log/forever.log" ``` ---------------------------------------------------------------------- KawabungaXDG · 2022-11-21 18:16 Yeah, systemd's unit file philosophy is so entrenched in my head... It is hard to think services as scripts after all these years! Back to the main topic: if I slap a `cd /usr/local/${name}` before the end of the script, `daemon` will call `yarn start` from said directory? If I understood your second sentence correctly, this should be the right way to do it, instead of appending a `cd` to the final `command` string. ---------------------------------------------------------------------- celestrion · 2022-11-21 18:39 It should. But, reading through `/etc/rc.subr` tells me that your `overseerr_chdir` variable ought to already be doing that. Maybe temporarily replace the command to point to a script like this: #!/bin/sh (set ; pwd) | logger And verify that the environment and working directory (which will be dumped to `/var/log/messages`) look like you expect when the service starts. ---------------------------------------------------------------------- scoobybejesus · 2022-11-22 14:45 Check out the rc file for Vaultwarden. It uses a chdir, and iirc it doesn't include it in the command at all. ---------------------------------------------------------------------- KawabungaXDG · 2022-11-22 16:15 Yeah, you are absolutely right! After a little bit of RTFM, I finally understood how `${name}_chdir` works (and other variables described in [rc.subr(8)](https://www.freebsd.org/cgi/man.cgi?rc.subr(8)) manual). My mistake was passing `--cwd` to `yarn`, asking it to change its working directory after the system had already changed it through `${name}_chdir` variable. Thank you!