在centos 6虚拟盒子上,我尝试在多个目录上运行gulp watch
,将其输出到2个文件。一个文件用于每个目录,另一个用于所有目录。所以采取以下数组;
directories=(
'local.site1.com'
'local.site2.co.uk'
)
我想循环使用它,然后总共创建3个文件;
如果我对站点1中的文件进行了更改,则更改将记录在site1.log和all-gulp.log中,然后对站点2进行更改,它应该记录到site2.log和all-gulp.log 。因此每个站点都有它的单独日志,但我可以在一个文件中看到所有站点的输出。所有文件都应附加日志。
一旦我有了这个单一文件,我希望能够运行命令tail -f /path/to/all-gulp.log
,以便我可以看到在两个目录上运行此进程的实时输出。
我的尝试如下
#!/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
更新
以上内容已经修改。最后一行tail -f
命令(我发誓我之前尝试过)确实给了我进程的运行输出,但仍然没有迹象表明站点2发生任何事情。没有文件被创建,当我保存时没有输出运行站点2上的文件(文件应该触发编译)
我用单个bash脚本尝试甚至可以做什么?
答案 0 :(得分:0)
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.
答案 1 :(得分:0)
正如Marc指出的那样,您可能需要&
在后台运行gulp
。我没有安装gulp
,因此无法测试您的代码。通过比较此脚本,您可以更好地处理&
的操作:
while true; do
sleep 1
echo -n 0
done
while true; do
sleep 1
echo -n 1
done
仅打印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
将打印0
和1
。