【问题标题】:Write output of subprocess launched by a `screen` CLI Command to a log file?将“screen” CLI 命令启动的子进程的输出写入日志文件?
【发布时间】:2016-10-28 17:12:58
【问题描述】:

我正在将一堆相同的脚本 (generate_records.php) 启动到屏幕中。我这样做是为了轻松地并行化这些过程。我想使用&> log_$i(StdOut 和 StdErr)之类的东西将每个 PHP 进程的输出写入日志文件。

我的 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

【问题讨论】:

  • 你为什么使用screen?为什么不直接使用普通的 shell i/o 重定向将输出发送到文件? (php generate_records.php "$starting_id" "$num_records_per_processor" "FALSE" > log-for-$start_id.txt 2>&1 &)
  • 我正在使用screen 并行启动一堆相同的脚本。我认为您的命令可以正常工作,但我无法让它在 screen 命令的上下文中工作。我认为这是一个 screen 命令或 sh 语法问题。

标签: linux bash shell logging stdout


【解决方案1】:

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

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

您还可以使用parallel 删除一些代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    • 2017-03-24
    • 2020-07-13
    • 2012-02-18
    • 2019-02-05
    • 2013-03-12
    相关资源
    最近更新 更多