简体   繁体   English

bash将gulp输出监视到多个文件,然后使用tail -f显示输出

[英]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. 在centos 6虚拟框中,我尝试在多个目录上运行gulp watch ,将其输出到2个文件。 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; 我想循环浏览一下,然后总共创建3个文件。

  1. local.site1.com-gulp.log local.site1.com-gulp.log
  2. local.site2.co.uk-gulp.log local.site2.co.uk-gulp.log
  3. all-gulp.log 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. 如果我对站点1中的文件进行更改,则更改将记录在site1.log和all-gulp.log中,然后对于站点2相同,它应记录到site2.log和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. 有了这个单一文件后,我希望能够运行命令tail -f /path/to/all-gulp.log以便可以看到在两个目录上运行的该进程的实时输出。

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) 使最后一行成为tail -f命令(我发誓,我已经尝试过了)确实为我提供了该进程的运行输出,但是仍然没有迹象表明站点2会发生任何事情。当我保存文件时,没有创建文件,也没有输出运行在站点2上(文件应触发编译)

Is what I'm attempting even possible with a single bash script? 使用单个bash脚本,我什至可以尝试吗?

If you really want it, anything is possible with a Bash script. 如果您真的想要,使用Bash脚本可以进行任何操作。

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). 第二个进程没有运行,因为Bash只是在等待第一个进程完成(它从来没有这样做)。

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. 尝试在tee命令的末尾添加&以并行启动进程,并在循环后阻塞之前wait命令,直到它们全部完成为止。

edit: I'm not sure about the parallel execution of tee to append to the same file, though. 编辑:我不确定要并行执行tee追加到同一文件。 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. 您可能需要查看parallel命令来处理来自并行进程的缓冲输出。

As Marc pointed out, you may need a & to run gulp in the background. 正如Marc所指出的那样,您可能需要一个&来在后台运行gulp I don't have gulp installed so cannot test your code. 我没有安装gulp因此无法测试您的代码。 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: 将只输出0 s,输出为:

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 . 这将打印01

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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