How to stop PHP file execution with exec or shell_exec - php

I have triggered a PHP file which has an infinite loop, with:
shell_exec('php '.__DIR__.'/serv.php 2>&1 > /dev/null &');
and now I can't to stop it.
Is there any way to do it with 'shell_exec' or 'exec'?

PHP is a service. You can either run killall php or killall php-cgiin shell or, if you are using a program like MAMP or WAMP, you can simply close and reopen the program. Also, if you are using a later version of PHP there should be infinite loop failsafes. Check your php.ini for max_execution_time to see how long the script should be allowed to run. You can also set this in your file using the set_time_limit function.

just run the following on your shell
killall php

Related

Run MTProxy remotely by PHP exec on Windows server

I installed PM2 and node.js on the Windows server to run JSMTProxy-master script.
The command for the run proxy is: (pm2 start "JSMTProxy-master\mtproxy.js" -i max) and it executed successfully in the CMD window with no problem.
Now I want to execute this command by PHP and exec.
I tried this:
exec ('pm2 start "JSMTProxy-master\mtproxy.js" -i max');
Or this: (just showed up cmd.exe in taskmgr)
exec ('c:\WINDOWS\system32\cmd.exe /c START "pm2 start c:\mypath\JSMTProxy-master\mtproxy.js -i max"');
Or this:
exec ('c:\WINDOWS\system32\cmd.exe /c START "JSMTProxy-master\mtp-run.bat"');
These don't work and nothing happens. no error, no process showed up in taskmgr.
How is it supposed to be to run by exec?
Note. I have PHP installed and working perfectly.
After trying different ways many times, Finally i found the problem.
"PM2" doesn't execute under PHP exexc. It has to be with full path of exe files.
I changed my command as below and it worked:
shell_exec('C:\Users\Administrator\AppData\Roaming\npm\pm2.cmd start "C:\mypath\JSMTProxy-master\mtproxy.js" -i max')

PHP exec on local script

Hello i have a PHP script, and its added to cron, it is possible to execute from this script shell command (with exec() or something) without enabling it on php.ini? I don't want to enable exec on my site
It's called PHP CLI, check here
Usually when you install php, there's option to install php_cli too.
So long you can run php on shell prompt, then it can work.
Open bash (or other shell), try this:
php -v
If the version printed, then it's working.
Then you can
php -f phpfile
or put
#!/usr/bin/php
At the beginning of your php file as a line, and chmod +x file.php, and then
./file.php
#or
/path/to/file.php
to run it.
(Note /usr/bin/php is the usual place of php executable, it might change, eg in unix is ually /bin/php. Use whereis php to check its place.)

php shell_exec return null

I want run php ../cat1/index.php gr_s2/3/gr-n40 1200 command by php.
The shell_exec return NULL as result but when I try that command on cmd, output was shown correctly.
What happen in php and shell_exec ?!
Note: The command with different parameters (Like: php ../cat1/index.php gr_s2/3/gr-n40 800) works correctly in both (php and cmd).
There is a note in php manual page of shell_execute:
Note: This function can return NULL both when an error occurs or the
program produces no output. It is not possible to detect execution
failures using this function. exec() should be used when access to the
program exit code is required.
Source: http://php.net/manual/en/function.shell-exec.php
So your code running with error. Try with exec.
If you want, insert your code (or blocks) to be checked.
This is fixed by adding user used by web-server to sudoer and run the php command with
sudo php ..........
To start run this command sudo visudo
Add the following line at the end of sudoer file opened
www-data ALL=NOPASSWD: /usr/bin/php
But if you whant to execute all commad from php add this line instead of the above www-data ALL=NOPASSWD: ALL which is not recommended
and the run your commands with sudo php /path/to/you/file.php
For my case I was running ubuntu 14.04
Have fun please

PHP After Apache Shuts Down?

I'm running an Apache server on CentOS and would like to be able restart the webserver from a protected page using the following:
PHP:
<?php
ignore_user_abort(true);
shell_exec('sh sh/restart.sh');
?>
restart.sh:
service httpd restart
My question is if the web server shuts down and the PHP stops executing will the sh script continue running to bring the web server back online?
You should be fine since Apache doesn't shutdown until after the command is issued. But if you really wanted to be safe, use nohup:
shell_exec('nohup sh sh/restart.sh');
If your PHP runs as apache module, then once you kill httpd your script will be terminated instantly. So you need to delegate restart to i.e. command line script (called using exec() or shell_exec())
You might be able to add an & at the end of the command. This will fork the process and run it in the background. This way it will not depend on apache still running.
shell_exec('sh sh/restart.sh &');
If this works, you should not need ignore_user_abort().

Run command from PHP, exit script, but keep command running

I am running a PHP script that uses the passthru function to run system commands in the background. By redirecting the output and putting the & at the end of the command, the script is able to continue executing without waiting for the system command to finish. However, the PHP script itself does not exit until all commands are complete. Is there a way that the PHP script can start a system command and then not have to wait for that command to finish before exiting?
Have you tried using nohup?
Instead of
my_command >/dev/null &
do
nohup mycommand >/dev/null &

Categories