간단한 port listen 샘플입니다.
아래 내용을 nc.sh 등으로 만들어 nc.sh 80 등으로 listen 하면 nc_80.pid 에 PID, nc_80.log 에 echo 내용이 저장됩니다.
#!/bin/bash
if [ "$#" -ne 1 ]; then
echo "need port parameter"
exit 1
fi
PORT=$1
PID_FILE="nc_$PORT.pid"
LOG_FILE="nc_$PORT.log"
kill_nc_if_running() {
local pid_file="$1"
if [ -f "$pid_file" ]; then
local pid=$(cat "$pid_file")
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
kill -9 "$pid" 2>/dev/null
sleep 1
rm -f "$pid_file"
echo Killed $pid
fi
fi
}
kill_nc_if_running "$PID_FILE"
nc -k -l $PORT >> $LOG_FILE &
echo $! > "$PID_FILE"
echo "nc $PORT PID: $(cat $PID_FILE)"
exit 0