Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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      
share|improve this question

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer
    
Worked like a charm, thanks! –  Mauvis Ledford Nov 18 '11 at 4:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.