简体   繁体   English

在一行中组合多个 linux 命令

[英]combine multiple linux commands in one line

I am trying to merge multiple linux commands in one line to perform deployment operation.我试图在一行中合并多个 linux 命令来执行部署操作。 For example例如

nohup php 1.php
nohup php 2.php
nohup php 3.php
nohup php 4.php

I want perform all of them in parallel, it is possible in a .sh file?我想并行执行所有这些,可以在 .sh 文件中吗?

In linux you can use && to execute commands sequentially, and a command will only execute if the previous one succeeded.在linux中你可以使用&&来顺序执行命令,只有前一个命令成功才会执行一个命令。

nohup php 1.php && nohup php 2.php && nohup php 3.php

Edit: in case you do not want the error-checking provided by the && operator, use the semicolon (;) to chain the commands, like this:编辑:如果您不希望&&运算符提供错误检查,请使用分号(;)链接命令,如下所示:

nohup php 1.php ; nohup php 2.php ; nohup php 3.php

If you don't mind putting them into the background, you can do that by putting如果你不介意把它们放在背景中,你可以通过把

  & ;     

between the 1st/2nd, and 2nd/3rd.在第一/第二和第二/第三之间。 This will execute the 3 essentially in parallel.这将基本上并行执行 3。

&: execute in bg &: 在 bg 中执行
; ; do 2nd command irrespective of success of 1st无论第一个命令是否成功,都执行第二个命令

you can also do-it like that :你也可以这样做:

nohup sh -c 'php 1.php; php 2.php; php 3.php' &

edit : to answer your question, the process are parallel.编辑:回答你的问题,这个过程是并行的。 you can verify this by writing the ps command.您可以通过编写ps命令来验证这一点。 eg : with the sleep command :例如:使用sleep命令:

nohup sh -c 'sleep 30 && sleep 30' &

output :输出:

 ....
 6920    7340    7340       6868  pty2    17763 16:33:27 /usr/bin/sleep
 6404    4792    4792       7004  pty2    17763 16:33:26 /usr/bin/sleep
 ....

edit 2 : Ok then try with parallel command.编辑 2 :好的,然后尝试使用parallel命令。 (you probably have to install it) (您可能必须安装它)

Create a file cmd.txt :创建一个文件cmd.txt

1.php
2.php
3.php

Then execute this command (haven't tried yet, but it should work).然后执行这个命令(还没试过,但应该可以)。 you can change the --max-procs numbers if you have more/less than 4 core :如果您的内核多于/少于 4 个,则可以更改--max-procs数字:

cat cmd.txt | parallel --max-procs=4 --group 'php {}'

hope it works...希望它有效...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM