Forum Moderators: coopster

Message Too Old, No Replies

how to exec >/dev/null 2>&1 0>&1

in php

         

Xuefer

11:33 am on Dec 18, 2003 (gmt 0)

10+ Year Member



this is in c:


/* force stdin/out/err allocate to /dev/null */
fclose(stdin);
fp = fopen("/dev/null", "w+");
if (fileno(fp)!= STDIN_FILENO) {
msg_out(crit, "fopen: %m");
exit(1);
}
if (dup2(STDIN_FILENO, STDOUT_FILENO) == -1) {
msg_out(crit, "dup2-1: %m");
exit(1);
}
if (dup2(STDIN_FILENO, STDERR_FILENO) == -1) {
msg_out(crit, "dup2-2: %m");
exit(1);
}

possible to rewrite it in php?

coopster

12:27 pm on Dec 22, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



No takers yet, eh? ;)

What is it you are trying to ultimately accomplish? It seems as though you want to close the standard file descriptors (stdin, stdout, and stderr) and then open new ones on /dev/null, but for what reason? I'm asking because there may be a better way to achieve your goal as opposed to using this technique.

Maybe you'll get an idea from one of the following resources:
Program Execution functions [php.net]
exec -- Execute an external program [php.net]

Xuefer

12:44 pm on Dec 22, 2003 (gmt 0)

10+ Year Member



the above c code is just redirect stdin/stdout/stderr to /dev/null
i can make the program separate from tty

in shell, i can do:
./prog >/dev/null 2>&1 0>&1 &
(equal to the bold part)
and the last "&" can be achieved by php code "pcntl_fork()"

coopster

12:55 pm on Dec 22, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Have you tried running your shell command using PHP's exec() function? I guess I'm wondering why that won't work for you?

Xuefer

1:13 pm on Dec 22, 2003 (gmt 0)

10+ Year Member



thanks
but it seems you missunderstood my post

i was not saying php to exec a program and redirect the input/output, but run php daemon from shell

redirect std*** to /dev/null inside php
the ./prog is actually ./prog.php (daemon program):


#!/usr/bin/php
....
redirect here
sleep(1000);

if i could do it in php, i shouldn't have done it using prog.sh:


#!/bin.sh
./prog.php >/dev/null 2>&1 1>&0 &

it will not help if i do exec("exec >/dev/null 2>&1 1>&0");

coopster

1:52 pm on Dec 22, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>>but it seems you missunderstood my post

Yes, I did. Thanks for the clarification. OK, I'm with you now. Have you had a look at popen [us2.php.net] or proc_open [php.net]?

I don't think PHP input/output streams [php.net] is going to work for you, but you might want to have a look there as well.

Xuefer

3:16 pm on Dec 22, 2003 (gmt 0)

10+ Year Member



both popen and proc_open open new process
which is child of my current php program, not child of shell
what i want to do is dettach tty from current process

proc_open maybe ok to do:
shell ---> myprog.php --proc_open--> mydaemon.php
but still not:
shell ---> mydaemon.php
but anyway, thank u so much for your paying time to my post :)