dongyong8098 2015-06-29 18:55
浏览 50

将输出重定向到两个文件,如果它死了则重新启动整个过程

I have a PHP server that runs on my machine when I execute the following command :

php src/server.php > >(tee -a ./logs/logs.txt) 2> >(echo "$(date)"; tee -a ./logs/errors.log >&2)

If I did no mistake, it should runs the server, prompts all messages to the console and write them to ./logs/logs.txt and, separately, write error output to .logs/errors.log with a timestamp.

The command executes well in my console and starts the server while creating the logs files.


I am looking for a way to put this line in a (bash?) script so I can combine it with this StackOverflow solution to restart the server if it crashes. For now, I've tried things like :

#!/bin/bash

php src/server.php >> >(echo "$(date)"; tee -a ./logs/logs.txt) 2>> >(echo "$(date)"; tee -a ./logs/errors.log >&2)

But it gives me the following error :

startServer.sh: line 3: syntax error near unexpected token `>'
startServer.sh: line 3: `php src/server.php >> >(echo "$(date)"; tee -a ./logs/logs.txt) 2>> >(echo "$(date)"; tee -a ./logs/errors.log >&2)'

I don't understand why it would not work as it runs perfectly when I run the command directly. It is a problem with the #!bin/bash code ? Or should I replace the >> with pipe (will it still works as expected ?) ?


Also, in the restart process solution, there is a until myserver; do line. What should I write instead of myserver ? startServer ?

Thanks everyone :)

Edit: Seems like 2> >(echo "$(date)"; tee -a ./logs/errors.log >&2) do put the error message in errors.log but it does not print the date in the file before. :(

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于单片机的靶位控制系统
    • ¥15 AT89C51控制8位八段数码管显示时钟。
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 下图接收小电路,谁知道原理
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错