Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Robert Walker
Robert Walker
17,146 Points

PHP shell_exec function and Java Runtime error.

I recently made a post about being able to run SSH commands using PHP and while looking through a load of articles and reading up I found that it is rather easy to do this using the shell_exec function built into PHP.

So a friend of mine has a media server he uses for streaming video and so on and sometimes it crashes, hes not very good with computers and normally needs myself or someone from the hosting company to restart it for him, I thought surely there must be an easier way to do this so he doesn't have to keep asking others for help.

I made a button with ajax call to run a script to start the media server up when it stops:

$command = "cd /home/jimmy12/mediaserver/server; ./mss.sh start";
$output = shell_exec($command);
echo "<pre>$output</pre>";

The problem is while it works for other things like killing a process or using ls when I run the server start command I get this issue:

Starting Xmedia server...
Cannot find Java runtime

If I login to the SSH (using Putty) and run the same command it says:

Starting Xmedia server...
Using JAVA_HOME:  /usr/local/jdk
Using Java:       /usr/local/jdk/bin/java

Which works perfectly fine, does anyone know how I can get it to use the Java runtime correctly through the PHP script?

3 Answers

One way is in your bash script you could have it call java with the the path in it.

IE.

mss.sh

/usr/bin/java -jar blah.jar

Also make sure you log in with the same user, and that the environmental variables are the same, mainly path and I think java_home can't remember right off the top of my head. But you could catch what it says when you do the env command with your script and see if it loads the variables correctly. If not you could always set your java_home in your script command. With

export JAVA_HOME=/usr/java/jdk1.7.0_00/bin/java
Robert Walker
Robert Walker
17,146 Points

I had to change in the ./mss.sh file to the correct path for Java was my mistake for not thinking of it before.

However I now have a new issue after fixing that, it now prompts for a username and password and I have no idea how to enter them in a PHP script, ive tried already:

$command = "cd /home/jimmy12/mediaserver/server; ./mss.sh start; -u username; -p password";
$output = shell_exec($command);
echo "$output";
$command .= "cd /home/jimmy12/mediaserver/server; ./mss.sh start;";
command .= "username;";
command .= "password;";
$output = shell_exec($command);
echo "$output";

I have no clue, even after a google search and reading a few forum posts here and there I cant see anything that says how to enter in prompts from a shell_exec.

Starting Xmedia  server...
Using JAVA_HOME:  /usr/local/jdk
Enter administrator username:Enter administrator password:Password required

I thought this would be simple but turning out to be one thing after another for as little as 3 lines of code, shows how much I don't know, just when I thought I had this whole learning coding thing down.

You need to do auth keys for ssh, if you don't do a passphrase, the key alone will log you in(don't share it!). Then if these are running both in Linux, you can then do ssh alias's that will allow you to set different settings when you connect to it with a shortcut name like "ssh media".

Here is a couple links to get started... http://www.linuxproblem.org/art_9.html https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys--2

If you want to learn google "linux ssh keys".

Robert Walker
Robert Walker
17,146 Points

Its not the actual SSH that is the issue, the media server itself has a username and password.

I can run all other SSH commands through the shell_exec, ls, kill etc but starting this server requires the username and password for the media server itself.

Using SSH keys wouldn't make a difference in this I feel.

Is there no way to pass in the username and password within the script or do I need to find a way to disable the media server username and password prompts?

Ok there is a way buy not with shell_exec. Now it's been about 3 yrs since I found this and wrote it but some googling on the method would help. It uses proc_open.

<?php
function pwdwrite($user, $pass){
    $cmd="htdigest test.pwd realm ".$user;
    $descriptorspec = array(// setting an array to handle the read and write function for proc_open ($process)
       0 => array("pipe", "r"),
       1 => array("pipe", "w")
    );
    $process = proc_open($cmd, $descriptorspec, $pipes);
    $pwd = $pass."\n";\\need to add a line feed to actually send the command
    if (is_resource($process)) {
        fwrite($pipes[0], $pwd); // first prompt
        fwrite($pipes[0], $pwd); // second prompt
        fclose($pipes[0]);
        $output = stream_get_contents($pipes[1]);
        fclose($pipes[1]);
        // It is important that you close any pipes before calling
        // proc_close in order to avoid a deadlock
        $return_value = proc_close($process);
    }
}
?>

This runs htdigest to set a htdigest password for a user. Not exactly what you want but will get you going in the right direction.

Robert Walker
Robert Walker
17,146 Points

I tried loads of different things but couldn't get it to work, I think ill have to adjust the script for start the media server instead to remove the username and password prompt. Thank you for taking the time to help though very grateful.