0

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

Community
  • 1
  • 1
RynnHeldeD
  • 51
  • 6
  • Whatever is running `startServer.sh` isn't using bash but is instead using `sh` (or some other shell). Also if you want to have this restarted when it dies I would, instead of using the loop idea, use a process/service supervisor (like systemd, runit, etc.) – Etan Reisner Jun 29 '15 at 19:16
  • Ho right. I always thought that "sh" was similar to "bash" (because of the sh in bash). I'll sleep smarter tonight ! When using `bash startServer.sh`it runs perfectly ! I'll take a look now at some process supervisor as you recommend. Thanks for your help! Btw, I edited my message because I didn't manage to print a timestamp in my errors.log before appending the error message. Do you have an idea to make it works ? – RynnHeldeD Jun 29 '15 at 20:38
  • You need to redirect the output of `date` to the log file too if you want that to work. And they are "similar" but "similar" is not "the same". – Etan Reisner Jun 29 '15 at 22:24
  • Thanks for your help Etan ! – RynnHeldeD Jun 30 '15 at 07:59

0 Answers0