2

I've created a service with reactphp which runs and does some stuff. It is started as a daemon so all output should be logged in a file. This log file should be named 'foo-log-file-$(date "+F")'. I want to have a single log file for each day.

Problem:

As mentioned the script runs as a service, without stopping. The starting call for the script is therefore only done once.

php my_script.php >> /var/log/bar/log-file--$(date "+%F") 2>&1

So everything which is printed to the console from this script is saved into the file, but the file is only created with the date-string when it was called and is not updated with a new date.

Question:

Is it possible to solve this without writing the log logic in the php-script? Can i handle this requirement with bash?

alabama
  • 413
  • 7
  • 19
  • 1
    You can probably run a cronjob for this, to be run every day for example. – fedorqui Nov 26 '15 at 12:06
  • @fedorqui what exactly do you mean with a "cronjob", the start of the daemon is only once, so what should this cronjob exactly do? – alabama Nov 26 '15 at 12:38
  • 1
    Uhms, I did not understand the issue properly. Now I see: the log file name is generated when you start the application. Then, you want to have this file changed every day. For this, you may want to use [rotate](http://www.cyberciti.biz/faq/how-do-i-rotate-log-files/). It will automatically move the log under some circumstances, so that you can "move" one at 00.00 of the next day. – fedorqui Nov 26 '15 at 12:41

1 Answers1

1

FYI The answer of @fedorqui was a good approach, i solved it with a cronjob, which copies the file to a different one and empties the rest. You cannot use move, cause the through the running service, it is open all time and you get the error:

cannot move 'foo.log' to 'bar.log': Text file busy

So i cp it and clear the old one with:

cp foo.log foo.log.$(date +"%F");
cp /dev/null file.log;
alabama
  • 413
  • 7
  • 19