无意中学习到split -l这个命令:按指定行数截断文件内容,可以多进程处理需要批量处理的相同的程序/脚本/命令,并结合nohup &将程序放到后台运行,ps -ef|grep 查看。
准备测试数据:
$ more first.sh | $ more BB |
输出helloworld,并且将文本1内容(hello world) 追加到文本2内容(this is:) #!/bin/bash a="hello world" echo $a cat text1.txt >>text2.txt | 内容为批量脚本 first.sh first.sh first.sh first.sh first.sh |
现在将内容分解为每组2行进行处理,即3组,命令为:
$ split -l 2 BB BB- 解释:2为分解单位 BB 为需要分解的文件 BB-为分解后的独立文件。
一般会按BB-aa BB-ab BB-ac···命名,并赋予可执行权限:
chmod 755 BB-aa BB-ab BB-ac//3个文件
验证nohup &命令的连接程序:
$ nohup BB-aa BB-ab BB-ac &
[1] 11971
nohup: appending output to “nohup.out” //再次回车才能看到done
[1]+ Done nohup BB-aa BB-ab BB-ac
$ more text2.txt
this is:
hello world
hello world
可以发现:这里只输出了两个helloworld。
而我们写成三个nohup&
$ nohup BB-aa &
$ nohup BB-ab &
$ nohup BB-ac &
$ more text2.txt
this is:
hello world
hello world
hello world
hello world
hello world
这才是我们希望的结果,连接多个文件时只默认第一个有效,并放入后台。
more nohup.out 中输出了7次 hello world,也就是在我第一次测试的基础上(结果为只执行了第一个文件,写入2次)追加了5次,同时我们对执行时间较长的后台进程用ps -ef 进行确认是否存活。