Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 同时执行多个脚本实例_Bash_Shell_Scripting - Fatal编程技术网

Bash 同时执行多个脚本实例

Bash 同时执行多个脚本实例,bash,shell,scripting,Bash,Shell,Scripting,我有一个crontable,它使用不同的参数执行脚本 0,5,10 * * * * /path/to/logScript "script2" 0,5,10 * * * * /path/to/logScript "script1" 0,5,10 * * * * /path/to/logScript "script3" 剧本 #!/bin/bash script=$1 $script args >/path/tmp/log.out 2>/path/tmp

我有一个crontable,它使用不同的参数执行脚本

0,5,10 * * * * /path/to/logScript "script2"
0,5,10 * * * * /path/to/logScript "script1" 
0,5,10 * * * * /path/to/logScript "script3" 
剧本

#!/bin/bash     
    script=$1

    $script args >/path/tmp/log.out 2>/path/tmp/log.err
    if [ ! -s  /path/tmp/log.err ]; then
       echo -n "$(date) success: " >> /path/logfile
       cat /path/tmp/log.out >> /path/logfile
    else
       echo -n "$(date) errors: " >> /path/logfile
       cat /path/tmp/log.err >> /path/logfile
    fi
我认为我遇到的问题是脚本在同一时间的执行。脚本没有获得正确的return值,无法知道它是stderr还是stdout。如果我在终端中一个接一个地执行crontable行,它可以正常工作,但是当它自动执行时,我得到的数据是不正确的

我试图通过对crontable进行更改来解决这个问题,但我仍然有同样的问题

0,5,10 * * * * /path/to/logScript "script2"&
0,5,10 * * * * /path/to/logScript "script1"& 
0,5,10 * * * * /path/to/logScript "script3"&

如果所有命令同时运行,您可以将命令组合成一个唯一的命令,使用命令末尾的(&at)将其推送到后台模式,如下所示:

0,5,10 * * * * /bin/bash -c '/path/to/logScript "script2" &  /path/to/logScript "script1" & /path/to/logScript "script3" & '

您需要将其作为后台进程运行&在进程行的末尾添加,以便生成它们

您还可以将它包装成一个循环,i是进程数,并在同一个文件中运行它

func () {
 #do this
 #do that
 #hit him with a bat
}
然后在上海 !/bin/bash

 for i in $(seq 0 5);

 do

 function() & 

 echo "child pid: " $! 

 done

问题是什么?@malyy查看我的编辑您的脚本同时将其输出写入相同的文件/path/tmp/log.out、/path/tmp/log.err和/path/logfile。当您运行脚本时,请添加一个锁文件,然后其他脚本需要检查锁文件是否存在,如果锁文件不存在,请执行第二个脚本。如果您试图以不同的执行时间使用同一资源,则会产生不可预测的结果。