I have the following bash script:
#!/bin/bash
export DISPLAY=:0.0
chromium-browser --incognito --kiosk https://www.google.de/
If I run it from terminal ./start_chromium.sh Chromium opens as expected.
Now I want to run this script using exec() in a PHP-file:
<?php
exec('/home/pi/start_chromium.sh');
?>
Unfortunately nothing happens.
Checking the error log of Apache2, I see the following error message:
(chromium-browser-v7:1992): Gtk-WARNING **: 09:33:27.850: cannot open display: :0.0
Can anybody help me?
Thanks in advance.
Regards
Lars
This may be an Xauth issue. To test, you can run (from your terminal)
DISPLAY=:0.0 ; xhosts +
This is not a recommended long term fix as it completely disabled access control to your X server.
Related
I am running PHP and Apache. My program is as follows:
<?php
echo shell_exec("ssh");
?>
It works from the command line as php script.php but when visiting the web browser 127.0.0.1/script.php it returns an empty result.
Any ideas why this may be happening?
Edit 1:
I tried running ssh as the www-data user through command line. This worked fine.
Edit 2:
I tried running sshpass instead of ssh (same location, same permissions) and it works but ssh does not.
Mentioned in kamermans's note, the help message of ssh is output to the stderr stream, so try
echo shell_exec('ssh 2>&1');
I've been unable to run php scripts that I need to use to start and stop webcam services that run on the local machine with the scripts. I can find nothing in the logs to indicate why the script doesn't' work.
I confess to being severely handicapped regarding PHP, especially server-side scripting.
The environment is Debian Jesse running Nginx with all required SSH and PHP modules installed
I have added www-data to the sudoers file with:
www-data ALL=(ALL) NOPASSWD: /var/www/html/start_webcam.sh
Enabled the $PATH environment for www-data at:
/etc/php5/fpm/pool.d/www.conf
The shell script resides in the .../html directory and runs from the terminal with no issues.
This is the code for both the php and shell scripts:
start_webcam.php:
<?php
echo exec('sudo bash /var/www/html/aspirebox/start_webcam.sh 2>&1, $output');
print_r($output);
?>
The $output and print_r stuff is there because it was the last thing I tried based on a post I found out here somewhere.
start_webcam.sh
#!/bin/bash
service motion start
Thanks in advance to anyone out here that has a clue. After 2 days of wrestling with this, I am sure that I do not.
according to Passing Variables to shell_exec()? you should change your code like this:
<?php
$output = exec('/var/www/html/aspirebox/start_webcam.sh 2>&1 ');
print_r($output);
?>
and let your bash script execute as all (no need to sudo bash):
chmod a+x /var/www/html/aspirebox/start_webcam.sh
Thank you very much - that worked.
I worked through getting the path straight for the directory the shell script runs in, and the correct path to run "service".
All I have now is to figure out why I'm getting "Failed to start motion.service: Access denied"
I've given www-data permission to run the script without a password on sudoers, have to keep digging.
Thanks again!
I am using web-socket in php to run a chat service problem is when i close the command line the connection also close , is there a solution so that the chat service can run forever .
i am using this command
php -q server.php
create a php file on your server name it as start_server.php
and post this content
<?php
$shell_command = "php -q server.php > /dev/null &";
shell_exec($shell_command);
?>
now execute this command
php start_server.php
I am using screen for running php script and so far I haven't really faced any issues.
screen is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells). Each virtual terminal provides the functions of a DEC VT100 terminal and, in addition, several control functions from the ISO 6429 (ECMA 48, ANSI X3.64) and ISO 2022 standards (e.g. insert/delete line and support for multiple character sets). There is a scrollback history buffer for each virtual terminal and a copy-and-paste mechanism that allows moving text regions between windows.
More about screen here.
One drawback with using screen is that if you make a change in your script you will have to terminate the virtual terminal and create a new one for the changes to take effect.
Here is a sample code:
$ screen -L php /path/to/php/script
This will create a new virtual terminal.
Some useful commands:
To detach screen (once you are in the virtual terminal): ctrl + a + d
To resume screen (from your main terminal once detached): $ screen -r
If there are multiple screens running on your machine the above command should be followed by the pid.tty.host string which is unique for each screen
To terminate screen: ctrl + c
To list pid.tty.host strings of each terminal: $ screen -ls
i read a lot about that, but my problem still remain:
what i'm trying to do is:
echo shell_exec("/usr/bin/nohup /usr/bin/java -Xmx1g -jar /var/www/html/myDir/ff.jar &");
it gives me that error:
Error occurred during initialization of VM Could not reserve enough space for code cache
i use nohup because jar must run even if php terminate.
What could be the problem?
Do you have SELinux enabled on the machine it's running on? See http://archimedeseureka.blogspot.com/2011/01/executing-java-from-php-in-rhelcentos.html - I had this trouble on a Fedora 18 system and setting httpd_execmem seems to have fixed it.
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.