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
cat <&3 | cat >> /home/pi/output &2>1
results in backgroundedcat
commands... – Sep 27 '14 at 19:26cat <&3 >>/home/pi/output
? this is also portable - so why not use a shell much less likely to hog resources - maybe#!/bin/dash
? – mikeserv Sep 27 '14 at 19:36"$1"
- it looks like this may be a candidate for awhile getopts
loop. But I don't understand how - or why - it receives the args. – mikeserv Sep 27 '14 at 19:41cat <&3 | cat >> /dev/null
, i.e., force the buffer into the trash? – eyoung100 Sep 27 '14 at 19:52$1
a1
? I think I might understand. Thecat </dev/ttyACM0 |cat >file
just hangs reading the tty - it never gets an EOF on</dev/ttyACM0
because when you close the descriptor you do so in the current shell not incat
's subshell. So you should be using insteadhead -n$GUARANTEED_NUMBER_OF_AVAILABLE_LINES
orsed $LINESq
or something - something that will explicitly quit input. – mikeserv Sep 27 '14 at 20:03cat >> /home/pi/output &2>1
runscat >> /home/pi/output
, and runs the null command with the redirection2>1
(i.e. standard error redirected to the file called1
), which creates an empty file called1
. Did you mean2>&1
, or2>&1 &
? – Gilles 'SO- stop being evil' Sep 27 '14 at 23:40