SVNANT Script Fails When Called By PHP Script - php

I have a shell script that called ant -buildfile /some/where/build.xml
That works fine. When I try to exec('shellScript'); or exec('ant -buildfile /some/where/build.xml'); it will fail. I have tried passthru, system, pcntl_exec, popen, and shell_exec with both the shell script and the command.
The build script uses SVN, and checks out some files. That's where it fails. In the ant script output, I get:
checkoutTrunk:
[svn] started ...
[svn] failed !
When I run the command or the shell script from SSH, everything works fine. Why would being called from a PHP script stop the checkout from working?

After getting nowhere, I changed the command it was trying to execute to 'id'. I found out that PHP was running as a different user than I thought. When I SSH as that user, I get an error when I run the command due to not having access to write to a directory. Once I changed permissions on those directories, I could then run the command as the PHP user from SSH. After I could do that, it also worked from the PHP script.

Related

PHP shell_exec() Behaves Differently Than Terminal Command Line MacOS

Scanline is a simple command line utility used for scanning documents from a twain scanner. http://blog.scottkleper.com/scanline-command-line-scanner-for-mac/
I am trying to use Scanline through a PHP script using shell_exec(); the same way that I would use it directly from the terminal in MacOS.
When I run Scanline directly from the command line, it detects all the attached scanners and prints them out ./scanline -list
When I run Scanline using shell_exec(), it does not detect any devices.
So far, I have changed the apache user to my local user, and have added the local user to the sudoers file. If I run 'whoami' in shell_exec() it is the same result as if I run it in command line.
I have printed the environment using printenv in command line, and have setup all of the same variables in my php script before executing shell_exec() using putenv(); If I run shell_exec('printenv 2>&1'), it is the exact same environment as when I run printenv in command line.
All the permissions are correct and allow access, and scanline runs when executed through shell_exec() without error (I checked apache's error logs, as well as put a error_reporting(E_ALL); in the top of the PHP file to printout any issues along the way). The only difference in how the program is executed is that in command line the devices are detected, and run through shell_exec(), no devices are found.
Any ideas as to what else I could be missing between command line and using shell_exec() ?
I also tried using system(), exec(), and shell_exec() interchangeably with the same result.

PHP shell_exec won't call Windows 10 bash script

I'm running a CakePHP project under XAMPP (Apache) on Windows 10 Anniversary Update.
Apache is running under my user account.
The app calls several external processes via shell_exec(): ImageMagick, phantomjs execute as expected.
I also want to call a bash script, that in turn calls ImageMagick under Ubuntu bash (installed separately, via apt-get). I've had to adjust all paths into a form that bash can resolve.
bash /mnt/e/Projects/project-name/website/bin/crop_to_aspect.sh 1 1
/mnt/e/Projects/project-name/website/webroot/images/agent_photo/tmp_ed564289-a6f6-45b7-b9f3-2aec2b8bb3d1.jpg
/mnt/e/Projects/project-name/website/webroot/images/agent_photo/ed564289-a6f6-45b7-b9f3-2aec2b8bb3d1.jpg```
The command fails when called via shell_exec(). The same command, written to CakePHP's log, and then called from a cmd.exe prompt, works perfectly.
Thinking it may have been a path issue, I wrapped the same script in a windows batch file, including the full path to bash. I called the full batch file path, ie:
#echo off
SET aw=%1
SET ah=%2
SET in=%3
SET out=%4
C:\Windows\System32\bash.exe /mnt/e/Projects/project-path/website/bin/crop_to_aspect.sh %aw% %ah% %in% %out%
This script is then called:
E:\Projects\project-path\website\bin\crop_to_aspect.bat 4 3
/mnt/e/Projects/project-path/website/webroot/images/listing_photo/tmp_ed564289-a6f6-45b7-b9f3-2aec2b8bb3d1.jpg
/mnt/e/Projects/project-path/website/webroot/images/listing_photo/ed564289-a6f6-45b7-b9f3-2aec2b8bb3d1.jpg
Once again, the command executes correctly in cmd.exe but does nothing when run via shell_exec() from the PHP script.
Check if a function is not in the list of functions disabled in php.ini (disable_functions) line

Execute a bash file located on a different VPS

I have a php webpage located on Webserver1, which is from Host1.
I also have a bash script located in Gameserver1 which is from Host2.
Is there any way to send a command from Webserver1 to Gameserver1 to execute the bash file? The webpage and file are on different VPSs. Both are running Debian 7.
The script is literally one line, to execute a java command via a screen, so the server can start if a player notices it's down. The command's available already so it doesn't need to be a secure way of hiding what the command is.
There are 2 ways I can think of. either create a bash file in Webserver1 that connects through ssh and executes the bash script you need on Gameserver1. then run it through php with exec() command.
Or you can create a php file in Gameserver1 that uses exec() to execute the bash script you need on Gameserver1 and call it using file_get_contents() on Webserver1, which is not that secure since anyone can call that file and run your script.

PHP shell_exec() issue

I am having an issue using the PHP function shell_exec().
I have an application which I can run from the linux command line perfectly fine. The application takes several hours to run, so I am trying to spawn a new instance using shell_exec() to manage better. However, when I run the exact same command (which works on the command line) through shell_exec(), it returns an empty string, and it doesn't look like any new processes were started. Plus it completes almost instantly. shell_exec() is suppose to wait until the command has finished correct?
I have also tried variations of exec() with the same outcome.
Does anyone have any idea what could be going on here?
There are no symbolic links or anything funky in the command: just the path to the application and a few command line parametes.
Some thing with you env
See output of env from cli (command line interface) and php script
Also see what your shell interpreter?
And does script and cli application runs from one user?
If so, se option safe_mode
Make sure the user apache is running on (probably www-data) has access to the files and that they are executable (ls -la). A simple chmod 777 [filename] would fix that.
By default PHP will timeout after 30 sec. You can disable the limit like this:
<?php
set_time_limit(0);
?>
Edit:
Also consider this: http://www.rabbitmq.com/

How to run a php script through the command line (and keep it running after logging out)

I am trying to run a php script on my remote Virtual Private Server through the command line. The process I follow is:
Log into the server using PuTTY
On the command line prompt, type> php myScript.php
The script runs just fine. BUT THE PROBLEM is that the script stops running as soon as I close the PuTTY console window.
I need the script to keep on running endlessly. How can I do that? I am running Debian on the server.
Thanks in advance.
I believe that Ben has the correct answer, namely use the nohup command. nohup stands for nohangup and means that your program should ignore a hangup signal, generated when you're putty session is disconnected either by you logging out or because you have been timed out.
You need to be aware that the output of your command will be appended to a file in the current directory named nohup.out (or $HOME/nohup.out if permissions prevent you from creating nohup.out in the current directory). If your program generates a lot of output then this file can get very large, alternatively you can use shell redirection to redirect the output of the script to another file.
nohup php myscript.php >myscript.output 2>&1 &
This command will run your script and send all output (both standard and error) to the file myscript.output which will be created anew each time you run the program.
The final & causes the script to run in the background so you can do other things whilst it is running or logout.
An easy way is to run it though nohup:
nohup php myScript.php &
If you run the php command in a screen, detach the screen, then it won't terminate when you close your console.
Screen is a terminal multiplexer that allows you to manage many processes through one physical terminal. Each process gets its own virtual window, and you can bounce between virtual windows interacting with each process. The processes managed by screen continue to run when their window is not active.

Categories