How to get the pid in tty1 of the process launched in tty2 ?
Context :
Trying to write a bash one-liner to kill a process generating a file when this file exceeds a pre defined max size. (The one-liner is not operating yet as it is as need to embed this into a loop).
During testing, the point is that lsof does not return any PID in the terminal tty1 despite the pid exists in the tty2 where the command is run.
tty1: generating the file and monitoring changes
MAX_SIZE_Ko=10001;file=test_lsof;dd if=/dev/zero of=$file bs=1k count=800;inotifywait $file;SIZE_Ko=$(du -s $file | cut -f1); [[ "$SIZE_Ko" -gt "$MAX_SIZE" ]] && ( PID=$(lsof $file | tail -n1 | awk -F" " '{ print $2 }') ; [[ ! -z $PID ]] && kill -9 $PID || echo "no running PID modifying $file" )
tty2 : increasing the file size
for (( 1; 1; 1));do echo -e "foobar\n" >> test_lsof; echo $(( i++ ))" - pid="$$; done
As mentioned in the other answer, the file is opened only for a short time, so the odds of your lsof catching it are low.
However, you can change that:
This uses advanced shell redirection – the exec line opens a file descriptor, the >&5 redirects output from the command to that file descriptor.
If you do that, the shell will be visible to lsof.