I'm trying to build a start script and a stop script for my project. I need to run a sass auto-compiler as well as a server, and redirect the output of both to a file. I'm using lite-server
and sass --watch
for this.
To make these processes run concurrently, I'm using &
to background the processes. This poses the challenge of stopping the scripts, I can't stop the scripts using Ctrl+C
as I normally would. I thought I would overcome this by storing the process IDs in a text file.
I came up with the following "start" script:
# Start a sass watcher and a server running simultaneously. Store the PIDs in a
# text file so that the processes can be easily stopped.
(
lite-server &
echo $! > .pids.txt &
sass --watch sass:css --style=compressed &
echo $! >> .pids.txt &
) &> log.txt
cat .pids.txt
Then, to stop the processes, I'm using
kill $(cat .pids.txt)
Writing the process IDs to a text file seems kind of hackish. Is there a better way to accomplish what I'm trying to do?
You could store them in an array, if that's what you wouldn't call hackish.
EDIT: Another way is to just execute:
kill $(jobs -p)
This kills all background processes (jobs -p prints the PIDs of all background process to stdout, which are then handed to kill).