19 Jul 2014

I've hacked a bunch of scripts together to send the CPU load of my server to an Arduino with some LEDs. They require two bash sessions at once: one to keep the port open and one to poll "top" for the cpu load and send it to the port.At first I did not automate this, which means that after a reboot of the server (which occurs rarely), I would always forget to set it up again.

I'm using screen, because I want to be able to log in over ssh and see how each process is doing.I'm positive that it's possible to do this in a much more elegant way, but that wasn't really my priority. It's just to see if I could make the Arduino do something useful.

Since getting the script to work with multiple screen windows was quite challenging, I thought I would post it here:

#!/bin/bash

~/rekelbox/initPort.sh

# Wait 10 seconds to initialize
sleep 10

# Create screen session rekelbox (-S) without attaching (-dm)
screen -dmS rekelbox
sleep 1

# create a new window (window 1) from window 0
screen -S rekelbox -p 0 -X stuff "screen$(printf \\r)" # on a bash prompt instead of $(printf \\r) do ctrl+V enter, now ^M will show

# send "cat </dev/ttyUSB0" command to rekelbox, window 0
screen -S rekelbox -p 0 -X stuff "cat </dev/ttyUSB0$(printf \\r)"

# send "watch -n1 ./sendCpuPerc.sh" command to rekelbox, window 1
screen -S rekelbox -p 1 -X stuff "watch -n1 ~/rekelbox/sendCpuPerc.sh$(printf \\r)"