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. :(