Ylianst / MeshCentral

A complete web-based remote monitoring and management web site. Once setup you can install agents and perform remote desktop session to devices on the local network or over the Internet.
https://meshcentral.com
Apache License 2.0
4.34k stars 579 forks source link

OpenBSD rc.d script #6396

Closed ivomarino closed 2 months ago

ivomarino commented 2 months ago

I've created this small rc.d script in order to run meshcentral on OpenBSD as _meshcentral user:

cat /etc/rc.d/meshcentral
#!/bin/ksh

daemon="/usr/local/bin/node /usr/local/meshcentral/node_modules/meshcentral/meshcentral.js --launch"
daemon_logger="daemon.info"
daemon_execdir="/usr/local/meshcentral"
daemon_user="_meshcentral"

. /etc/rc.d/rc.subr

rc_bg=YES
rc_reload=NO

rc_cmd $1

it actually works but crashes after some time with this error log:

Sep 20 10:35:49 stantz meshcentral[63100]: node:events:495
Sep 20 10:35:49 stantz meshcentral[63100]:       throw er; // Unhandled 'error' event
Sep 20 10:35:49 stantz meshcentral[63100]:       ^
Sep 20 10:35:49 stantz meshcentral[63100]:
Sep 20 10:35:49 stantz meshcentral[63100]: Error: read EIO
Sep 20 10:35:49 stantz meshcentral[63100]:     at TTY.onStreamRead (node:internal/stream_base_commons:217:20)
Sep 20 10:35:49 stantz meshcentral[63100]: Emitted 'error' event on ReadStream instance at:
Sep 20 10:35:49 stantz meshcentral[63100]:     at emitErrorNT (node:internal/streams/destroy:151:8)
Sep 20 10:35:49 stantz meshcentral[63100]:     at emitErrorCloseNT (node:internal/streams/destroy:116:3)
Sep 20 10:35:49 stantz meshcentral[63100]:     at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
Sep 20 10:35:49 stantz meshcentral[63100]:   errno: -5,
Sep 20 10:35:49 stantz meshcentral[63100]:   code: 'EIO',
Sep 20 10:35:49 stantz meshcentral[63100]:   syscall: 'read'
Sep 20 10:35:49 stantz meshcentral[63100]: }
Sep 20 10:35:49 stantz meshcentral[63100]:
Sep 20 10:35:49 stantz meshcentral[63100]: Node.js v18.19.1

when I run the whole process from /etc/rc.local in a tmux session like this:

cat /etc/rc.local
#!/bin/sh

tmux new-session -d -s meshcentral "doas -u _meshcentral sh -c 'cd /usr/local/meshcentral && /usr/local/bin/node /usr/local/meshcentral/node_modules/meshcentral/meshcentral.js --launch'"

exit 0

all works fine and the process remains stable, any suggestions? Thanks

si458 commented 2 months ago

I have actually just merged into the repo a freebsd script which u can install/uninstall etc!

If u want to try do npm install Ylianst/MeshCentral

This will install the development version

Then u can run node node_modules/meshcentral --install to install --uninstall to uninstall, --start to start, etc...

Please can u try that?

Make sure u run npm and node commands with whatever user u want meshcentral to run as!?

ivomarino commented 2 months ago

Thanks @si458, is it compatible with OpenBSD? rc scripts are bit different between the two. Any link to the commit? Thanks

si458 commented 2 months ago

@ivomarino oh right erm no idea!? I always through freebsd and openbsd are the same! What iso/site did u use so I can create a vm for openbsd and test?

ivomarino commented 2 months ago

What iso/site did u use so I can create a vm for openbsd and test?

https://cdn.openbsd.org/pub/OpenBSD/7.5/amd64/install75.img

tschettervictor commented 2 months ago

Thought I'd throw this in here. This script works for me. Make sure the user you are running as has ownership of the meshcentral directories, and the meshcentral_chdir should be the path to you meshcentral directory.

Link to the actual install script is https://github.com/tschettervictor/bsd-apps/blob/main/meshcentral

#!/bin/sh

# MeshCentral FreeBSD Service Script

# PROVIDE: meshcentral
# REQUIRE: NETWORKING
# KEYWORD: shutdown

. /etc/rc.subr

name=meshcentral
desc="MeshCentral Computer Management"
rcvar=meshcentral_enable

load_rc_config $name

: ${meshcentral_enable:="NO"}
: ${meshcentral_args:=""}
: ${meshcentral_chdir:="/usr/local/meshcentral"}
: ${meshcentral_daemon_user:="meshcentral"}
: ${meshcentral_daemon_group:="meshcentral"}

pidfile="/var/run/${name}/${name}.pid"
node="/usr/local/bin/node"
command="/usr/sbin/daemon"
command_args="-u ${meshcentral_daemon_user} -P ${pidfile} -H -o /var/log/${name}/${name}.log ${node} node_modules/${name} ${meshcentral_args}"

start_precmd="meshcentral_startprecmd"

meshcentral_startprecmd()
{
    if [ ! -d /var/run/${name} ]; then
        install -d -o ${meshcentral_daemon_user} -g ${meshcentral_daemon_group} /var/run/${name};
    else
        chown -R ${meshcentral_daemon_user}:${meshcentral_daemon_group} /var/run/${name};
    fi
    if [ ! -d /var/log/${name} ]; then
        install -d -o ${meshcentral_daemon_user} -g ${meshcentral_daemon_group} /var/log/${name};
    else
        chown -R ${meshcentral_daemon_user}:${meshcentral_daemon_group} /var/log/${name};
    fi
}

run_rc_command "$1"

One thing to note is the meshcentral_daemon_user variable. This is necessary because meshcentral will not run properly if that variable is called meshcentral_user unless that user is root. This way it allows the daemon command to be run as root, but the actual meshcentral process to run as the specified user. The user you run as also should be created by pw user add meshcentral -c meshcentral -u 6374 -s /usr/sbin/nologin -d /home/meshcentral -m as I believe it needs a home directory to install NPM modules. (not 100% sure on this last point)

si458 commented 2 months ago

the new script was included in the 1.1.31 release so give it a try, make sure to run the commands as whatever user you want meshcentral to run as node node_modules/meshcentral --install node node_modules/meshcentral --uninstall node node_modules/meshcentral --start node node_modules/meshcentral --stop node node_modules/meshcentral --restart