System Uptime Info and an Automatic Reboot

I am curious how you would (automatically) retrieve the uptime of the openHAB (java) process, since the SystemInfo binding does not let you determine a process id. The SystemInfo binding does give you the computer uptime, however.

For my purposes (display only, no scheduled reboots) I have both the computer uptime through the SystemInfo binding and the uptime of the openHAB process (usually shorter, since I tend to restart the openhab2.service every now and then) through the Exec binding.

uptime.sh

#!/bin/sh

PID=`ps aux --sort=start_time | grep openhab.*java | grep -v grep | awk '{print $2}' | tail -1`
UPTIME=`ps -o etimes= -p "${PID}"`
echo $UPTIME

demo.things

Thing exec:command:uptime [command="/path/to/uptime.sh", interval=10, timeout=2]

duration_seconds.js

// computes nicely formatted duration from given minutes
(function(i){

	// seconds to minutes
	i = Math.floor(i / 60)

	var d = Math.floor(i / (24 * 60));
	var h = Math.floor((i / 60) - (24 * d));
	var m = Math.round(i - 60 * (24 * d + h));
	var result = '';

	// days
	if (d > 0) {
		result = result + d;
		if (d == 1) {
			result = d + ' day';
		} else {
			result = d + ' days';
		}
	}

	// hours
	if (h > 0) {
		if (result != '') {
			result = result + ', ';
		}
		result = result + h;
		if (h == 1) {
			result = result + ' hour';
		} else {
			result = result + ' hours';
		}
	}

	// minutes
	if (m > 0) {
		if (result != '') {
			result = result + ', ';
		}
		result = result + m;
		if (m == 1) {
			result = result + ' minute';
		} else {
			result = result + ' minutes';
		}
	}

	return result;
})(input)

demo.items

// Number type does not yet work for exec binding
String System_openHAB_Uptime "openHAB uptime [JS(duration_seconds.js):%s]" <clock> (System) { channel="exec:command:uptime:output" }