Hello everyone,
I am an OpenBSD user and I recently installed Node.js. I managed to
create an rc.d script to start a basic Node.js application, but it only
starts manually.
At OpenBSD boot, I can see that the application is started, but it
doesn't work. I have the impression that it is related to the
node_modules directory, because after inspection, the JavaScript file
seems to stop at the line :
const express = require("express");
It cannot load the express module.
Here are the full details so that you can reproduce the same error on
your side:
OpenBSD version: 7.3 (release + syspatch)
Node version: 18.15.0
npm version: 9.5.0
Let's create a basic node.js app:
mkdir /var/www/htdocs/app
cd /var/www/htdocs/app/
npm init -y
npm install express
touch server.js
chown -R www.www /var/www/htdocs/app
chmod -R 775 /var/www/htdocs/app
/var/www/htdocs/app/server.js file:
--8<--
const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.send("Hello from OpenBSD");
});
app.listen(80, () => {
console.log("Server started on port 80");
});
--EOF--
You can run `node server.js` to test the url: http://your-ip
Ctrl-c to quit
Now let's create the /etc/rc.d/app script:
--8<--
#!/bin/ksh
daemon="/usr/local/bin/node"
daemon_execdir="/var/www/htdocs/app"
daemon_flags="server.js"
daemon_logger="daemon.info"
#daemon_rtable=0
#daemon_timeout=30
#daemon_user=root
. /etc/rc.d/rc.subr
#pexp="$(eval echo ${daemon}${daemon_flags:+ ${daemon_flags}})"
rc_bg="YES"
rc_reload="NO"
#rc_reload_signal=HUP
#rc_stop_signal=TERM
#rc_usercheck= # (undefined or "NO")
#rc_configtest() {
#}
#rc_pre() {
#}
#rc_start() {
# rc_exec "${daemon} ${daemon_flags}"
#}
#rc_check() {
# pgrep -T "${daemon_rtable}" -q -xf "${pexp}"
#}
#rc_reload() {
# pkill -${rc_reload_signal} -T "${daemon_rtable}" -xf "${pexp}"
#}
#rc_stop() {
# pkill -${rc_stop_signal} -T "${daemon_rtable}" -xf "${pexp}"
#}
#rc_post() {
#}
rc_cmd $1
--EOF--
chmod +x /etc/rc.d/app
rcctl enable app
Start the app: rcctl start app
Check in the browser or use: netstat -anf inet | grep 80
Stop the app: rcctl stop app
Check that it is not anymore started: pgrep node => nothing
- Reboot -
If I do a `dmesg -s`, I can see the following line:
starting package daemons: app.
No errors, no output, even in the /var/log/daemon log.
I would be very grateful for any help or advice that you can provide.
Sincerely,
-- Remi
"Everyone makes mistakes, and the best way to learn is from our
mistakes." - Henry Ford