0

I am using Ubuntu 14.04.4 LTS (GNU/Linux 3.13.0-85-generic x86_64) where I created a simple Upstart job:

# content of /etc/init/listener.conf

start on runlevel [2345]
stop on runlevel [!2345]
respawn

exec /home/mk/log_listener

The script itself looks like this:

#!/bin/sh

tail -F /var/log/vsftpd.log | while read line; do
    if echo "$line" | grep -q 'OK UPLOAD:'; then
         curl http://some.url/
    fi
done

But for some reason the job starts two processes:

$ ps jax | grep lis
   1  1234  1234  1234 ?           -1 Ss       0   0:00 /bin/sh /home/mk/log_listener
1234  1236  1234  1234 ?           -1 S        0   0:00 /bin/sh /home/mk/log_listener

I suppose this is not a "normal" behavior? How can I find out who and why is starting the second process and how to properly call the script in my Upstart job?

errata
  • 175

1 Answers1

1

We see that those two processes are related (the parent of PID 1236 is 1234) and that PID 1234 is the session leader (lower-case 's' in the STAT field).

See https://unix.stackexchange.com/a/18178/117549 where we see that pipelines in shell scripts create a new process group.

Thus, I believe that Upstart is executing your script correctly, and you are seeing two processes because of the tail ... | while read pipeline.

If I can suggest a small improvement to your script, you don't need to echo every line against grep inside your loop; instead:

tail -F /var/log/vsftpd.log | grep --line-buffered 'OK UPLOAD:' | while read unused
do
  curl http://some.url/
done
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • Thanks for your answer! I didn't quite understand the conclusion, so, this IS normal behavior (because both processes shown are part of the same PID)? Thanks for the script suggestion, I might update it also :) – errata Apr 15 '16 at 09:47
  • Normal behavior; you can see it happen separately from upstart as well. – Jeff Schaller Apr 15 '16 at 09:53