简体   繁体   中英

bash output gulp watch to multiple files then show output with tail -f

On a centos 6 virtual box I'm attempting to run gulp watch on multiple directories, output that to 2 files. One file is for each directory and another would be for all directories. So take the following array;

directories=(
    'local.site1.com'
    'local.site2.co.uk'
)

I want to loop through this, and then create 3 files in total;

  1. local.site1.com-gulp.log
  2. local.site2.co.uk-gulp.log
  3. all-gulp.log

If I make a change to a file in site 1, the change will be logged in site1.log and all-gulp.log, then same for site 2, it should log to site2.log and all-gulp.log. So each site has it's individual logs, but I can see the output for all sites in a single file. All files should append the logs.

Once I have this single file, I want to be able to run the command tail -f /path/to/all-gulp.log so that I can see the live output of this process running on both directories.

My attempt is as follows

#!/bin/bash

directories=(
    'local.site1.com'
    'local.site2.co.uk'
)


for dir in ${directories[@]}
do
    cd /var/www/$dir
    gulp watch | tee -a /var/log/gulp/$dir-gulp.log /var/log/gulp/all-gulp.log

done

tail -f /var/log/gulp/all-gulp.log

UPDATE The above has been modified. Making the last line the tail -f command (I swear I tried earlier) does give me the running output of the process, but still no indication that anything happens with site 2. No file gets created and no output runs when I save a file on site 2 (the files should trigger a compilation)

Is what I'm attempting even possible with a single bash script?

If you really want it, anything is possible with a Bash script.

I may be mistaken, but it seems like you forgot to run your processes in parallel. The second process is not running because Bash is simply waiting for the first one to finish (which it never does).

Try to add a & at the end of the tee command to start the processes in parallel, and a wait command after the loop to block until they all finish.

edit: I'm not sure about the parallel execution of tee to append to the same file, though. There may be cases of race conditions if files are modified at the same time on both sides. You may want to look at the parallel command to handle buffered outputs from parallel processes.

As Marc pointed out, you may need a & to run gulp in the background. I don't have gulp installed so cannot test your code. You can get a better handle on what & does by comparing this script:

while true; do                                                           
    sleep 1                                                              
    echo -n 0                                                               
done                                                                    
while true; do                                                           
    sleep 1                                                              
    echo -n 1                                                               
done   

that will print only 0 s with the output of:

while true; do                                                           
    sleep 1                                                              
    echo -n 0                                                               
done &        # <--- Note '&' in this line will run first while-loop on background                                                                   
while true; do                                                           
    sleep 1                                                              
    echo -n 1                                                               
done  

that will print 0 and 1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM