小编use*_*439的帖子

bash脚本在后台运行一定数量的作业

我需要一个bash脚本在后台运行一些作业,一次三个作业.

我知道可以通过以下方式做到这一点,为了说明,我假设工作的数量是6:

./j1 &
./j2 &
./j3 &
wait
./j4 &
./j5 &
./j6 &
wait
Run Code Online (Sandbox Code Playgroud)

但是,通过这种方式,例如,如果j2运行j1和j3需要更长的时间,那么,我将只会遇到一个运行很长时间的后台作业.

替代方案(这就是我想要的)是每当一个作业完成时,bash应该开始队列中的下一个作业,以便在任何给定时间保持3个作业的速率.是否可以编写一个bash脚本来实现这个替代方案,可能使用循环?请注意,我需要运行更多的工作,我希望这种替代方法能为我节省大量时间.

这是我的脚本草稿,我希望你可以帮助我验证它的正确性并改进它,因为我是bash脚本的新手.从这里,这里这里获取和修改此脚本中的想法:

for i in $(seq 6)
do
   # wait here if the number of jobs is 3 (or more)
   while (( (( $(jobs -p | wc -l) )) >= 3 )) 
   do 
      sleep 5      # check again after 5 seconds
   done

   jobs -x ./j$i &
done
wait
Run Code Online (Sandbox Code Playgroud)

恕我直言,我认为这个脚本做了所需的行为.但是,我需要知道 - 来自bash专家 - 如果我做错了什么或者是否有更好的方法来实现这个想法.

非常感谢你.

linux bash shell

5
推荐指数
1
解决办法
921
查看次数

grid_plot + tikzDevice + 带有乳胶标记的共享图例

我一直在尝试遵循这个小插图,了解如何为多个 ggplot2 制作共享图例。给定的示例按原样完美运行,但就我而言,我使用 tikzDevice 导出tikzpicture环境。主要问题似乎是图例键的宽度未被正确捕获grid_plot

我想出了一个最小的 R 代码来重现该问题:

require(ggplot2)
require(grid)
require(gridExtra)
require(cowplot)
require(tikzDevice)

tikz(file = "./tmp.tex", width = 5.6, height = 2.2, standAlone = T )
mpg2 <- mpg

mpg2$cyl = as.factor(mpg2$cyl)
levels(mpg2$cyl) <- c("\\textbf{\\textsc{four}}",
                      "\\textbf{\\textsc{five}}",
                      "\\textbf{\\textsc{six}}",
                      "\\textbf{\\textsc{seven}}",
                      "\\textbf{\\textsc{eight}}")

plot.mpg <- ggplot(mpg2, aes(x=cty, colour=cyl, y = hwy)) + 
     geom_point() +
     theme(legend.position='none')

legend <- get_legend(plot.mpg + theme(legend.position = "top"))

print(plot_grid(legend, 
                 plot.mpg, nrow=2, ncol=1,align='h', 
                 rel_heights = c(.1, 1)))

dev.off()
Run Code Online (Sandbox Code Playgroud)

生成的 PDF 文件(编译 tmp.tex 后)如下所示:

在此输入图像描述

正如我们所观察到的,第一个图例键(四)仅部分显示,图例键(八)完全不可见。我尝试更改tikz …

r ggplot2 cowplot tikzdevice

5
推荐指数
1
解决办法
566
查看次数

ggplot2:如何向水平图例添加换行符

请考虑以下 R 脚本(取自此处并稍作修改):

require(ggplot2)

x <- 1:10
y <- jitter(x^2)

DF <- data.frame(x, y)

p <- ggplot(DF, aes(x = x, y = y)) + geom_point() +
  stat_smooth(method = 'lm', aes(colour = 'linear')) +
  stat_smooth(method = 'lm', formula = y ~ poly(x,2), 
              aes(colour = 'polynomial')) +
  stat_smooth(method = 'nls', formula = y ~ a * log(x) +b, 
              aes(colour = 'logarithmic')) +
  stat_smooth(method = 'nls', formula = y ~ a*exp(b *x), 
              aes(colour = 'Exponential')) +
  theme(legend.position = "top") 

p <- …
Run Code Online (Sandbox Code Playgroud)

r legend ggplot2

0
推荐指数
1
解决办法
4580
查看次数

标签 统计

ggplot2 ×2

r ×2

bash ×1

cowplot ×1

legend ×1

linux ×1

shell ×1

tikzdevice ×1