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

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

Related

Why update nohup.out when running nohup in exec 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);

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.

Run compiled python script from PHP

I need to run a python script, compiled with pyinstaller via a PHP generated webpage.
I tried shell_exec(), exec() and system() without success.
I regularly run the script from terminal in background using:
temperature_sensor_code > /dev/null 2>&1
I've added www-data user to sudoers. I know it's not a good way but I need it in order to send killall temperature_sensor_code command (this is works).
This is my situation:
<?php
$run = escapeshellcmd('temperature_sensor_code > /dev/null 2>&1');
shell_exec($run);
header("Refresh: 0; URL=index.php");
?>
I've made a symlink in /usr/bin, also tried with the full path of the script with no luck.
UPDATE: to make it simpler, i've created a simple sh script run.sh and put in /var/www and make it RUN with
shell_exec("/var/www/run.sh");
this is working for me. So I put my script temperature_sensor_code in /var/www but this is not working. If I add var_dump(exec("/var/www/temperature_sensor_code/temperature_sensor_code"));
gives me: string(0) ""
I think there are problems with the compiled python script because the PHP side seems to be OK.
escapeshellcmd() does this:
Escape shell metacharacters
$run = escapeshellcmd('temperature_sensor_code > /dev/null 2>&1');
var_dump($run);
string(43) "temperature_sensor_code \> /dev/null 2\>\&1"
But you have shell metacharacters that you do want to behave as shell metacharacters:
temperature_sensor_code > /dev/null 2>&1
^ ^^^^
You're also doing no troubleshooting at all:
You discard all command output (that's what sending it to /dev/null does)
You don't get the return code
I suggest to:
Switch to exec() and make sure you use all its arguments and not just the mandatory ones
Get rid of > /dev/null until you diagnose the issue

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.

Exec to launch php as background process

I am struggling with getting a php file to run in the background with PHP's exec(). As a first test, I tried :
exec("ls -l > logfile.txt 2> errfile.txt &");
That works fine. logfile.txt gets filled with a directory listing.
Per instructions in the php documentation, since the exec kicks off a process that runs in the background, standard out (and standard error) are redirected to a file.
Now, I try
exec("/usr/bin/php -f /path/to/my.php > logfile.txt 2> errorfile.txt &");
It appears nothing happens.
Here are test files that I'm trying:
alpha.php
<?php
$version="a";
// Go do something we do not need to wait for.
exec("/usr/bin/php -f /path/to/beta.php > logfile.txt 2> errorfile.txt &");
?>
<html>
<head><title>Test</title></head>
<body>
<p>This is Alpha version <?php echo $version; ?></p>
</body>
</html>
beta.php
<?php
if (!($fp = fopen('/home/johnst12/public_html/workshops/admin/betadata.txt', 'w'))) { exit;}
fprintf($fp, "Proof that Beta executed.");
fclose($fp);
?>
If I run beta.php directly, it works fine. Betadata.txt gets the message.
If I run alpha.php to launch beta.php, betadata.txt is not created. logfile.txt and errorfile.txt remain empty (expected).
I am sure that the path to php, and the path to my php file are correct.
Googling for clarification has not been fruitful. A couple of common themes seem to be (a) running out of resources? (b) lack of permission on the target php file? Out of resources seems unlikely. The permission on the script is global read 644 (rw-r--r--). I tried adding execute (755) just in case it would help. It made no difference.
PHP version 5.3.21
Linux/Apache system.
safe_mode Off
What am I missing? Thanks.
First of all : Have you verified that /usr/bin/php is the correct path to PHP?
Php doesn't like running like that. Something to do with stdin. Try with nohup:
exec("nohup /usr/bin/php -f /path/to/beta.php > logfile.txt 2> errorfile.txt &");
With -f anything else that looks like a flag will go to PHP, so if you wanted to pass a "-x" option to your script then you'd have to
/usr/bin/php -f /path/to/beta.php -- -x
Without, options before the filename go to PHP and after go to the script.
/usr/bin/php /path/to/beta.php -x
I assume you've already looked at the two files in case they have output or errors?
A few other things to check:
Delete the two files. Are they recreated each time this code runs?
exec("nohup /usr/bin/php -v > logfile.txt &");
should output version information to that log file.
exec("/usr/bin/php -f /path/to/beta.php > logfile.txt 2> errorfile.txt");
should run the script properly (but not in the background).

Categories