Hello, I have a small ksh script that uses ps(1) inside a background loop to monitor some process while the script does some other stuff in the foreground. Here is a simplified version of the script that monitors the startx process, as an example:
#!/bin/ksh -ex while true; do ps -lww | egrep startx | egrep --line-buffered -v egrep; sleep 1; done & while true; do echo XXX | egrep --line-buffered -v egrep; sleep 1; done When I run this script: ./foo > bar2 2>&1 I get the expected output: + true + true + echo XXX + egrep --line-buffered -v egrep XXX + sleep 1 + ps -lww + egrep startx + egrep --line-buffered -v egrep 1000 22065 8346 17 18 0 848 4 pause I+ C0 0:00.02 /bin/sh /usr/X11R6/bin/startx /usr/X11R6/bin/startx /usr/X11R6/bin/startx /usr/X11R6/bin/startx + sleep 1 + true + true + echo XXX + egrep --line-buffered -v egrep + ps -lww + egrep startx + egrep --line-buffered -v egrep XXX + sleep 1 1000 22065 8346 17 18 0 848 4 pause I+ C0 0:00.02 /bin/sh /usr/X11R6/bin/startx /usr/X11R6/bin/startx /usr/X11R6/bin/startx /usr/X11R6/bin/startx + sleep 1 + true + echo XXX + egrep --line-buffered -v egrep XXX + sleep 1 + true + ps -lww + egrep startx + egrep --line-buffered -v egrep 1000 22065 8346 17 18 0 848 4 pause I+ C0 0:00.02 /bin/sh /usr/X11R6/bin/startx /usr/X11R6/bin/startx /usr/X11R6/bin/startx /usr/X11R6/bin/startx + sleep 1 + true + echo XXX [...] When I kill the script using ^C, the background loop keeps running, since it's apparently executed as a separate process, so I have to kill it using kill(1). Fine. So far so good. Now if I modify the background loop to monitor a non-existent process named startxBOGUS: #!/bin/ksh -ex while true; do ps -lww | egrep startxBOGUS | egrep --line-buffered -v egrep; sleep 1; done & while true; do echo XXX | egrep --line-buffered -v egrep; sleep 1; done and run the script again: ./foo > bar1 2>&1 I get this unexpected output: + true + true + echo XXX + egrep --line-buffered -v egrep XXX + sleep 1 + ps -lww + egrep startxBOGUS + egrep --line-buffered -v egrep + true + echo XXX + egrep --line-buffered -v egrep XXX + sleep 1 + true + echo XXX + egrep --line-buffered -v egrep XXX + sleep 1 + true + echo XXX + egrep --line-buffered -v egrep XXX + sleep 1 + true + echo XXX + egrep --line-buffered -v egrep XXX Note how the trace information from ksh indicates that the 'ps -lww' command now only runs once. After killing the script using ^C, I also notice that there is no separate process that continues running the background loop. So here is the question: why the difference in behavior between the two background loops? I don't see how changing 'egrep startx' into 'egrep whatever' could influence the number of times the background loop runs... Any help? A few more things: - In the real script that I'm trying to write, the process that I want to monitor appears and disappears multiple times over several hours, so I need the background loop to keep working even when the process that I want to monitor is not around. - I tried the same thing with bash and it exhibits the same difference in behavior (except that when killing the script using ^C, bash also kills the background loop too, if it's still running)! - It's ksh 5.2.14 running on OpenBSD 5.6 generic stable i386. Thanks, Philippe