Execute sh script on linux with php - php

I'm stuck with the following problem:
I try to execute a sh script using php.
My attempts:
I can run other commands. As an example I can run the 'cat' programm to display the script I'd like to execute: <?php echo exec("cat /srv/web/scripts/start_server.sh"); ?>
The following snippets don't work:
<?php echo `/srv/web/scripts/start_server.sh`; ?>
<?php echo `sh /srv/web/scripts/start_server.sh`; ?>
<?php exec("sh /srv/web/scripts/start_server.sh"); ?>
<?php exec("/srv/web/scripts/start_server.sh"); ?>
The same with: system(), popen(), proc_open(), passthru(), shell_exec()
I tried various file permissions on the script. Even rwxrwxrwx and rwsrwxrwx.
PHP continues execution after the execution as if it worked. No PHP error is generated. When I start the script on the console (like: sh /srv/web/scripts/start_server.sh) it works.
I tested with a simple script, which works on the console but not when I try to execute it with php:
#!/bin/sh
echo hallo > /srv/web/scripts/file.txt
There is no safe-mode enabled. The exec function is nowhere disabled.
As an environment I use ipfire core 71 on an alix board. Apache as httpd and php5 as php interpreter.
I hope someone can help me.

Related

Run Php Code in Linux

I would like to run some php code in my linux scripts to do some automation.I have searched on Google but I can't find any resources on it.Can anyone suggest a way to do this?
#!/bin/bash
<?php echo "this is a test "?>
Error Message:syntax error near unexpected token 'newline'
You can run your script using command in bash on linux:
php /path/to/file.php
also you can put it into some bash script.
#!/usr/bin/php
<?php
echo 'foo';
use which php to find out where your php interpreter is hidden and make sure you can execute your file chmod u+x foo.php

Running shell commands in .php file from command-line

I have a series of shell commands I want to put in a program and execute the program from the command line. I decided to use PHP for this so currently I am trying to get the most basic shell commands to run.
Save as build.php
<?php
shell_exec('cd ..');
echo "php executed\n";
?>
From the command-line
php build.php
Output
php executed
Php executes correctly but I'm still in the same directory. How do I get shell_exec( ... ) to successfully call a shell command?
You need to change the cwd (current working directory) in PHP... any cd command you execute via exec() and its sister functions will affect ONLY the shell that the exec() call invokes, and anything you do in the shell.
<?php
$olddir = getcwd();
chdir('/path/to/new/dir'); //change to new dir
exec('somecommand');
will execute somecommand in /path/to/new/dir. If you do
<?php
exec('cd /path/to/new/dir');
exec('somecommand');
somecommand will be executed in whatever directory you started the PHP script from - the cd you exec'd just one line ago will have ceased to exist and is essentially a null operation.
Note that if you did something like:
<?php
exec('cd /path/to/new/dir ; somecommand');
then your command WOULD be executed in that directory... but again, once that shell exits, the directory change ceases to exist.

PHP and Apache : shell_exec(wkhtmltopdf with xvfb) command doesn't work

I try to run the wkhtmltopdf (0.11.0 rc1) with php (5.4.2) on apache (2.4.2).
When I try to launch wkhtmltopdf-i386 --use-xserver http://www.google.com google.pdf 2>&1, I can find my pdf. Here my php code
<?php
$cmd= '/usr/bin/wkhtmltopdf-i386 http://www.google.com google.pdf 2>&1';
$ret = shell_exec($cmd);
echo $ret;
?>
It works with apache and as command line php test.php.
Because my target page contains many images and some "heavy" js charts. I have got a Segmentation Fault with the wkhtmltopdf command when I try to turn it into pdf.
The only way to make it work is to use xvfb as X11 emulator. The code looks like this :
<?php
$cmd= '/usr/bin/xvfb-run /usr/bin/wkhtmltopdf-i386 --use-xserver http://www.google.com google.pdf 2>&1';
$ret = shell_exec($cmd);
echo $ret;
?>
This script works with the command line php test.php but it doesn't work with apache. If I take a look into the apache's process with htop, I can see that there are two process (with php test.php) :
xvfb
wkhtmltopdf
When I launch with apache I have only xvfb process. It finish by a timeout from apache because it's waiting the wkhtmltopdf process.
I can make it works with apache (2.2.21) and php (5.3.10).
Is there something that I'm missing ? Maybe something in the apache's config-files ?
I was having the same problem. I was using the exec function, but the same applies to shell_exec. The function execution was disabled in php.ini.
SOLUTION: Remove the shell_exec string from the disable_functions at php.ini file.
I am not sure why your second version is not callable from Apache (must not be using the same shell, since shell_exec uses a shell?), but as a work-around could you (from Apache PHP) shell_exec("php test.php"); and get your intended result?
Perhaps also try one of the other process execution functions such as pcntl_exec.
it's mostly because of ownership and permissions, try
su www-data (for debian)
php test.php
you'll probably see the error.

running a jar file in PHP

I have a command to be run like this
$command="java -jar ".dirname(__FILE__)."\gmksplit.jar"." ".$input_path." ".$output_path;
I have echoed the $command variable and I get the output as
java -jar X:\wamp\www\moodle\gmksplit.jar X:\wamp\www\moodle/upload/maze_4.gmk X:\wamp\www\moodle/outputs/maze_4;
which is exactly I want to run..
I am trying to run it as
echo $exec($command);
it is not running. I have tried all the functions like shell_exec() and system()
It gives the output as
Java Version: 10700 (1.7.0_01)
when i run the same line in command prompt I get the output as
Java Version: 10700 (1.7.0_01)
Loading lib files in X:\wamp\www\moodle\gmksplit.jar
01_move.lgl 02_main1.lgl 03_main2.lgl 04_control.lgl
05_score.lgl 06_extra.lgl 07_draw.lgl
time taken to load file: 254 ms
so, as you see my php code is giving only the first line as output. The command is not running properly and I am not getting the intended output.
please help me
I am using the wampp server
Can you try this:
<?php
$command="java -jar ".dirname(__FILE__)."\gmksplit.jar"." ".$input_path." ".$output_path;
$out = array();
exec = ($command, $out);
print_r($out);
?>

PHP shell_exec() - having problems running command

Im new to php shell commands so please bear with me. I am trying to run the shell_exec() command on my server. I am trying the below php code:
$output = shell_exec('tesseract picture.tif text_file -l eng');
echo "done";
I have the picture.tif in the same directory as the php file. In my shell I can run this without a problem.
It takes a while to run the code, then it doesnt make the text_file like it does when I run it in command prompt.
Per your comment:
Should I write a loop in shell
instead?
You can write a very simple shell script to run the command in a loop. Create the script file first:
touch myscript.sh
Make the script executable:
chmod 700 myscript.sh
Then open it with a text editor such as vim and add this:
for (( i = 0 ; i <= 5; i++ ))
do
tesseract picture.tif text_file -l eng
done
Thats the very basics of it (not sure what else you need), but that syntax should help get you started. To run the script, do this if you're in the same directory as the script:
./myscript.sh
Or specify the full path to run it from anywhere:
/path/to/mydir/myscript.sh
Could this be a permissions issue? My guess is that PHP isn't running with the same permissions that you do when you execute the command directly from the command prompt. What OS are you running on?

Categories