How to get the PID of a process that is piped to another process in Bash?

前端 未结 14 1164
无人共我
无人共我 2020-12-01 01:35

I am trying to implement a simple log server in Bash. It should take a file as a parameter and serve it on a port with netcat.

( tail -f $1 & ) | nc -l -         


        
相关标签:
14条回答
  • 2020-12-01 02:37

    The --pid option to tail is your best friend here. It will allow you total control of the pipeline running in background. read the tail command options for more resilience in case your file is actively rotated by another process which might leave you tailing a inactive inode. The example below, though not used to process the data demonstrate the "imposed" restriction on the tail and the ability to tell it to exit at any time. This is used for measuring the service pressure on httpd .

      # Set the tail to die in 100 second even if we die unexpectedlly.
    sleep 100 & ;  ctlpid=$!
    tail -q -n 0 --follow=name --retry --max-unchanged-stats=1 --pid=$ctlpid -f  /var/log/httpd/access_log 2>/dev/null | wc –l > /tmp/thisSampleRate &
    …. Do some other work
    ….  Can kill the pipe at any time by killing $ctlpid 
    …. Calculate preassure if /tmp/thisSampleRate is ready
    
    0 讨论(0)
  • 2020-12-01 02:40

    One way would be to simply do a ps -ef and grep for tail with your script ppid

    0 讨论(0)
提交回复
热议问题