Re: [PHP] Redirecting STDERR to a file?

2008-02-04 Thread Daniel Brown
On Feb 3, 2008 10:08 PM, Richard Lynch [EMAIL PROTECTED] 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 [EMAIL PROTECTED]
# 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 21  $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



Re: [PHP] Redirecting STDERR to a file?

2008-02-03 Thread Richard Lynch


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. :-)

-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/from/lynch
Yeah, I get a buck. So?

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



Re: [PHP] Redirecting STDERR to a file?

2008-02-02 Thread Per Jessen
js wrote:

 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.

I think nohup program 2errorlog will do what you're after.


/Per Jessen, Zürich

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



Re: [PHP] Redirecting STDERR to a file?

2008-02-02 Thread js
nohup would work in some ways, but that's not a real daemon
because the process will have tty, in a session and has a process group.
I like to have a real daemon.

On Feb 2, 2008 5:25 PM, Per Jessen [EMAIL PROTECTED] wrote:
 js wrote:

  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.

 I think nohup program 2errorlog will do what you're after.


 /Per Jessen, Zürich

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



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



Re: [PHP] Redirecting STDERR to a file?

2008-02-02 Thread Per Jessen
js wrote:

 nohup would work in some ways, but that's not a real daemon
 because the process will have tty, in a session and has a process
 group. I like to have a real daemon.

Well, it depends on what you're after.  Are you solving a problem or are
you doing an exercise because you can? 

If you're solving a problem, and my nohup suggestion isn't sufficient,
just write your daemon in C.  If it's an exercise, take a look at
proc_open().


/Per Jessen, Zürich

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



Re: [PHP] Redirecting STDERR to a file?

2008-02-02 Thread js
Hi Per,

  nohup would work in some ways, but that's not a real daemon
  because the process will have tty, in a session and has a process
  group. I like to have a real daemon.

 Well, it depends on what you're after.  Are you solving a problem or are
 you doing an exercise because you can?

Not an exercise. This is for my day job...

 If you're solving a problem, and my nohup suggestion isn't sufficient,
 just write your daemon in C.  If it's an exercise, take a look at
 proc_open().

C would be the last resort. I suppose it would be easily done in Perl,
but most of my colleagues prefer PHP.
I love to make taking over the code easy, so PHP is more preferable to me.

I took your advice and tried proc_open, but it seems blocking the
parent process.
 (BTW, I didn't know this function, thanks!)

I've already spent a lot of time solving this, os might beter to give
up this problem
and go for the other solution...

Anyway, thanks for you help.
I do appreciate it very much.

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



Re: [PHP] Redirecting STDERR to a file?

2008-02-02 Thread Per Jessen
js wrote:

 C would be the last resort. I suppose it would be easily done in Perl,
 but most of my colleagues prefer PHP.
 I love to make taking over the code easy, so PHP is more preferable to
 me.

That makes perfect sense, but sometimes PHP is not the best/right
answer. 

What I think you need to do is: 

parent:   fork() your child process, then do whatever. 
child:use proc_open to call your program and redirect stderr. 



/Per Jessen, Zürich

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



Re: [PHP] Redirecting STDERR to a file?

2008-02-02 Thread js
  C would be the last resort. I suppose it would be easily done in Perl,
  but most of my colleagues prefer PHP.
  I love to make taking over the code easy, so PHP is more preferable to
  me.

 That makes perfect sense, but sometimes PHP is not the best/right
 answer.

 What I think you need to do is:

 parent:   fork() your child process, then do whatever.
 child:use proc_open to call your program and redirect stderr.

That's kind of odd, but seems to be the only way to workaround this...
Thanks for you help.

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



Re: [PHP] Redirecting STDERR to a file?

2008-02-02 Thread Richard Heyes

C would be the last resort. I suppose it would be easily done in Perl,
but most of my colleagues prefer PHP.
I love to make taking over the code easy, so PHP is more preferable to
me.

That makes perfect sense, but sometimes PHP is not the best/right
answer.

What I think you need to do is:

parent:   fork() your child process, then do whatever.
child:use proc_open to call your program and redirect stderr.


That's kind of odd, but seems to be the only way to workaround this...
Thanks for you help.


Having come to this rather late, I recently wrote some code that may be 
of some help to you which forks processes: 
http://www.phpguru.org/downloads/pcntl/


You need pcntl and it only works on *nix (I understand). It does make 
fork()ing somewhat easier though. Be careful of defunct processes though.


--
Richard Heyes
http://www.websupportsolutions.co.uk

Knowledge Base and Helpdesk software for £299pa hosted for you -
no installation, no maintenance, new features automatic and free

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



[PHP] Redirecting STDERR to a file?

2008-02-01 Thread js
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.

Thanks.

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