1

I have a PHP script I'm running from Cron. I want to save its output to file, but also want to save the shell output to a different file. Ideally, I'd like to have this in one line. As such, I tried the following:

script "/folder/log/file.errors."`date +"%Y-%m-%d.%H-%M-%S"`".txt" && /usr/bin/php /folder/file.php > "/folder/log/file.php."`date +"%Y-%m-%d.%H-%M-%S"`".txt"

But it only runs the first command (before &&). Likewise, if I use ; instead of &&.

When I run this as two separate commands, it works just fine:

root:~# script "/folder/log/file.errors."`date +"%Y-%m-%d.%H-%M-%S"`".txt"
root:~# /usr/bin/php /folder/file.php > "/folder/log/file.php."`date +"%Y-%m-%d.%H-%M-%S"`".txt"

How can I join these two commands into one command/line?

Also, when run via Cron, will it be necessary for me to run an exit command after the above code in order for script to properly save to file?

13
  • May I know the purpose of using script command with a txt file?
    – Vikyboss
    Jan 29, 2016 at 20:14
  • 1
    I wasn't aware that you were trying to run the unix script command, as it changes the things. Consider this, with the script command, you are creating a new shell (with logging capability) while the second part of your command is still waiting for the first part to exit so that it can proceed. Because your first (parent) shell is going to run it
    – MelBurslan
    Jan 29, 2016 at 20:27
  • 1
    @roaima: But Pamela says she is getting file.errors.2016-01-29.15-18-38.txt, so it's not choking on the %s. Jan 29, 2016 at 20:27
  • 1
    My suggestion: instead of relying on script logging, especially if this will eventually be a cron job, consider sending output and error messages to one or more designated files(s) in your php code. When you run it in cron, it will create a session log unless you divert it to something like >/dev/null 2>&1 directive. So, as a debug tool you can check that cron log
    – MelBurslan
    Jan 29, 2016 at 20:29
  • 1
    As long as you found your answer, getting an uptick in the points system, is not that important to me. Good luck with your cron job.
    – MelBurslan
    Jan 29, 2016 at 20:37

1 Answer 1

1

Per terdon's request, I am posting this comment as an answer, so that the question can be marked as "answered"

Instead of relying on script logging, especially if this will eventually be a cron job, consider sending output and error messages to one or more designated files(s) in your php code. When you run it in cron, it will create a session log unless you divert it to something like >/dev/null 2>&1 directive. So, as a debug tool you can check that cron log

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .