Unix error logging

I am generating dynamix unix scripts which I then will only need to include using a single php shell_exec(‘. /path/to/script.sh’) call. However, I want to be able to document it’s activity. The goal:

  1. Log the commands
  2. Log any output (as it occurs)
  3. Log any errors (as they occur)

Now, I know how to re-direct the file descriptors to wherever I need them to go, but as it stands, that means I would need to append 2>logfile.log (or > logfile.log 2>&1) to every line, which isn’t THAT difficult to do dynamically using PHP (is there a unix solution that would do the same thing?). However, that still leaves the problem of not being able to log the input. The script command doesn’t work for pre-written scripts (I tried it, the connection hangs). My original plan was to use PHP and go line by line, logging the line and then outputting and logging the result, but the PHP wrappers (e.g. shell_exec) seem to reset their environment once the call completes so that doesn’t work for me. I’ve been working at this for two days. Perhaps someone has a solution for me?

I would assume that you can do it in Unix directly although I don’t know if there is a solution already programmed for this. Probably.
At least, I know that my brother who is a Unix expert, creates his own shell scripts and dynamic logs and it works for him so I am reasonably sure that you would be able to do it.

If my memory serves me well, you would need to use STDOUT and STDERR for this (Standard Output and Standard Error).

But I’m not an expert at all so you may need to check it out. And, sadly, my memory doesn’t serve me well… all the time :slight_smile:

I am a bit unclear about what you are trying to do.

Is this a correct assessment?

  • PHP dynamically ‘writes’ a *nix shell script; as a result of a user interacting with a webpage
  • You need to capture:
    [LIST=1]
  • The input parameters
  • any output generated
  • any errors that may occur
    [/LIST]

Can you add logic to the scripts themselves to log the data you need? Rather than rely/expect PHP to capture it within a loose shell environment call.

does it work if you change the PHP code to
shell_exec(‘. /path/to/script.sh > logfile.log 2>&1’)

ParkinT: That is correct. My biggest issue has been to set environment variables that persist through multiple function calls in PHP, hence why I’ve resorted to trying to created a single script that has these variables dynamically set. However, tracking the session is important for debugging purposes. As I mentioned in my original post, I could add descriptor re-directions to every line, but I was looking for a simpler way.

jurn: Let me give that a shot and I’ll let you know. That looks like it could do what I want it to do. And that should catch all errors until the script halts right? Not just the last error that occurred in the script?

hi Smola,

I think it will catch all output to stdout and stderr, and save it into logfile.log.
as for return codes it will be last code from /path/to/script.sh

Jurn