简体   繁体   中英

Help with executing bash script from PHP

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

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.

可能是用户环境问题,在有限的环境下运行apache,您可能需要重新创建用户的默认环境值才能正确运行程序(在这种情况下为java)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM