0

attempting to run a shell script using PHP. the shell script contains: 'cd /data/nfsshare; python /data/nfsshare/kron_hull.py> /data/nfsshare/log.txt' that is running a python script named kron_hull.py

the php code im using is:

if ($_POST['visual']) {
  shell_exec("sh test.sh");
  echo "working! woot woot";
} else {
  echo "not working";
}

i am using the echo's as a way to tell if its running the if statement correctly, and i am able to successfully echo "working! woot woot", but the shell file does not run, because the script is meant to create a new file on the server, which it does not.

the shell file works when you put it directly in the linux terminal as "sh test.sh" so the issue is not with the shell file.

i also ran shell_exec('whoami'); and shell_exec('ls -lart'); and was able to echo both of those, so i don't believe it is a problem with the shell_exec() command.

my problem is: i dont believe the shell script is even running when put into the shell_exec() command, and I dont know a better way to check besides looking for the new file its supposed to create, which i get none.

any help is appreciated, if you need me to do anything for testing, or need more information, lmk and ill be glad to answer.

i checked the related questions and they did not help my case

John Conde
  • 217,595
  • 99
  • 455
  • 496

1 Answers1

0

One way to check whether the shell command executed correctly is to use the exec() function instead. exec() gives you the ability to see the return value and the output in a variable.

You can build a error checking execution like this:

if ($_POST['visual']) {
    exec("sh test.sh", $output, $return);

    // Return will return non-zero upon an error
    if (!$return) {
        echo "working! woot woot";
    } else {
        echo "Error with shell script";
    }
}

To see error codes you can look here.

shn
  • 865
  • 5
  • 14
  • So now that I'm using this code I get the echo statement: "Error with shell script" , so where do I go from there? EDIT: I added "echo $return;" and got 255 which is "255 The extended attributes are inconsistent. " any ideas? – Max Gingold Aug 08 '19 at 18:22
  • That's great! You're on track to finding the problem. I'm not sure what the 255 means. Have you tried running the shell script on its own in the command prompt? – shn Aug 08 '19 at 18:34