I'm running a php socket. I run the program through nohup. Run this program properly through root. But my problem is running the program via the exec ()
function in php. When I run the command this way the program runs correctly but the program output is not printed in nohup.out.
my command in ssh:
nohup php my_path/example.php &
#is working
my command in user php:
exec('nohup php my_path/example.php >/dev/null 2>&1 &', $output);
#not update nohup.out
please guide me...
CodePudding user response:
From PHP docs on exec
:
If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.
From man nohup
:
If standard input is a terminal, redirect it from /dev/null. If standard output is a terminal, append output to 'nohup.out' if possible, '$HOME/nohup.out' otherwise. If standard error is a terminal, redirect it to standard output. To save output to FILE, use 'nohup COMMAND > FILE'.
To satisfy both - redirect manually to nohup.out
:
exec('nohup php my_path/example.php >>nohup.out 2>&1 &', $output);