Skip to content

Instantly share code, notes, and snippets.

@apearson
Last active February 28, 2024 09:50
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save apearson/56a2cd137099dbeaf6683ef99aa43ce0 to your computer and use it in GitHub Desktop.
Save apearson/56a2cd137099dbeaf6683ef99aa43ce0 to your computer and use it in GitHub Desktop.
Node-RED FreeBSD RC Script
#!/bin/sh
#
# PROVIDE: nodered
# REQUIRE: LOGIN
# KEYWORD: shutdown
# Author: Andrew Pearson (apearson.io)
# Version: 1.0.2
# Description:
# This script runs Node-RED as a service under the supplied user on boot
# How to use:
# Place this file in /usr/local/etc/rc.d/
# Add nodered_enable="YES" to /etc/rc.config
# (Optional) To run as non-root, add nodered_runAs="node-red" to /etc/rc.config
# (Optional) To pass Node-RED args, add nodered_args="" to /etc/rc.config
# Freebsd rc library
. /etc/rc.subr
# General Info
name="nodered" # Safe name of program
program_name="node-red" # Name of exec
title="Node-RED" # Title to display in top/htop
# RC.config vars
load_rc_config $name # Loading rc config vars
: ${nodered_enable="NO"} # Default: Do not enable Node-RED
: ${nodered_runAs="root"} # Default: Run Node-RED as root
# Freebsd Setup
rcvar=nodered_enable # Enables the rc.conf YES/NO flag
pidfile="/var/run/${program_name}.pid" # File that allows the system to keep track of node-red status
# Env Setup
export HOME=$( getent passwd "$nodered_runAs" | cut -d: -f6 ) # Gets the home directory of the runAs user
# Command Setup
exec_path="/usr/local/bin/${program_name}" # Path to the node-red exec, /usr/local/bin/ when installed globally
output_file="/var/log/${program_name}.log" # Path to Node-RED output file
# Command
command="/usr/sbin/daemon"
command_args="-r -t ${title} -u ${nodered_runAs} -o ${output_file} -P ${pidfile} ${exec_path} ${nodered_args}"
# Loading Config
load_rc_config ${name}
run_rc_command "$1"
@jprovan
Copy link

jprovan commented Jun 27, 2022

Well, I am sorry to have bothered you. I retraced my steps and it is working properly. Thank you for responding so quickly.

@apearson
Copy link
Author

@jprovan No worries! Glad it's working

@kitten77
Copy link

kitten77 commented Aug 25, 2022

is it just me or am I missing something, because as far as I can tell node-red is running as root.
That surely can't be right, can it?

never mind, found the rc.conf setting I was missing.

@jprovan
Copy link

jprovan commented Aug 29, 2022 via email

@apearson
Copy link
Author

is it just me or am I missing something, because as far as I can tell node-red is running as root. That surely can't be right, can it?

never mind, found the rc.conf setting I was missing.

Glad you found the rc.conf setting. Is there anything I could to make that setting more visible?

@kitten77
Copy link

kitten77 commented Sep 7, 2022

is it just me or am I missing something, because as far as I can tell node-red is running as root. That surely can't be right, can it?
never mind, found the rc.conf setting I was missing.

Glad you found the rc.conf setting. Is there anything I could to make that setting more visible?

Could make it default so that you only have to change the user if you don't want it to run as user nodered.

@RanceH
Copy link

RanceH commented Jan 9, 2023

This is in reply to @kitten77.

I don't think its a good idea to change the default user in a script like this, since the Freebsd install instructions don't require the creation of a non-root user. While this is good practice and highly recommended, there is no guarantee that each of us will create that non-root user the same way, thus breaking the script and causing more work for @apearson.

@apearson
Copy link
Author

apearson commented Jan 9, 2023

I agree with @RanceH on this. We can't depend on the nodered_runAs user being created before and the people creating the user will be the ones to read the comment and know how to change the nodered_runAs user

@EliasZoockt
Copy link

Hello, can someone help me instaling it on TrueNAS core 13.1?

@apearson
Copy link
Author

What have you tried @EliasZoockt?

@EliasZoockt
Copy link

I tried to run the script and than to understand the comments but i dont know what i have to do.

@apearson
Copy link
Author

  1. Did you Place this file in /usr/local/etc/rc.d/
  2. Then Add nodered_enable="YES" to /etc/rc.config
  3. Then service start nodered

@EliasZoockt
Copy link

how do i put the file in the path with the shell?

@apearson
Copy link
Author

@EliasZoockt email me at noderedhelp at {myusername} dot io

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment