I'm running a shell script on a raspberry pi to communicate with a arduino. Every hour my ram gets used up by about 50 megabytes more.
The script itself just connects to ttyACM0 (the arduino's usb terminal) and then sends a char to it. The script runs once per minute to check the Temps. Values for the first argument can be: 'a', 'b' or 'T'. In the case of 'a' or 'b' it switches on/off a relay. In the case of 'T' the arduino returns 3 temperature values, which I store.
The script runs fine (I can control the relay and receive values, without the arduinos serial getting restarted). but I'm having issues because of my limited memory.
I don't have any other processes running on the device and already tried a fresh raspbian setup.
Can somebody tell me where and if there are memory leaks in this script and how I can prevent them?
EDIT: I found the culprit: the script runs hundreds of 'cat' commands with time, how do I get rid of them? I already tried killall cat, but this resets my raspberrys serial connection (I want to prohibit that, because it 'restarts' the arduino!) @mikeserv pointed me towards using head, which automatically quits after reading a defined number of lines, unfortunately that doesn't work either. And I can't seem to get the temperatures written in my output file without a pipe
head -n3 <&3 >>/home/pi/output
doesn't work as it's not exiting too and I don't get any output
I could kill all cat's every few minutes to free the mem, but this also resets my tty to arduino (so it reboots and loses the state for the relay)
Edit2: I did not manage to get this working (I tried several possibilities including minicom, screen etc...) but the receiving part is where I have trouble. Sending chars to the arduino works fine though!
#!/bin/bash # READ / WRITE ARDUINO exec 3<> /dev/ttyACM0 echo "connected, sleep for 1 sec..." sleep 1 echo "send $1..." echo "$1" >&3 if [ "$1" = "T" ] then cat <&3 | cat >> /home/pi/output &2>1 else echo "nothing to save" fi echo "closing.." exec 3>&- exit 0