FreeBSD Startup Script

1,947 views
Skip to first unread message

Shawn Mix

unread,
Sep 17, 2014, 10:28:13 AM9/17/14
to ope...@googlegroups.com
Are there any FreeBSD/scripting gurus who might be able to help provide some direction on getting a startup script created? I'm running this on a FreeNAS system with a port jail which runs a basic FreeBSD OS. I've functionally gotten OpenHAB to run, but it requires manual start/stop/restart by me from the command line. I'd like to get it into a daemon state preferably on startup/restart of the machine. Any help would be extremely appreciated.

Shawn Mix

unread,
Sep 19, 2014, 1:47:36 PM9/19/14
to ope...@googlegroups.com
So I think I'm making headway, but I don't quite have it where I want it. The code below is getting OpenHAB to start with the system startup. Unfortunately though, it just runs as "java" for the PID when I run TOP to view running processes. Additionally, if I kill the process (for testing updates and changes etc) I can't "restart" it like a daemon. My goal is to have it respond to the start/stop/restart commands, but currently it's not doing so. This at least gets it running for me on startup which is about 70% of what I need from it at this point. That last 30% though is what I'm trying to get to. Any help is greatly appreciated.

For anyone using this script for themselves, please note that I use "/opt/openhab-core/" in a few places, this should be replaced with the location for which you've installed the openhab folder.


Startup script in /etc/rc.d/openhab

#! /bin/sh
# PROVIDE: openhab
# REQUIRE: DAEMON NETWORKING
# KEYWORD: SHUTDOWN
#
# Short-Description: OpenHAB Daemon
cd "/opt/openhab-core"

. /etc/rc.subr
                                                                                               
name=openhab                                                                                   
rcvar=openhab_enable                                                                           
pidfile="/var/run/$name.pid"                                                                   
eclipsehome="/opt/openhab-core/server"                                                         
cp=$(find $eclipsehome -name "org.eclipse.equinox.launcher_*.jar" | sort | tail -1)            
httpport=8080                                                                                  
httpsport=8443                                                                                 
user=root                                                                                      
                                                                                               
command="/usr/local/openjdk7/bin/java"                                                         
command_args="-Djava.net.preferIPv4Stack=true -Dosgi.clean=true -Declipse.ignoreApp=true -Dosgi.noShutdown=true -Djetty.port=$httpport -Djetty.port.ssl=$httpsport -Djetty.home=. -Dlogback.configurationFile=configurat
                                                                                               
load_rc_config $name
run_rc_command "$1"

Shawn Mix

unread,
Oct 7, 2014, 10:11:03 AM10/7/14
to ope...@googlegroups.com
Just want to repost here and see if any FreeBSD scripting gurus out there would be able to help with this piece. I'm really in need of being able to start/stop/restart the service vs having to shutdown and restart the VM every time. The only reason restarting the VM is a pain, is that it resides on a FreeNAS box and I have to use that interface to start/stop the jails and it isn't always a pretty process unfortunately. Any help would be GREATLY appreciated.

Fredrik Dahlberg

unread,
Oct 11, 2014, 12:35:24 PM10/11/14
to ope...@googlegroups.com
Hi Shawn,

I threw this rc.d script together last night. I have only done some quick testing, but it seems to do the job.

Fredrik

#!/bin/sh
#
# $FreeBSD:$
#
# PROVIDE: openhab
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# Configuration settings for openhab in /etc/rc.conf:
# Mandatory:
# openhab_enable (bool):
#   Set to "NO" by default.
#   Set it to "YES" to enable openhab
#
# Optional:
# openhab_user (bool):
#   Set to "openhab" by default.
#
# openhab_http_port (num):
#   Default to 8080
#
# openhab_https_port (num):
#   Default to 8443
#
# openhab_telnet_port (num):
#   Default to 5555
#
# openhab_home (str):
#   Default to "/usr/local/openhab"
#

. /etc/rc.subr

name="openhab"
rcvar="${name}_enable"
pidfile="/var/run/${name}.pid"

load_rc_config "${name}"

: ${openhab_enable="NO"}
: ${openhab_user="openhab"}
: ${openhab_http_port=8080}
: ${openhab_https_port=8443}
: ${openhab_telnet_port=5555}
: ${openhab_home="/usr/local/openhab"}

required_files="${openhab_home}/configurations/openhab.cfg"

cp=$(find ${openhab_home}/server -name "org.eclipse.equinox.launcher_*.jar" | sort | tail -1);

java_command="/usr/local/bin/java
    -Dosgi.clean=true
-Declipse.ignoreApp=true
-Dosgi.noShutdown=true
-Djetty.port=${openhab_http_port}
-Djetty.port.ssl=${openhab_https_port}
-Djetty.home=${openhab_home}
-Dlogback.configurationFile=${openhab_home}/configurations/logback.xml
-Dfelix.fileinstall.dir=${openhab_home}/addons
-Djava.library.path=${openhab_home}/lib
-Djava.security.auth.login.config=${openhab_home}/etc/login.conf
-Dorg.quartz.properties=${openhab_home}/etc/quartz.properties
-Dequinox.ds.block_timeout=240000
-Dequinox.scr.waitTimeOnBlock=60000
-Dfelix.fileinstall.active.level=4
-Djava.awt.headless=true
-jar $cp -console ${openhab_telnet_port}"
 
command="/usr/sbin/daemon"
command_args="-p ${pidfile} -u ${openhab_user} -f ${java_command}"

start_precmd="pid_touch"
start_cmd="openhab_start"
stop_cmd="openhab_stop"
status_cmd="openhab_status"

pid_touch() {
    touch ${pidfile}
    chown ${openhab_user} ${pidfile}
}

openhab_start() {
cd ${openhab_home}
    echo "Starting ${name}..."
    exec ${command} ${command_args}
}

openhab_stop() {
    rc_pid=$(openhab_check_pidfile ${pidfile})
    if [ -z "${rc_pid}" ]; then
        [ -n "${rc_fast}" ] && return 0
        echo "${name} not running? (check ${pidfile})."
        return 1
    fi
    echo "Stopping ${name}..."
    kill -KILL ${rc_pid} 2> /dev/null && echo "Killed."
    rm -f ${pidfile}
sleep 1
}
 
openhab_status() {
    rc_pid=$(openhab_check_pidfile $pidfile)
    if [ -n "${rc_pid}" ]; then
        echo "${name} is running as pid ${rc_pid}."
    else
        echo "${name} is not running."
        return 1
    fi
}

openhab_check_pidfile() {
    _pidfile=$1
    if [ -z "${_pidfile}" ]; then
        err 3 'USAGE: openhab_check_pidfile pidfile'
    fi
    if [ ! -f ${_pidfile} ]; then
        debug "pid file (${_pidfile}): not readable."
        return
    fi
    read _pid _junk < ${_pidfile}
    if [ -z "${_pid}" ]; then
        debug "pid file (${_pidfile}): no pid in file."
        return
    fi
    if [ -n "`/usr/local/bin/jps -l | grep -e "^${_pid} ${cp}\$"`" ]; then
        echo -n ${_pid}
    fi
}

run_rc_command "$1"

Shawn Mix

unread,
Oct 12, 2014, 10:32:45 AM10/12/14
to ope...@googlegroups.com
Fredrik - THANK YOU!!! I haven't gotten to copy this down and test, but will do so later this afternoon and let you know how my testing goes. I'm very hopeful this works as expected. I'm not very good with FreeBSD as stated, so someone giving me this frame helps me understand how to do this in the future as well. I wasn't really sure (mainly) how to get the PID created and tracked. This makes sense, and I have a good feeling it will work perfectly. 

PS - I jumped for joy and acted like a giddy little kid for a few moments when I first saw your reply! :)

Fredrik Dahlberg

unread,
Oct 12, 2014, 4:34:10 PM10/12/14
to ope...@googlegroups.com

Shawn,

I’m glad that I could help you. But to be totally honest I wrote the script for myself. :-) I will also run OpenHAB on FreeNAS so I need it as much as you do. A tip for the next time you need something like this is to look for similar stuff already out there. My script is mostly based on the startup script for Tomcat. Let me know if it works out ok for you.

Shawn Mix

unread,
Oct 13, 2014, 10:38:52 PM10/13/14
to ope...@googlegroups.com
No shame in doing it for yourself, I would have been in the same boat if I had done this for you. :)

So I tried it out finally but I'm running into a bit of a conundrum. It seems to be acting just as it did previously with the bit of scripting I had, though it's actually not recognizing that openhab is a service that is running and/or that openhab is even a service in /etc/rc.d/. The service is clearly running as I can access the server and if I run 'top' I can see the Java process running. For some reason though I'm not able to get it to be recognized as a service on the box.

I backed up my current openhab script file in /etc/rc.d/openhab, copied to openhab.backup. Created a new openhab file with the script as you had above. Some minor tweaks to add in the IPv4 issues I've faced and adapted the location for the OpenHAB server files. I did create a new users to mimic your openhab user, though I've also tried running with root as well. It seems I must be hitting something mildly different that is causing an issue being picked up as a service.

What kind of Jail did you use on FreeNAS if I may ask? I know there are some variations on what runs and how it runs in the different jail types. I'm a bit perplexed as to what could be erroring out on me.

Shawn Mix

unread,
Oct 13, 2014, 10:59:52 PM10/13/14
to ope...@googlegroups.com
Ok, going to retract some earlier statement. I've got it working, and from the basic test of restarting the Jail, checking status on service, restarting, it looks to be working. 

The modifications I made (not sure 100% which one solved it, but I'm going to guess it was the user mainly): Modified the java path (I'm using the OpenJDK vs the Oracle install), changed user to root (I know it's not the best idea, but I just needed it to work for now), modified my working directory (in case the '-' was causing any issues in parsing the script), and did a chmod 555 on the openhab script in /etc/rc.d/.

It's looking golden and again Fredrik I can't thank you enough, regardless of your intent ;o)

Fredrik Dahlberg

unread,
Oct 14, 2014, 12:28:19 PM10/14/14
to ope...@googlegroups.com
I'm happy to hear that you got it running. I will give you some answers / tips even if you don't want them anymore. :-)

It's normally a really big no-no to run daemons as root. But it doesn't really matter that much in a small home server that does not expose any services to the internet.

But here's how you create/configure a non-privileged user named openhab: 
1. There is no /home directory by default so you will have to create it.
Enter code here...# mkdir /home
2. Create the openhab group
Enter code here...# pw groupadd openhab
3. Create the openhab user
# pw useradd -n openhab -c "openhab user" -g openhab -m  -s /usr/sbin/nologin
4. Change user and group ownership on the openhab directory and it's subdirectories
# chown -R openhab:openhab /path/to/openhab
5. Run the openhab rc.d script manually to test it. Important! When executing the openhab rc.d script you have to be root. 

Java:
There is no need to change the java executable path in the script unless you want to run OpenHAB with another java version than you normally use in your system. "/usr/local/bin/java" is a wrapper for your primary java installation. The reason for this wrapper is that you normally don't want to change your java executable path in your shell path or in every script everytime you change your java version. Instead you just change the java executable path in "/usr/local/etc/javavms" and you are done. Just type "java -version" at the command prompt and it will show you which java version that is default in your system.

FreeNAS jails:
I always run my jails as portjails with their own tcp/ip stacks. (VIMAGE=checked, vanilla=unchecked.)

OpenHAB rc.d script:
Normally you always put your own rc.d scripts in "/usr/local/etc/rc.d". But it's the same thing here as it is with running dameons as root, doesn't really matter in your case.

Happy OpenHABing,
Fredrik




Shawn Mix

unread,
Oct 14, 2014, 12:37:56 PM10/14/14
to ope...@googlegroups.com
Fredrik - I welcome the pointers/direction!! As I work in other jails on other projects, this helps clarify for me some of the basic constructs I've missed since getting started learning FreeBSD. Kind of dropped in head first and following tutorials where possible to get functioning apps. ;o)

I am aware of many of the best practices (like not running a daemon as root, putting scripts in /usr/local/, etc), but wasn't as concerned as you pointed since it's a small home based server. My goal was to get it running first, then slowly improve on what I'd done. This is the same for my rules that have been written. I'm sure many are done sloppily and/or in a way that have caused more work, but I plan to go back through and identify where I can improve and trim fat on my rules configs after I get it working properly in the first place. I really like the info on the java and the user pieces though, I wasn't as aware of some of the "necessary" steps and how the references worked. I'm not big on BSD/nix based systems, so its a learning curve. Definitely learning quite a bit as I go though, thanks for the direction!!

Rajil Saraswat

unread,
Aug 23, 2015, 8:50:07 AM8/23/15
to openhab
Unfortunately, this script is working only partially in a jail of my freenas system. The script does startup the openhab process but the status/stop commands are not working. The pid does exist for the process. Any idea what could be wrong?

#ps aux
root  
59558  0.0  0.0   12088   1632 ??  IsJ   5:46PM 0:00.00 daemon: /usr/local/bin/java[59559] (daemon)
hab  
59559  0.0  3.9 2352836 651476 ??  SJ    5:46PM 2:20.33 /usr/local/openjdk7/bin/java -Dosgi.clean=true -Declipse.ignoreApp=true -Dosgi.noShutdown=true -Djetty.port=8080 -Djetty.port.ssl=8443 -Djetty.home=/home/hab/runtime

#service openhab status
openhab
is not running.

#ls -la /var/run/openhab.pid
-rw-r--r--  1 hab  wheel  5 Aug 22 17:46 /var/run/openhab.pid

# /usr/local/bin/jps -l
59559 -- process information unavailable
1002 sun.tools.jps.Jps

Fredrik Dahlberg

unread,
Aug 29, 2015, 3:16:21 PM8/29/15
to openhab
Hi Rajil,

I'm not running OpenHab on FreeBSD anymore so unfortunately I can't really help you. But try to run "/usr/local/etc/openhab status" (or whatever your path is to your openhab rc file) instead of "service openhab status" to see if you get the same result. If you do get the same result as before I would start checking the "openhab_check_pidfile" function in the end of the rc script. Try copying that function to a new temporary script and mess around with it to see if you can get it to work.

Regards,
Fredrik Dahlberg
Reply all
Reply to author
Forward
0 new messages