Re: Redirecting STDERR to a file?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Feb 3, 2008 10:08 PM, Richard Lynch <ceo@xxxxxxxxx> wrote:
>
>
> On Fri, February 1, 2008 10:58 pm, js wrote:
> > Hi,
> >
> > I was trying to write a script  in PHP that takes a program name
> > as an argument and invoke it as a daemon.
> > PHP provides fork(pcntl_fork), setsid(posix_setsid) and umask,
> > so it was easy.
> > However, I couldn't find a way  to redirect STDERR a file.
> > I like to have the daemon write its log to its  own logfile, like
> > apache and mysql do.
> >
> > So is there any way to accomplish that?
> > Any pointers, suggestions would be greatly appreciated.
>
> http://php.net/set_error_handler
>
> You can catch (almost) all the errors and send them wherever you want.
>
> Or maybe you just want to write this as a shell script instead of
> using PHP in the first place. :-)

    .... in which case....

#!/bin/bash
# Name: daemonize.sh (chmod 755)
# Run as: sh daemonize.sh [PROG_TO_DAEMONIZE] [LOG_FILE]
# Daniel P. Brown <parasane@xxxxxxxxx>
# To disable logging, just use /dev/null as LOG_FILE

if [ "$1" == "" ]; then
        echo "Missing PROG_TO_DAEMONIZE"
        echo "Usage: $0 [PROG_TO_DAEMONIZE] [LOG_FILE]"
        exit 1
fi

if [ "$2" == "" ]; then
        echo "Missing LOG_FILE (if you don't want logging, use /dev/null)"
        echo "Usage: $0 [PROG_TO_DAEMONIZE] [LOG_FILE]"
        exit 1
fi

exec "$1" 2>&1 >> $2 &

if [ "$?" != 0 ]; then
        echo "There was an error daemonizing $1."
        if [ "$2" != "/dev/null" ]; then
                echo "Please check the log file ($2) for errors."
        fi
        exit 1
fi

echo "Daemonized $1 with PID $! (from $0 with PID $$)."

if [ "$2" != "/dev/null" ]; then
        echo "All output will be logged to $2."
fi

exit 0


-- 
</Dan>

Daniel P. Brown
Senior Unix Geek
<? while(1) { $me = $mind--; sleep(86400); } ?>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux