Forum Moderators: coopster
/* 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);
}
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]
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");
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.
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 :)