How to run a PHP script with command line globally, like:
update var1=abc var2 var3=def
"Update" located in fact: "C:\Localhost\Scripts\Update.php"
Many sources suggest this as:
php update.php var1=abc var2 var3=def
Shortly: How to run a PHP script as if it was a registered executable?
Disclaimer: I've written this answer for Linux/Ubuntu, I'm not sure if it'll work for Windows (the OP updated the question's requirements).
So, this is possible, the way I've described here basically just has a global bash script call a PHP file. Here's the steps:
Make a new bash script:
#!/usr/bin/env bash
php /path/to/yourphpfile.php
Change the file executable: chmod +x /path/to/yourbashscript
Copy the bash script into /usr/local/bin/: sudo cp /path/to/yourbashscript /usr/local/bin/
Open a new terminal, then run yourbashscript (or whatever you named it).
And voila! A globally available PHP script!
I finally solved it:
-Create a .bat file where your php file in, it contains:
#ECHO OFF
php %~dp0/update.php %*
-And save it as "update.bat"
-Add the directory name into "Path" in "Environment Variables"
Now you can write "update var1=abc var2 var3=def" freely in command prompt!
Related
I am not preferring using PEAR method, so I decided to use another way, However, I still cannot make a perfect .bat file to run in two situations.
I downloaded the phpunit.phar and the path I had added into PATH => C:\dev
e.g C:\dev\phpunit.phar
I also create a phpunit.bat file inside C:\dev\phpunit.bat, this allow me to run phpunit in any place.
This is the content of the phpunit.bat
#echo off
php c:\dev\phpunit.phar
I have run perfectly result when I am using the guard-phpunit. However, the problem is when I want to pass an argument / parameter to the phpunit it does not work.
E.g I got a testing file in C:\testing\CalculatorTest.php
When I run
C:\testing>phpunit C:\testing\CalculatorTest.php
or
C:\testing>phpunit CalculatorTest.php
It only output like php phpunit.phar, it does not take the argument.
It only work when I enter the command like this
C:\testing>php c:\dev\phpunit.phar CalculatorTest.php
I try to edit the phpunit.bat file to make it like below:
#echo off
set arg1=%1
:start
if "%1"=="" (goto :main)
REM without any argument
php C:\dev\phpunit.phar
goto :end
:main
php C:\dev\phpunit.phar %arg1%
:end
The above code will work in C:\testing>php c:\dev\phpunit.phar CalculatorTest.php, but when run in guard-phpunit it will keep prompting option --include-path requires an argument error.
Anyone know how to fix the .bat so it able to run in both situation?
Thank you so much!
I have created phpunit.bat file in same directory of the phpunit are instaled, for example B:\libs, containing:
#ECHO OFF
php "%~dp0phpunit.phar" %*
and in the windows PATH variable I added ;B:\libs:
Hold Win and press Pause.
Click Advanced System Settings.
Click Environment Variables.
Append ;B:\libs to the Path variable.
Restart Command Prompt.
After restart the terminal PHPunit works:
PS C:\Users\joridos> phpunit.bat --version
PHPUnit 4.1.3 by Sebastian Bergmann.
Use for phpunit.bat:
#echo off
php c:\dev\phpunit.phar %*
%* is replaced on execution by whatever was passed to phpunit.bat which means nothing if nothing was passed to phpunit.bat, or first + second + third + ... arguments exactly as specified on calling phpunit.bat.
I am trying to Execute a multiple commands in php using exec() and shell_exec but i am getting a null value back which i shouldn't and nothing is happening (if i copy and paste the strings below in the command line it will work fine and accomplish the job needed) this is the commands i am using:
$command = "cd /../Desktop/FolderName;";
$command .= 'export PATH=$PATH:`pwd`;';
$command .= 'Here i execute a compiler;';
and then i use the escapeshellcmd()
$escaped_command = escapeshellcmd($command);
then
shell_exec($escaped_command);
any ideas what i am doing wrong and i also tried escapeshellarg() instead of escapeshellcmd()?
Solution: the Problem was the permission of the execution compiler for other owners is non and this was the problem.
because when you are using exec() function in php the owner of the file will be www-data so you need to give permission for the www-data either from the ACL of ubuntu or whatever linux based operating system(you can know the owner by doing this exec('whoami')), or by the files you need to execute.
(Sorry my bad English)
On Linux you can add your Commands in a Shell Script.
You can put this in any file:
#!/bin/bash
cd /../Desktop/FolderName
export PATH=$PATH:`pwd`
EXECUTE COMPILER
And save this as fille.sh
Then, add execution permissions:
chmod +x path/to/file.sh
From PHP, you can call this Script executing:
shell_exec('sh path/to/file.sh');
Hope this helps!
I have a shell script which works perfectly if I run it in the terminal (MAC OSX)
#!/bin/bash
cd /Applications/XAMPP/xamppfiles/htdocs/chemedit/
babel -imol 'a.mol' -oinchi 'outputfile.inchi'
babel -imol 'a.mol' -osmi 'a.smsi'
babel a.smi -O out.svg -xC -xe
exit
I have this in a file called a.sh
I want to run this from PHP using:
$output = shell_exec("bash a.sh 2>&1");
This does not work and returns:
Cannot write to outputfile.inchi
0 molecules converted
1 errors
for all files
I have given both files chmod 777.
I am pretty sure safe mode is off for PHP.
The babel command is likely not in the PATH environment variable for the user running PHP, and thus the script via PHP. The simplest solution is to edit your shell script to refer to babel by its full path.
Try calling babel with its absolute name. Use which babel to determine this, and replace 'babel' with it in your script.
I have a script in /var/www/myscript.sh which creates folders and runs the command svn update for my projects. I need to execute this script by calling it in a PHP file in the browser (i.e. Localhost/test.php). I tried using functions shell_exec() and exec() but those did not work. I ran my shell script in terminal with su www-data && ./myscript.sh and it worked. What else am I missing?
<?php
$output = shell_exec("./myscript.sh");
?>
Update 5/4/2011:
I added www-data ALL=(ALL) NOPASSWD:ALL to /etc/sudoers and it works, but this is very insecure. Is there another way to do this?
Several possibilities:
You have safe mode enabled. That way, only exec() is working, and then only on executables in safe_mode_exec_dir
exec and shell_exec are disabled in php.ini
The path to the executable is wrong. If the script is in the same directory as the php file, try exec(dirname(__FILE__) . '/myscript.sh');
You might have disabled the exec privileges, most of the LAMP packages have those disabled. Check your php.ini for this line:
disable_functions = exec
And remove the exec, shell_exec entries if there are there.
Good Luck!
Residuum did provide a correct answer to how you should get shell exec to find your script, but in regards to security, there are a couple of points.
I would imagine you don't want your shell script to be in your web root, as it would be visible to anyone with web access to your server.
I would recommend moving the shell script to outside of the webroot
<?php
$tempFolder = '/tmp';
$webRootFolder = '/var/www';
$scriptName = 'myscript.sh';
$moveCommand = "mv $webRootFolder/$scriptName $tempFolder/$scriptName";
$output = shell_exec($moveCommand);
?>
In regards to the:
i added www-data ALL=(ALL) NOPASSWD:ALL to /etc/sudoers works
You can modify this to only cover the specific commands in your script which require sudo. Otherwise, if none of the commands in your sh script require sudo to execute, you don't need to do this at all anyway.
Try running the script as the apache user (use the su command to switch to the apache user) and if you are not prompted for sudo or given permission denied, etc, it'll be fine.
ie:
sudo su apache (or www-data)
cd /var/www
sh ./myscript
Also... what brought me here was that I wanted to run a multi line shell script using commands that are dynamically generated. I wanted all of my commands to run in the same shell, which won't happen using multiple calls to shell_exec(). The answer to that one is to do it like Jenkins - create your dynamically generated multi line of commands, put it in a variable, save it to a file in a temp folder, execute that file (using shell_exec in() php as Jenkins is Java), then do whatever you want with the output, and delete the temp file
... voila
If you are having a small script that you need to run (I simply needed to copy a file), I found it much easier to call the commands on the PHP script by calling
exec("sudo cp /tmp/testfile1 /var/www/html/testfile2");
and enabling such transaction by editing (or rather adding) a permitting line to the sudoers by first calling sudo visudo and adding the following line to the very end of it
www-data ALL=(ALL) NOPASSWD:/bin/cp /tmp/testfile1 /var/www/html/testfile2
All I wanted to do was to copy a file and I have been having problems with doing so because of the root password problem, and as you mentioned I did NOT want to expose the system to have no password for all root transactions.
How we run php script using Linux bash?
php file test.php
test.php contains:
<?php echo "hello\n" ?>
From the command line, enter this:
php -f filename.php
Make sure that filename.php both includes and executes the function you want to test. Anything you echo out will appear in the console, including errors.
Be wary that often the php.ini for Apache PHP is different from CLI PHP (command line interface).
Reference: https://secure.php.net/manual/en/features.commandline.usage.php
First of all check to see if your PHP installation supports CLI. Type: php -v. You can execute PHP from the command line in 2 ways:
php yourfile.php
php -r 'print("Hello world");'
There are two ways you can do this. One is the one already mentioned, i.e.:
php -f filename.php
The second option is making the script executable (chmod +x filename.php) and adding the following line to the top of your .php file:
#!/path/to/php
I'm not sure though if a webserver likes this, so if you also want to use the .php file in a website, that might not be the best idea. Still, if you're just writing some kind of script, it is easier to type ./path/to/phpfile.php than having to type php -f /path/to/phpfile.php every time.
Simply this should do:
php test.php
just run in linux terminal to get phpinfo .
php -r 'phpinfo();'
and to run file like index.php
php -f index.php
php -f test.php
See the manual for full details of running PHP from the command line
php test.php
should do it, or
php -f test.php
to be explicit.
I was in need to decode URL in a Bash script. So I decide to use PHP in this way:
$ cat url-decode.sh
#!/bin/bash
URL='url=https%3a%2f%2f1%2fecp%2f'
/usr/bin/php -r '$arg1 = $argv[1];echo rawurldecode($arg1);' "$URL"
Sample output:
$ ./url-decode.sh
url=https://1/ecp/