I need a full path/command thats been execued to start a process thats exposed on the network - listens on tcp/udp . I did manage to get all of that using below "one liner":
netstat -nlp46|tail -n +3 | while IFS=" " read -r -a line;
do nb=${#line[@]};
PID=$(echo ${line[$((nb - 1))]} | awk -F'/' '{print $1}');
CMD=$(ps -ef|awk '$2 == '"$PID" |awk -F" " '{out=$8; for(i=9;i<=NF;i++){out=out""$i}; print out}');
echo $PID^$CMD;
done
but when process contains some special characters in command line, eg thin server, script has some issues with following error
awk: line 1: missing ) near end of line
when I did run part of script against that process I had no problems
root@ftp-host02:~# PID=15000; COMMAND=$(ps -ef|awk '$2 == '"$PID" |awk '{out=$8; for(i=9;i<=NF;i++){out=out" "$i}; print out}');
echo $COMMAND
thin server (0.0.0.0:3000)
What I'm doing wrong here?
Thanks
ps -ww -o args= -p $PID
is probably an easier way to get the command line (or look at/proc/$PID/cmdline
)