Why update nohup.out when running nohup in exec php - php

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

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);

Related

Attempting to launch a php scrip in the background from a php script, using the exec() function, on a raspberry pi

I want to have a chat bot working on my raspberry pi.
In the command line, I am entering this command:
nohup php /path/to/script.php >/dev/null 2>&1 &
And this works. When I decide I need the the bot to be down, I enter this command:
killall php
This works, but I want to be able to start and stop the script from a browser.
So, in my dashboard php script....
//This command works.
$runningProc = exec("pgrep -a php");
//This command does not. The process starts, but immediately ends
$startCommand = "nohup php /path/to/script.php >/dev/null 2>&1 &";
exec($startCommand);
//This command does not seem to work for a nohup started from the command line.
exec("killall php");
Okay, so I figured out what was going on, once I found some relevant logs.
In my dashboard file I changed the exec() argument to :
$startCommand = "nohup php script.php >/dev/null 2> nohup.out &";
exec($startCommand);
This way, I could review the 'nohup.out' file for any errors.
The first require() command triggered a "No such file" error. Changing that function to the full filepath made everything work as expected.

PHP script hangs when calling external command from CLI

I have a PHP script that executes an external bash script to make an SSH connection but even though i am using ssh's move to background (-f) as well as an '&' my PHP script hangs.
Problem line in PHP script
system('/usr/local/bin/startProxy 172.16.0.5 9051');
I have also tried :
system('/usr/local/bin/startProxy 172.16.0.5 9051 &');
And the startProxy script is simply :
#!/bin/bash
#
# startProxy <IP_Address> <Proxy_Port>
#
# Starts an ssh proxy connection using -D <port> to remote system
#
ssh -o ConnectTimeout=5 -f -N -D $2 $1 &
Calling the startProxy script from command line works find and the script returns immediately.
When the system() command is run, the called script does run (I can see ssh running via ps), it just never appears to return to the PHP script.
I have run into the same issue when trying to call ssh directly via the system() method as well.
Thanks #Martin,
For future self and others, I had to change the system call to
system('/usr/local/bin/startProxy 172.16.0.5 9051 2>&1 >/dev/null');
and to change the startProxy script to :
ssh -o ConnectTimeout=5 -f -N -D $2 $1 2>&1 >/dev/null
before PHP return to the rest of the script!
Anyone able to explain why PHP stops like this if output is not redirected (I presume PHP isn't scanning the command and seeing the redirection part) and also why PHP hangs is i don't include the redirection in the ssh command within startProxy, dispite the fact that both the PHP system call and the ssh command in startProxy where using background options (-f and '&' )

running php exec command in background

I am attempting to launch sar and have it run forever via a php script. But for whatever reason it never actually launches. I have tried the following:
exec('sar -u 1 > /home/foo/foo.txt &');
exec('sar -o /home/foo/foo -u 1 > /dev/null 2>&1 &');
However it never launches sar. If I just use:
exec('sar -u 1')
It works but it just hangs the php script. My understanding that if a program is started with exec 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.
I will assume your running this on a *nix platform. To get php to run something in the background and not wait for the process to finish I would recommend 2 things: First use nohup and also redirect the output of the command to /dev/null (trash).
Example:
<?php
exec('nohup sar -u 1 > /dev/null 2>/dev/null &');
nohup means we do not send the "hang up" signal (which kills the process) when the terminal running the command closes.
> /dev/null 2>/dev/null & redirects the "normal" and "error" outputs to the blackhole /dev/null location. This allows PHP to not have to wait for the outputs of the command being called.
On another note, if you are using PHP just to call a shell command, you may want to consider other options like Ubuntu's Upstart with no PHP component--if you are using Ubuntu that is.

runing script in background and returning its pid from php

I try to invoke an app inside php:
ob_start();
passthru("(cd /opt/server/TrackServer/release && ./TrackServer& ) && pidof TrackServer");
$pid = ob_get_clean();
var_dump($pid);
exit;
The goal is to run TrackServer within its path and to get it's process id so I can close it after I do some test.
When I run the command in terminal:
(cd /opt/server/TrackServer/release && nohup ./TrackServer&) && pidof TrackServer
I get correct pid returned but in php the command stops and doesn't go further, the TrackServer is started and running but I have to kill it from terminal to unblock the php script, after killing the process the php script prints the correct pid for the process I've just closed from terminal.
Why the command stops?
Is there a way to make it run in php the way I'am trying to run it (without forking to a new thread)?
From the passthru manual page: The passthru() function is similar to the exec() function in that it executes a command.
What this means is that you can't execute your command line directly, as this runs several commands and relies on the shell to implement backgrounding and subshells as needed.
Try this instead:
passthru("/bin/bash -c 'cd /opt/server/TrackServer/release && nohup ./TrackServer& && pidof TrackServer'");
EDIT:
I found a working solution:
ob_start();
passthru("/bin/bash -c 'cd /opt/server/TrackServer/release && nohup ./TrackServer&' > /dev/null 2>&1 &");
passthru("pidof TrackServer");
$pid = ob_get_clean();
The command was stopping because:
Run multiple exec commands at once (But wait for the last one to finish)
PHP's exec function will always wait for a response from your
execution. However you can send the stdout & stderror of the process
to /dev/null (on unix) and have these all the scripts executed almost
instantly.

Run Script in Background Linux Server w/ PHP exec()

I'm trying to trigger a PHP script to run in the background using the exec() function but I cannot get it to work. I've read countless posts on stack overflow and other forums and tried many variations to no avail.
Server Info:
Operating System: Linux
PHP: 5.2.17
Apache Version: 2.2.23
Home Directory: /home1/username
I'm currently using the code:
exec("/home1/username/php /home1/username/public_html/myscript.php > /dev/null &");
When I run the above script I get no error_log and no error in my cPanel error log, however the script definitely doesn't execute. When I browse to http://www.mydomain.com/myscript.php it runs and e-mails me instantly. Any idea why this isn't working / how I can find out what error is being produced?
Update cPanel Process Manager Output
exec("php /home1/username/php /home1/username/public_html/myscript.php > /dev/null &");
Produces:
27183 php /home1/username/php /home1/username/public_html/myscript.php
27221 [sh]
27207 php /home1/username/php /home1/username/public_html/myscript.php
27219 php /home1/username/php /home1/username/public_html/myscript.php
27222 php /home1/username/php /home1/username/public_html/myscript.php
27224 php /home1/username/php /home1/username/public_html/myscript.php
27249 sh -c php /home1/username/php /home1/username/public_html/myscript.php > /dev/null &
Is that normal? Script appears to hang around for a long time even though it should execute very quickly.
Couldn't get the exec working with php. Even when I got shell access to the server the command just hung. I decided to use wget instead which accomplishes the same thing. Works great :)
exec("wget http://www.mydomain.com/myscript.php > /dev/null &");
Have you tried invoking the php CLI directly?
exec("php /home1/username/php /home1/username/public_html/myscript.php > /dev/null &");
You will not need the #!, which would output to the browser if called through Apache.
EDIT.
It looks like your script is working, but your PHP script executing in the background is hanging (not exiting). Try this variation:
exec("php /home1/username/php /home1/username/public_html/myscript.php > /dev/null 2>&1 &");
What does “> /dev/null 2>&1″ mean?
since you want to run the myscript from your command line, wy not do this:
exec('(/home1/username/public_html/myscript.php) > /dev/null &',$r,$s);
And write this as a first line in the myscript.php:
#!/home1/username/php -n
<?php
//script goes here
?>
That should work. The hashbang tells the system what programme to use to run the script that follows, so you don't need to add that to your exec call. Also, it's safer (and therefore better) to put brackets around the full script call, just so PHP knows what output has to be redirected to what stream, to avoid any issues that might occur. Especially when libs or packages like PHP-GTK are installed on the server (hence the -n option).

Categories