Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kill Cat After Background Process Ends

Tags:

bash

cat

Running a C# program with mono under Cent OS. There is a fifo that allows external input to go into this program.

I also have cat to accept input from the screen session the mono program is running in.

#! /bin/bash -
echo "Starting server"
mono --gc=sgen Server-CLI.exe < $fifo &
echo $$ > $PIDFILE
cat > $fifo
echo "Server stopped. Cleaning up"
rm -f $fifo
rm -f $PIDFILE

How can I make cat exit whenever the mono program exits? Right now if the mono program exits, cat is still running so the script never reaches the 2nd echo.

like image 290
Dequ Avatar asked Jan 23 '26 13:01

Dequ


1 Answers

Save the PID of mono. Run cat in the background and save its PID. wait for the PID of mono. When this is satisfied, kill the PID of cat.

mono &
monoPID=$!
cat &
catPID=$!
wait "$monoPID"
kill "$catPID"
like image 88
Dennis Williamson Avatar answered Jan 25 '26 11:01

Dennis Williamson