LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   shell commands with php (http://www.linuxquestions.org/questions/programming-9/shell-commands-with-php-779786/)

unknown1 01-04-2010 02:43 PM

shell commands with php
 
Hello, I am trying to create a screen capture program with Xvfb server, ImageMagick and Khtml2PNG.
I seem to be having some issues and was hoping someone could help me out.

I have everything installed and working on a centos WHM/CPanel system and am able to generate the screen shots and save them to root folder from shell with the following commands.

Xvfb :2 -screen 0 1024x768x24&
export DISPLAY=localhost:2.0
khtml2png2 --sw 200 --sh 150 http://www.example.com example.png

It works 100% fine when you run it from shell.

I have created the following php script to run the exact same commands but get no results.


$connection = ssh2_connect('xxxxxxxxxxxxxx', 22);
ssh2_auth_password($connection, 'root', 'xxxxxxxxxxxxxx');

$stream = ssh2_exec($connection, 'Xvfb :2 -screen 0 1024x768x24&');
sleep(15);
$stream = ssh2_exec($connection, 'export DISPLAY=localhost:2.0');
sleep(2);
$stream = ssh2_exec($connection, 'khtml2png2 --sw 200 --sh 150 http://www.example.com example.png');
sleep(7);

echo "All done!";


Does anyone know why this would work fine from shell but NOT work at all when I run it from the php script?

Thanks in advance!

alunduil 01-10-2010 02:03 PM

Are you running those commands as root on the CLI as well? Also, why do you use ssh2_connect rather than system or exec or popen?

Regards,

Alunduil

unknown1 01-10-2010 11:33 PM

Well I am using ssh2 to run the commands cause it's the only way I know how to run them as root. Do you know of another way to login as root though php without ssh2_connect? When I use system() or exec() it's always as user nobody when I do whoami... any suggestions?


Quote:

Originally Posted by alunduil (Post 3821527)
Are you running those commands as root on the CLI as well? Also, why do you use ssh2_connect rather than system or exec or popen?

Regards,

Alunduil


alunduil 01-11-2010 11:08 AM

Setup up a sudoers to allow your php user to execute the particular commands you would like and then you can use system or exec.

That's the only solution to that I've found as well, but then it's not executing over the network which is slightly better.

Regards,

Alunduil

unknown1 01-12-2010 12:50 PM

I have tried to set it up like the following but still doesn't work.

www-data ALL = NOPASSWD: export DISPLAY=localhost:2.0
www-data ALL = NOPASSWD: /usr/bin/Xvfb
www-data ALL = NOPASSWD: /usr/local/bin/khtml2png2
www-data ALL = NOPASSWD: /root

Am I doing this right?





Quote:

Originally Posted by alunduil (Post 3822631)
Setup up a sudoers to allow your php user to execute the particular commands you would like and then you can use system or exec.

That's the only solution to that I've found as well, but then it's not executing over the network which is slightly better.

Regards,

Alunduil



All times are GMT -5. The time now is 08:00 AM.