2

I am trying to run a shell script from a php script.

I have complete control of the environment (unix on mac), I should have all the permissions, etc. set correctly.

The web script is in /htdocs/

The shell script can be executed from anywhere so when I go to /htdocs/ in the shell, I can easily run it like this: $ my_shellscript

.. but when my php script (which is located in htdocs) tries to call it:

shell_exec('my_shellscript');

I get nothing.

I have proven the script can be called from that location and I have temporarily granted full access to try to get it working somehow. I am going crazy, please help.

If you know of some other way of triggering a shell script via the web that would be fine.

Thanks in advance.

hakre
  • 193,403
  • 52
  • 435
  • 836
dataskills
  • 646
  • 7
  • 15
  • 1
    How are you invoking the script from your PHP code? Post the code sample. – Rahul Jul 08 '11 at 20:26
  • `shell_exec()` is okay to call a script, but you must ensure the path is correct as well. Related: http://stackoverflow.com/q/6626995/367456 – hakre Jul 08 '11 at 20:37
  • what does `var_dump(shell_exec('my_shellscript'));` prints out? In case it's `NULL` invoking the command failed. – hakre Jul 08 '11 at 20:39

4 Answers4

1

well i got few weeks same problem, the solution is to check if the apace has the permission to execute your script. You could also try to run the script in php cli.

Risto Novik
  • 8,199
  • 9
  • 50
  • 66
  • Except he said that he granted full access to it and it still didn't work, so there must be something else. – Clint Jul 08 '11 at 20:31
  • well you can check if the shell_exec('ls'); returns any output. Also read my topic http://stackoverflow.com/questions/6468360/shell-exec-returns-empty-string – Risto Novik Jul 08 '11 at 20:33
  • It seems like the script will work everywhere but if I try to run it from a php script. Other shell programs run fine, I tried small stuff like echoing pwd, etc.; it is probably something really basic that I am missing. Thank you all for your suggestions, I will continue searching .. – dataskills Jul 08 '11 at 23:04
0

Since it is a shellscript, it needs to be invoked with the path prefix. My guess is you need to do this:

shell_exec('./my_shellscript');
thunderflower
  • 181
  • 1
  • 5
0

First thing: make sure php isn't running in Safe Mode

Next thing: Try running it with the exec() function and using the full path (e.g. /var/www/htdocs/my_shellscript)

Clint
  • 2,871
  • 2
  • 25
  • 28
  • Thank you for the suggestion, I tried giving the full path (the script is called runtoc)and I got "/Users/homedir/util/runtoc: line 4: 53730 Trace/BPT trap php myscript.php -v -e www" There seems to be a problem with a reference to a php script I am referencing in the shell script, but I have never seen this type of error. Any ideas? – dataskills Jul 08 '11 at 23:15
  • I am not sure. When I google that error it seems to be on osx devices only. I am on osx and I haven't seen it, but I don't often run shell scripts. I would suggest using full paths in your shell scripts as well. Also, are you calling a shell script from PHP, that then calls another PHP file from command line? I am sure you have good reasoning, but at face value it seems silly. – Clint Jul 09 '11 at 00:17
0

Try doing

echo shell_exec('my_shellscript 2>&1');

which will capture the script's stderr output and print it out. If something inside the script is failing, this output would otherwise be lost when not being run interactively.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Thank you, at least I got some output to work with; I got "sh: ./runtoc: No such file or directory" Then I gave the full path to the script and I got "/Users/homedir/util/runtoc: line 4: 53730 Trace/BPT trap php myscript.php -v -e www" There seems to be a problem with a reference to a php script I am referencing in the shell script, but I have never seen this type of error. Any ideas? – dataskills Jul 08 '11 at 23:13
  • no idea. you'd have to dig through the runtoc script (app?) and see what's going on in there – Marc B Jul 09 '11 at 17:43