有5脚本在任意给定时间运行的 [英] Have 5 scripts running at any given time

查看:104
本文介绍了有5脚本在任意给定时间运行的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有启动90 不同的 PHP脚本,bash脚本(CentOS的6.4下运行),即。

 #!/斌/庆典PHP路径1 / some_job_1.php&安培;
PHP路径2 / some_job_2.php&安培;
PHP path3时/ some_job_3.php&安培;
PHP path4 / some_job_4.php&安培;
PHP path5 / some_job_5.phpPHP path6 / some_job_6.php&安培;
PHP path7 / some_job_7.php&安培;
PHP path8 / some_job_8.php&安培;
PHP path9 / some_job_9.php&安培;
PHP path10 / some_job_10.php
...退出0

为了避免超载我的服务器,我用的是符号&安培; ,它的工作原理,但我的目标是要始终有5个脚本同时运行时间

有没有办法实现这一目标?


解决方案

此问题被弹出好几次,但我无法找到它正确的答案。现在我想我找到了一个很好的解决方案!

不幸的是平行不是标准分布的一部分,但是的是。它有一个开关 -j 做使得并行。

男人化妆(1)]:( 在make并行执行更多信息)


  -j [就业],--jobs [=职位]
        指定要同时运行的作业(命令)的数量。如果
        有一个以上的-j选项,最后一个是有效的。如果
        -j选项时没有参数给出使不会限制
        就业人数,可以同时运行。


因此​​,与合适的的Makefile 的问题可以得到解决。

  .PHONY:所有$(PHP_DEST)#在PHP1 PHP2的形式创建的对象的数组... PHP90
PHP_DEST:= $(添加preFIX PHP,$(SEQ壳1 90))#默认目标
所有:$(PHP_DEST)#执行适当的脚本,对每个目标
$(PHP_DEST):
        N = $(SUBST PHP ,, $ @); PHP的路径$ N / some_job_ $ N.php

它创建的 PHP#90 目标每个电话 PHP路径#/ some_job _#。PHP 。如果您运行使-j 5 然后它会运行PHP并行5实例。如果一个人完成启动下。

我改名的Makefile parallel.mak ,我跑搭配chmod 700平行。麦和我添加#!的/ usr / bin中/让-f 来的第一线。现在,它可以被称为 ./ parallel.mak -j 5

甚至可以使用更复杂的 -l <​​/ code>开关:


  -l [负载],--load平均[=负载]
        指定任何新的就业机会(命令),如果要开始有
        正在运行的其他工作,平均负载至少负载(
        浮点数)。不带参数,将删除previous负荷
        限制。


在这种情况下,使将决定多少作业可根据系统的负载启动。

我用 ./ parallel.mak -j -l 1.0 测试,并很好地运行。它开始4个程序并行起初相反 -j 无ARGS是指运行多个并行的过程,因为它可以。

I have a bash script (running under CentOS 6.4) that launches 90 different PHP scripts, ie.

#!/bin/bash

php path1/some_job_1.php&
php path2/some_job_2.php&
php path3/some_job_3.php&
php path4/some_job_4.php&
php path5/some_job_5.php

php path6/some_job_6.php&
php path7/some_job_7.php&
php path8/some_job_8.php&
php path9/some_job_9.php&
php path10/some_job_10.php
...

exit 0

In order to avoid overloading my server, I use the ampersand &, it works, but my goal is to always have 5 scripts running at the same time

Is there a way to achieve this?

解决方案

This question is popped several times, but I could not find a proper answer for it. I think now I found a good solution!

Unfortunately parallel is not the part of the standard distributions, but is. It has a switch -j to do makes parallel.

man make(1)]: (more info on make's parallel execution)

  -j [jobs], --jobs[=jobs]
        Specifies the number of jobs (commands) to run simultaneously.  If
        there  is  more than one -j option, the last one is effective.  If
        the -j option is given without an argument, make  will  not  limit
        the number of jobs that can run simultaneously.

So with a proper Makefile the problem could be solved.

.PHONY: all $(PHP_DEST)

# Create an array of targets in the form of PHP1 PHP2 ... PHP90
PHP_DEST := $(addprefix PHP, $(shell seq 1 1 90))

# Default target
all: $(PHP_DEST)

# Run the proper script for each target
$(PHP_DEST):
        N=$(subst PHP,,$@); php path$N/some_job_$N.php

It creates 90 of PHP# targets each calls php path#/some_job_#.php. If You run make -j 5 then it will run 5 instance of php parallel. If one finishes it starts the next.

I renamed the Makefile to parallel.mak, I run chmod 700 parallel.mak and I added #!/usr/bin/make -f to the first line. Now it can be called as ./parallel.mak -j 5.

Or even You can use the more sophisticated -l switch:

  -l [load], --load-average[=load]
        Specifies  that  no new jobs (commands) should be started if there
        are others jobs running and the load average is at least  load  (a
        floating-point number).  With no argument, removes a previous load
        limit.

In this case will decide how many jobs can be launched depending on the system's load.

I tested it with ./parallel.mak -j -l 1.0 and run nicely. It started 4 programs in parallel at first contrary -j without args means run as many process parallel as it can.

这篇关于有5脚本在任意给定时间运行的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆