tags:

views:

105

answers:

2

I am using Apache with PHP to execute a bash script and am running into a problem. Pseudo code...

Website Button -> Click -> jQuery.post("file.php") -> php: system("/home/user/file.sh cmd") -> bash: screen -S name -> bash: java -jar file.jar

The bash script then starts a named screen session, and runs a Java application (java -jar file.jar) inside of it. I almost have this whole thing working. The only thing that is not working is when the Java application is launched, it is unable to read one, out of many, of its local configuration files.

Apache is running as the same user that owns the home directory and the bash script and the Java application (a local user). If the bash script is ran right from the CLI, as the same user as Apache is running as, everything works 100%. File permissions are not an issue.

My real issue here is finding what to blame. Is the Java app coded wrong, looking in the wrong location for the config. file? Is my PHP wrong? Is Apache wrong? Is one of the scripts missing TERM and thus breaking something? I really don't know where to start.

Just to reiterate, if I cut out PHP and the website and execute the bash script from the command line, it works. If I execute the script from PHP, one of the Java app's config files cannot be read.

There is a lot of code here, between the HTML, Javascript, PHP and bash. If you think it'll help for me to post each script, I will.

Edit: The latest thing that I've tried is to confirm that the working directory in my php script is correct.. Relevant parts below:

<?php
 session_start();
 $action = $_REQUEST['action'];
 $user = "public";
 $home = "/home/minecraft/$user/mc/";
 $script = "minecraft.sh";
 $lck = "/home/minecraft/$user/mc/server.log.lck";
 chdir($home);

 function perform_action($action, $script, $home){
  echo("Performing $script $action\n");
  system("sh -c 'cd $home && ./$script $action'");
 }
?>

So I am first changing into the appropriate directory, and then running the script (which starts the screen and then the java app inside it) from the current working directory. If I were in the wrong directory, the file wouldn't exist and thus wouldn't start.

EDIT: Here are the results of the user and web's environment, before and after: http://pastebin.com/ip0McMUc

+1  A: 

Are you using absolute paths to your config files? If not, it may be using the classpath to find them, and that could easily be different between your manual test and your invocation via php.

I'm leaning towards the issue being with the java part. Either something in it or the environment its invoked in. You can use a simple perl script to dump the environment settings and see what's different:

#!/usr/bin/perl

for my $key (keys %ENV) {
    print "$key: $ENV{$key}\n";
}

If that doesn't reveal any key differences then maybe you should try posting some key bits of the java stuff.

bemace
Good idea. I just tried that, however I am still having the same problem. group-txt-location=/home/minecraft/public/mc/groups.txt
Kyle
Just to make sure, are you able to run file.sh from different folders such as / and it still works?
bemace
Yes, if I cd /; /home/minecraft/public/mc/minecraft.sh start, everything works.
Kyle
Re: Java. I have recently been executing the PHP script directly, without calling it from java, just in case.
Kyle
Where should I be running that perl script from? Executing it in the browser?
Kyle
@Kyle call it once from the command line logged in as apache, and run it once from your php script. Compare the output and see if you can find any important differences between the environments
bemace
@bemace: There were a lot of differences. After using putenv to add the important-looking environment variables, still the same problem. Results at http://pastebin.com/ip0McMUc
Kyle
hmm... the only interesting difference I see is that `/usr/games` is on your user path but not your apache path.
bemace
A: 

probably user environment issues, apache run with a limited environment you might need to recreate default environment values of the user to run the program properly (java in that case )

dvhh
This sounds promising... Could you explain a bit more?
Kyle
I had the same case with perl calling executable that needed more environment variable than the apache use had. use phpinfo to see what is the environment of your php script, then compare to the one used by a user able to run the script/executable, use putenv to add the relevant environment variable needed. @bemace seem to provide one part of the solution.
dvhh
the prime suspect would be LD_LIBRARY_PATH then the CLASSPATH. maybe you could print the result of the "system" call.
dvhh
also use 'sh -c "cd /home/minecraft/public/mc/ ./server_nogui.sh" this would stop if the script is not able to change directory plus explicitly calling sh would be a plus.
dvhh
Looking at `env` from the user in question, I don't see anything special standing out that differs from phpinfo()'s. http://pastehtml.com/view/1bknmmr.html is phpinfo()'s environment. http://nopaste.linux-dev.org/?8208 is `env` I don't see anything relating to LD_LIBRARY_PATH or CLASSPATH from either?
Kyle
Kyle
did the "system" function return any output ?
dvhh
function perform_action($action, $script, $home){ echo("Performing $script $action\n"); $out = system("sh -c 'cd $home echo("out: $out"); }Only output "Launching Minecraft server..." which is what $script is coded to do. Did I do that correctly?
Kyle
I suppose you script does not start as a deamon so, I think php is probably terminating the application om timeout (30 sec max script execution time);
dvhh
because my guess would be that you want to run a minecraft server this way
dvhh
without modifying the java to handle the HUP signal that might not be possible
dvhh
I've taken the java out of the equation for this testing by executing the PHP script directly.
Kyle
Also the problem is not with the server running - it starts up and keeps running.
Kyle
Here are the results of the environment variables of the user, of the php script from apache before using putenv, and then after using putenv: http://pastebin.com/ip0McMUc
Kyle
humm ... can you check the access rights on the files ?
dvhh
http://nopaste.linux-dev.org/?8209 is a ls -al of directory. Would you say that at this point, it could be a problem with the actual java code? The java code (A Minecraft Mod) being what the script starts.
Kyle