I wrote a (ba)sh script to write to a file the content received from a POST method:


while true
  echo -e "HTTP/1.1 200 OK\n\n" | ./busybox-armv7l nc -l -p $PORT | tr '[\r\n]' '#' | sed "s/.*##//" | tr '#' '\n' | ( read line; echo $line > $FILE; echo $line )

If I launch the script from the console it works fine:

./server 5000 /tmp/foo

and the specified file is filled with the data received. But when I try to launch it on startup it doesn't work anymore. Because it's a commercial embedded device I have a script in /use/sbin/init.sh where I can add my own scripts:

export PATH=/usr/bin:/bin/:/usr/sbin:/sbin

mount -t proc none /proc
mount -t sysfs none /sys
mount -t tmpfs none /tmp

export MACHINE_ID=`cat /sys/devices/soc0/machine | sed 's/ /-/g'`

echo 8 >/proc/sys/kernel/printk

mkdir -p /dev/pts
mount -t devpts devpts /dev/pts -ogid=5,mode=620

ln -snf /tmp /dev/shm

mkdir /tmp/sd
mount -t vfat /dev/mmcblk0p1 /tmp/sd -o rw

depmod -a

/etc/init.d/syslog start

ifconfig lo up
ifconfig eth0 up
sleep 1s
mii-tool --reset

ifconfig eth0

[ ! -x /etc/dropbear/dropbear_rsa_host_key ] || /usr/sbin/dropbearkey -t rsa -f /etc/dropbear/dropbear_rsa_host_key
[ ! -x /etc/dropbear/dropbear_dss_host_key ] || /usr/sbin/dropbearkey -t dss -f /etc/dropbear/dropbear_dss_host_key

/usr/sbin/dropbear -r /etc/dropbear/dropbear_rsa_host_key -p 22

/home/root/server.sh 5000 /tmp/foo > /tmp/log.txt &


it happens that the nc server doesn't receive anything and the log.txt is continuously filled with \n.

Do you see something obviously wrong here? In which way I can further debug the issue?

  • Maybe nc in listening mode is not the best choice for that. – Rui F Ribeiro Nov 27 '18 at 9:47
  • I'm aware of that but I have no other choice. The device is very limited. Indeed, it works fine - it doesn't work only if I launch it on startup as described. – Mark Nov 27 '18 at 10:08
  • xinetd or redir might be better solution for what you want. nc in listening mode is a one shot solution, and putting it on a loop might create instability. You might even already have xinetd installed. I prefer redir, small as well. – Rui F Ribeiro Nov 27 '18 at 10:13
  • As said I've no alternatives: neither xinetd nor redir. And nc could be used in a loop: stackoverflow.com/questions/16640054/…. – Mark Nov 27 '18 at 11:00
  • I did not say it is not possible. I do not advise it as a good quality solution. For instance, it might not account for sockets in TIME_WAIT state. – Rui F Ribeiro Nov 27 '18 at 11:37

Use absolute paths instead of relative ones, so the requested executable could be find even when the current directory is different:

echo -e "HTTP/1.1 200 OK\n\n" | /home/root/busybox-armv7l nc -l -p $PORT | tr '[\r\n]' '#' | sed "s/.*##//" | tr '#' '\n' | ( read line; echo $line > $FILE; echo $line )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.