Bash script does not generate desired log entries until it exits

Bash script does not generate desired log entries until it exits

I have a script that is supposed to generate some log entries and then grep through those logs to present them to the script user. However, the log entries don't get generated until the script exits. Making the script sleep for long time does not help. The only way for me to see the logs is to let the script exit and then I have to manually look at the logs. Below is a summarized version of the script and what I'm currently stuck on. My intent is to have the script read the log entries that its executing processes have generated without the need of another script or writing to files.

notroot@localhost:~$ cat script.sh

#!/bin/bash                                                                                                                                                                                                                             
exec 9<>/dev/tcp/localhost/22                                                                                                                                                                                                           
pid=$(ps -ef | grep "ssh" | grep -v "grep" | tail -n 1 | awk '{print $2}')
echo "The log entries that process $pid generated are listed below:"
egrep -R "\[$pid\]|pid=$pid" /var/log | grep -v "grep"

notroot@localhost:~$
notroot@localhost:~$ sudo ./script.sh

The log entries that process 2564 generated are listed below:

notroot@localhost:~$ 
notroot@localhost:~$ sudo egrep -R "\[2564\]|pid=2564" /var/log | grep -v "grep"

/var/log/secure:Jul 29 17:38:52 localhost sshd[2564]: Did not receive identification string from ::1 port 51812
-----Additional output omitted-----

The following is my desired output:

notroot@localhost:~$ sudo ./script.sh

The log entries that process 2564 generated are listed below:
/var/log/secure:Jul 29 17:38:52 localhost sshd[2564]: Did not receive identification string from ::1 port 51812
-----Additional output omitted-----
notroot@localhost:~$

Thank you for your time. ( ^ - ^ )

相关内容