bash:在后台启动多个链式命令

编程入门 行业动态 更新时间:2024-10-08 00:23:38
本文介绍了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:在后台启动多个链式命令

本文发布于:2023-11-05 10:20:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560569.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:链式   多个   后台   命令   bash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!