For anyone interested the final solution looks like the following, the key difference to Nicholas solution below is that I needed to grep the PID of the socat process rather than use $?:
#!/bin/bash
DELAY=600
SOCAT_PID=$(/bin/ps -eo pid,args | grep "socat -u TCP-LISTEN:12456" | grep -v grep | awk '{ print $1 }')
while `kill -0 $SOCAT_PID`
do
touch /var/ais/out.txt
NEXT_STOP=`date +%s --date "$DELAY second"`
while `kill -0 $SOCAT_PID` && [ "$(date +%s)" -lt "$NEXT_STOP" ]
do
head -q - >> /var/ais/out.txt
done
mv /var/ais/out.txt "/var/ais/_socat_received/"$(date +"%Y-%m-%d-%T")"__out.txt"
done
In addition adding the start script within an infinite while loop so that when the client disconnects we restart socat and wait for the next connection attempt:
while true
do
socat -u TCP-LISTEN:12456,keepalive,reuseaddr,rcvbuf=131071 STDOUT | /var/ais/socat_write.sh
done