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