Re: Redirecting STDERR to a file?
| From: | Daniel Brown | Date: | Mon, 04 Feb 2008 15:17:44 +0000 |
| Subject: | Re: Redirecting STDERR to a file? | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-268637@lists.php.net to get a copy of this message | ||
On Feb 3, 2008 10:08 PM, Richard Lynch <ceo@l-i-e.com> 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@gmail.com>
# 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); } ?>