将`screen` CLI命令启动的子进程的输出写入日志文件?

问题描述:

我将一堆相同的脚本(generate_records.php)启动到屏幕中。我正在这样做,以便轻松并行化流程。我想使用类似&> log_$i(StdOut和StdErr)将每个PHP进程的输出写入日志文件。将`screen` CLI命令启动的子进程的输出写入日志文件?

我的shell脚本很弱,我无法获得正确的语法。我不断收到屏幕的输出,这是空的。

〔实施例:launch_processes_in_screens.sh

max_record_id=300000000 

# number of parallel processors to run 
total_processors=10 

# max staging companies per processor 
((num_records_per_processor = $max_record_id/$total_processors)) 

i=0 
while [ $i -lt $total_processors ] 
do 
    ((starting_id = $i * $num_records_per_processor + 1)) 
    ((ending_id = $starting_id + $num_records_per_processor - 1)) 
    printf "\n - Starting processor #%s starting at ID:%s and ending at ID: %s" "$i" "$starting_id" "$ending_id" 
    screen -d -m -S "process_$i" php generate_records.php "$starting_id" "$num_records_per_processor" "FALSE" 
    ((i++)) 
done 
+0

你为什么使用'screen'?为什么不使用正常的shell I/O重定向将输出发送到文件? ('php generate_records.php'$ starting_id“”$ num_records_per_processor“”FALSE“> log-for- $ start_id.txt 2>&1&') – larsks

+0

我使用'screen'并行启动一堆相同的脚本。我认为你的命令可以正常工作,但我无法在'screen'命令的上下文中使用它。这是一个'screen'命令或sh语法问题,我正在努力与我想。 –

如果您使用screen的唯一原因是并行推出许多过程,你可以完全避免它,并使用&start them in the background

php generate_records.php "$starting_id" "$num_records_per_processor" FALSE & 

你也可以使用parallel删除一些代码。