bash:在后台启动多个链式命令 [英] bash: start multiple chained commands in background

查看:26
本文介绍了bash:在后台启动多个链式命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 bash 在后台并行运行一些命令.这是我正在尝试做的事情:

I'm trying to run some commands in paralel, in background, using bash. Here's what I'm trying to do:

forloop {
  //this part is actually written in perl
  //call command sequence
  print `touch .file1.lock; cp bigfile1 /destination; rm .file1.lock;`;
}

反引号 (``) 之间的部分生成一个新的 shell 并连续执行命令.问题是,只有在执行完最后一个命令后,才会返回对原始程序的控制.我想在后台执行整个语句(我不希望有任何输出/返回值)并且我希望循环继续运行.

The part between backticks (``) spawns a new shell and executes the commands in succession. The thing is, control to the original program returns only after the last command has been executed. I would like to execute the whole statement in background (I'm not expecting any output/return values) and I would like the loop to continue running.

调用程序(具有循环的程序)在所有生成的 shell 完成之前不会结束.

The calling program (the one that has the loop) would not end until all the spawned shells finish.

我可以在 perl 中使用线程来生成调用不同 shell 的不同线程,但这似乎有点矫枉过正......

I could use threads in perl to spawn different threads which call different shells, but it seems an overkill...

我可以启动一个 shell,给它一组命令并让它进入后台吗?

Can I start a shell, give it a set of commands and tell it to go to the background?

推荐答案

我没有测试过,但是怎么样

I haven't tested this but how about

print `(touch .file1.lock; cp bigfile1 /destination; rm .file1.lock;) &`;

括号的意思是在子shell中执行,但这不会有什么坏处.

The parentheses mean execute in a subshell but that shouldn't hurt.

这篇关于bash:在后台启动多个链式命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆