I have the following problem:
I hava a xampp sever running and I want it to execute a powershell. A php triggers a .bat file which contains the following code:
#echo
cd C:\OpenBR\bin
start /WAIT br -algorithm FaceRecognition -compare C:\xampp\htdocs\upload C:\xampp\htdocs\DP C:\xampp\htdocs\results\result.csv
start /WAIT C:\xampp\htdocs\CSVconvert\sortieren.ps1
start /WAIT C:\xampp\htdocs\CSVconvert\Removedouble.ps1
start /WAIT C:\xampp\htdocs\CSVconvert\remove_path.ps1
start /WAIT C:\xampp\htdocs\CSVconvert\remove_foo.ps1
start C:\xampp\htdocs\CSVconvert\remove_quoatation.ps1
The first part works fine, up until the point when i want to exec the powershell "sortieren.ps1". When I run the batch manually, it executes and does the job, when triggered via php, it doesn't.
I set "Set-ExecutionPolicy Unrestricted" in both x86 and x64 shells.
I am just confused because the normal command line works and powershell doesn't, even after setting it on unrestricted.
I viewed
executing a Powershell script from php
and
PowerShell on Windows 7: Set-ExecutionPolicy for regular users
but couldn't solve the problem.
What did i miss?
The session you are running those commands in doesn't have the same environment variables as when you are using PowerShell to run them manually. You'll have to specify the absolute path to the powershell executeable and the scripts that you want to run so that they will be found.
start /WAIT C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe C:\xampp\htdocs\CSVconvert\sortieren.ps1
Since the problem was the environment i thought you might benefit from a package that handles that aspect automatically. Here is a project that allows PHP to obtain and interact dynamically with a real Powershell. Get it here: https://github.com/merlinthemagic/MTS
After downloading you would simply use the following code:
$shellObj = \MTS\Factories::getDevices()->getLocalHost()->getShell('powershell');
$strCmd1 = 'first command from first script';
$return1 = $shellObj->exeCmd($strCmd1);
$strCmd2 = 'second command from first script';
$return2 = $shellObj->exeCmd($strCmd2);
Instead of triggering a single script you can just trigger each command individually and handle the return. You can issue any command you like against the $shellObj, the environment is maintained throughout the life of the PHP script.
Related
I have git bash and I am making commits using it. I need however, to make the .bashrc alias, that will also run a php script, return a value N (which is a number) from that script, and use that value Nto change the name of a file like so:
nameOfFileN
How do I do this? I need this sequence of commands. It is my local windows machine. I have php installed. I use windows git bash to use git. I want to run this in it (in the git bash). I need this bash command together with the php file that just returns an echo-ed variable value.
If your PHP script is hosted in a webserver, use curl:
curl http://localhost/path/to/script.php
If it is a stand-alone script and you have the PHP CLI interpreter installed, run the script directly:
php /path/to/script.php
Both calls will give you the output of the PHP script. Use it however you need (pipe it to another process, redirect it to a file, use it for command substitution, assign it to a shell variable, …)
I'm trying to run a ROS shell program on the server through php on Ubuntu 14.04. I have tried using system, exec, shell_exec but nothing happens and I don't get any output. The system call is the following:
echo shell_exec("/opt/ros/indigo/bin/rosrun gazebo_ros spawn_model -database Part_A -gazebo -model Part_A");
What are the limitations of using system or exec to run any shell command through php on a server?
I don't care as much about the output of the command as for its execution. I think the problem has to do with the fact that PHP doesn't have any PATH like shell does so it can't find any applications without specifying the exact location. How can I make PHP se the same PATH shell does?
The problem was that the apache user and the environment in which the bash commands are running are not set up correctly. I followed the instructions from this answer but instead of using "source" I used "." and instead of using the source.bash file I used the source.sh file. I also set all the environment variables that had to do with ros or gazebo using the putenv() function.
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.
I am trying to get the path to certain exe's using the where command in the command prompt on windows.
Here is what i did in command prompt.
where g++
where java
where javac
where python
All of these are giving the correct output of the path in the console window which indicated that I have set the environment variables correctly.
But now When i try to run the commands using the shell_exec() function in PHP, only the call to where java and where python gives the correct output. I was even able to successfully execute a respective test file using these commands through PHP.
But strangely, where g++ and where javac give this error in the browser when run through PHP:
INFO: Could not find files for the given pattern(s).
Also if I get the outputs of these two commands on the console and then copy that into my script to compile a c++ or java file, it works perfect. But the where command returns the above INFO when run through the PHP script.
I am running the server on localhost using XAMPP. Any idea what is missing?
I would like to execute a cronjob for a routine task every X hours. The cronjob basically executes a shell script which in turn uses a WGET command to download files from a remote server. However, before I run this shell script I want the cronjob to execute a php script which will check whether the update's available (there's no point in wasting BW and downloading the same file over and over again) and if it is, it should pass on the update URL to the shell script which in turn uses the WGET command.
The cronjobs are set from the hosts Admin Panel. There is no other way around it. Being a shared hosting service, I am not allowed access to other functions on PHP which might do the task for me either.
Is this possible? I am Linux illiterate. I have installed a few RPM's on Fedora but that's about it. Please bear with me. Thanks!
Just pass --timestamping to your wget command.
Alternatively if you are more familiar with PHP's ways you can check this question for a usable method.
Use a curl HEAD request to get the file's headers and parse out the Last-Modified: header.
To use a php script as a regular command line executable use this as a starting point:
#!/bin/env php
<?php
echo "Hello World\n";
Save the file without the .php and tuck it somewhere that your server won't serve it.
Next, set the executable bit so that you can execute the script like a regular program
(u+x in the following command means grant the [u]ser e[x]ecute privileges for helloworld, and chmod is the command that unix variants use to set file permissions)
Omit the $ in the following sequence, as it represents the command prompt
$ chmod u+x helloworld
now you can execute your commandline script by calling it in the bash prompt:
$ ls
helloworld
$ ./helloworld
Hello World
$
From here you can get the full path of the executable script:
$ readlink -f helloworld
/home/SPI/helloworld
And now you can install the cronjob using the path to your executable script.