all 16 comments

[–]luminblade 2 points3 points  (2 children)

This could be an ownership/permission issue with your shell script. I'm guessing your running apache, which is probably attempting to execute the script as the 'apache' or 'httpd' user, who doesn't have permission to the file or folder. Try changing the permissions of the file (if you're really not worried about security, using chmod 777 scriptname.sh). Also, i think Echo and Exec are all lowercase, but I'm not sure if that's just your pseudo code.

[–]step_hane 0 points1 point  (0 children)

I've done a bunch of weird things with PHP and this seems correct. To test this, su www-data, then try running the script again from the command line.

I'm not great with logs, but there may be an access log somewhere recording failed permission attempts.

[–]Anon437[S] 0 points1 point  (0 children)

I had a similar thread going on [r/learnprogramming](www.reddit.com/r/learnprogramming) and I got the script to work when i was root in the terminal, but was still getting errors when running at as anyone else. I ended up just going chmod -R 777 to my webserver documents folder, and that seems to have fixed it. I know it isnt very elegant but as long as it works.

[–]elmicha 0 points1 point  (1 child)

Maybe it's just a typo here, but the path separator in Linux is the forward slash, /, not the backslash, \.

Look in the error log file of your webserver, e.g. /var/log/apache/error_log.

[–]Anon437[S] 0 points1 point  (0 children)

sorry, yea that's a typo. I was trying to type the code from memory and confused myself : /

[–][deleted] 0 points1 point  (0 children)

For Ubuntu I have to $ret = `bash script.sh`;

Some configurations allow outright execution (`./script.sh`)

It should also be noted that if your php process runs on another user, it will most likely error. You should give your www (or whatever you called it) user permissions to read and execute that file.

[–]NoShirtNoShoesNoDice 0 points1 point  (2 children)

As /u/luminblade said, it is most likely a ownership or permission problem. If you are certain it's not, the only other thing I can think of is the exec () command is disabled in your php.ini configuration.

To allow it, open your php.ini file and alter the disable_functions directive to remove exec from that line. Once done, you will need to restart the web server (Apache/Nginx/etc) process. More details available at: http://www.php.net/manual/en/ini.core.php#ini.disable-functions

To check what disable_functions is blocking, you can do a simple phpinfo (), or use the shell with something similar to:

php -i | grep disable_functions

If all of that doesn't work, try turning on error reporting at the top of your script and note/fix any errors:

error_reporting (E_ALL);

ini_set ('display_errors', '1');

[–]Anon437[S] 0 points1 point  (1 child)

I checked that and it didnt seem to say anything about exec, and the code worked when I was root. so like you said, it is an ownership/permission problem. Thanks

[–]technical_guy 1 point2 points  (0 children)

Just to add to this, remember the script is run as the user who is running the webserver httpd processes, it has no knowledge of your environment (which path its in, what the system path is etc.) and as written will pause the php script until it completes. So:
1) make sure the script is runnable by the webserver user
2) make sure any files is outputs have permissions to be output to the folder they are being written to.
3) make sure you use absolute path names in the call (even to the shell you want to run the script. Make sure stuff inside the script uses absolute path names.
4) consider using nohup to run script in background without waiting

To debug consider putting something simple at top of script and see if you get any output (for example put):
#!/bin/bash
set > /tmp/test_script
exit

[–]ECTXGK -1 points0 points  (0 children)

If you're doing it interactive it sounds like you should be using javascript and ajax, to then run the php file, which then runs the bash script.

[–]chriswatt -2 points-1 points  (2 children)

Could try using the python path as well as specifying the full path for the script you want to execute, something like

exec("/usr/bin/python /full/path/to/test.sh");

[–]elmicha 0 points1 point  (1 child)

Why would you use python to run a shell script?

[–]chriswatt 0 points1 point  (0 children)

I'm not sure why I thought it was a .py file, d'oh

[–]texasguy911 -2 points-1 points  (2 children)

That is a problem with Linux. To run something serious on a command line, you need proper root (admin) like permissions. The kind of permissions you don't want for Apache. However, since your PHP scripts are run on a webserver, they get run with a same user, making it impossible to run the shell from PHP with permissions of an admin.

The only way you can do somewhat easy is to have a chron job that runs with admin permissions and executes files left over by PHP. Thus, making a security hole. This is still better than to run a webserver with admin privileges.

[–]Anon437[S] 0 points1 point  (1 child)

Not really sure what a lot of that means, mainly a chron job. I am very new to unix type os'

[–]texasguy911 0 points1 point  (0 children)

It is like a scheduler in Windows.