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 make 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 make 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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…