RaspNXT - a standalone NXT environment for the Raspberry Pi
Thanks for your work davethetrousers!
I just modified your run_nxt.sh to be more raspian frendly with some log messages and to enable system startup and stop. It monitors the log file at start or stop of the server and logs it in the start/stop message. Just copy it to /etc/init.d and run "sudo update-rc.d run_nxt.sh defaults". It will start and stop in the runlevels and
it will wait on "stop" untill blockchain is saved (timeout 10m).
Please make sure to setup the variables like directorys and such.
This is tested only with 0.5.9 and if a next update changes the log output, this script have to be changed!
#! /bin/bash
### BEGIN INIT INFO
# Provides: nxt
# Required-Start: $local_fs $remote_fs $network $syslog $named $ntp $sshd
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start/stop NXT server
# Description: NXT protocol server
### END INIT INFO
if [ `id -u` != "0" ] && [ "$1" = "start" -o "$1" = "stop" ] ; then
echo "You must be root to start/stop nxt."
exit 1
fi
# Settings
nxtdir=/home/pi/nxt
nxt=start.jar
java=/usr/bin/java
nxtpid=/var/run/nxt.pid
nxtlog=/var/log/nxt.log
nxtuser=pi
nxtgroup=pi
nxtnice=6
timeout=600
# end Settings
# check for exist files and dirs
test -d $nxtdir || exit 0
test -f $nxtdir/$nxt || exit 0
test -f $java || exit 0
# make sure $nxtdir is ours!
chown -R $nxtuser:$nxtgroup $nxtdir
. /lib/lsb/init-functions
case "$1" in
start)
log_daemon_msg "Starting NXT server..." "nxt " || true
# check if server is running
if [ -e $nxtpid ]; then
pid=`cat $nxtpid`
var=`ps -p$pid | wc -l`
if [ $var -lt "2" ]; then
# clear logfile
echo -n > $nxtlog
else
echo -n "Server is already running!"
log_end_msg 0 || true
exit 0
fi
fi
# n minute timeout.
sleep $timeout &
timerPid=$!
# check log file and generate status-msg; will be killed when server is started or timeout hit
tail -q -n0 -F --pid=$timerPid $nxtlog 2> /dev/null | while read line; do
if echo $line | grep -q "Loading transactions..."; then
echo -n "Loading transactions..."
elif echo $line | grep -q "Loading blocks..."; then
echo -n "Loading blocks..."
elif echo $line | grep -q "Scanning blockchain..."; then
echo -n "Scanning blockchain..."
fi
if echo $line | grep -q "Exception"; then
echo -n "Java Exception! Blockchain corrupt? Killing process..."
log_end_msg 1 || true
kill -9 `cat $nxtpid` > /dev/null 2>&1
# stop the timer..
kill $timerPid > /dev/null 2>&1
fi
if echo $line | grep -q "started successfully."; then
echo -n "Server started!"
log_end_msg 0 || true
# stop the timer..
kill $timerPid > /dev/null 2>&1
fi
done &
# start server
if start-stop-daemon --start --name nxt --nicelevel $nxtnice --chuid $nxtuser:$nxtgroup --pidfile $nxtpid -m --chdir $nxtdir --exec $java >> $nxtlog -- -Xms250m -Xmx350m -jar $nxt STOP.PORT=7873 STOP.KEY=0815 2>&1 >> $nxtlog & then
# wait for the timer to expire (or be killed)
wait %sleep > /dev/null 2>&1
else
log_end_msg 1 || true
fi
;;
stop)
log_daemon_msg "Stopping NXT server..." "nxt " || true
# check if server is running
if [ -e $nxtpid ]; then
pid=`cat $nxtpid`
var=`ps -p$pid | wc -l`
if [ $var -lt "2" ]; then
echo -n "Server not running!"
log_end_msg 0 || true
exit 0
fi
else
echo -n "Server not running!"
log_end_msg 0 || true
exit 0
fi
# n minute timeout.
sleep $timeout &
timerPid=$!
# check log file and generate status-msg; will be killed when server is stopped or timeout hit
tail -q -n0 -F --pid=$timerPid $nxtlog 2> /dev/null | while read line; do
if echo $line | grep -q "Saved blocks.nxt"; then
echo -n ".saving blockchain files, this will take a while...Saved blocks.nxt..."
fi
if echo $line | grep -q "Saved transactions.nxt"; then
echo -n "Saved transactions.nxt..."
fi
if echo $line | grep -q "stopped."; then
echo -n "Server stopped!"
log_end_msg 0 || true
# stop the timer..
kill $timerPid
fi
done &
if start-stop-daemon --start --name nxt_stop --chdir $nxtdir --exec $java 2>&1 >> $nxtlog -- -jar $nxt --stop STOP.PORT=7873 STOP.KEY=0815 2>&1 >> $nxtlog & then
# wait for the timer to expire (or be killed)
wait %sleep > /dev/null 2>&1
rm $nxtpid > /dev/null 2>&1
else
log_end_msg 1 || true
fi
;;
restart)
$0 stop
sleep 1
$0 start
;;
*)
echo "usage:`basename $0` start | stop | restart"
exit 1
;;
esac
I hope its useful for someone. If someone finds bugs or something stupid, please let me know, I'm not an expert! ;o)
greets
eb