我正在尝试使用 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:在后台启动多个链式命令
发布评论