0

I'm a web developer who runs two scripts during web development. One watches for CSS changes and one watched for JS changes. When a change is sensed it will merge and concatenate the appropriate files. You can stop "watching" for changed by Ctr-c'ing and stopping each process.

I just used Tmux windows to run these scripts but other devs who don't use screen or tmux ask to combine these "watch" shell scripts into one file. I'm not sure how to do this as when I combined them only one process runs and when I Ctrl-c it, then the other process starts watching. Optimally there would be a way to get them to work with one call, and to stop both process when a stop call is sent.

I'm assuming I need to fork here but am not sure how to do that. The scripts use standard bash and work on all Unix-based systems. Here's the code so far:

#!/bin/sh                                                                                                                                                                                                                                                                                                                                                                 
# Watch for js template changes.
./template-watch.sh                                                                                                                                                                                                                                                                                                                                                       

# Watch for changes in sass files and render them to CSS files.                                                                                                                                                                                                                                                                                                           
sass --watch --style=compressed ../src/scss:../assets/css      

1 Answer 1

4

I think, you could run your scripts in background (using &), store their pid from $!. Than you can wait for that processes to finish. In order to kill both scripts, you can setup trap on kill signal and kill all backgrounded processes.

#!/bin/sh

kill_background_processes() {
    kill $template_watch_pid $sass_pid
}    
trap kill_background_processes SIGINT SIGTERM

# Watch for js template changes.
./template-watch.sh &
template_watch_pid=$!

# Watch for changes in sass files and render them to CSS files.
sass --watch --style=compressed ../src/scss:../assets/css &
sass_pid=$!

wait $template_watch_pid $sass_pid

This functions - I have tested that. Simply run this command and finally press Ctrl + C. Both processes should be terminated.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .