[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP exec()

Posted on 2012-09-03
11
Medium Priority
?
592 Views
Last Modified: 2012-09-19
Hi There,
I am new to PHP so this might seem like a very basic problem to resolve...

I have created ledson.sh that I want to execute from a php page then once the .sh has run, the user must be directed to another page:

<?php
exec('ledson.sh',$retval);
header('Location: http://url.url/leds.php');
?>

Open in new window


./ledson.sh works fine

If I comment out the redirect line, and print $retval, I get a result but script doesn't run.

Where am I going wrong?
Thanks in advance.
Ian
0
Comment
Question by:IanGP
  • 4
  • 2
  • 2
  • +2
11 Comments
 
LVL 29

Accepted Solution

by:
fosiul01 earned 375 total points
ID: 38361839
you need to make the script as Executable

chmod  755 ledson.sh

also make sure that

the ledson.sh is owned by apache user ( if you want to run this via apache)
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38361852
What is ledson.sh supposed to do?  Does it put some output somewhere?  If so, you can look at the output.

If ledson.sh takes a while to run, your PHP script might need to sleep() for a while or otherwise synchronize itself with the other script.
http://us3.php.net/manual/en/function.sleep.php
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38361863
Afterthought, but hopefully a useful one.  Here is a book that can help you get a foundation in how PHP and MySQL (or MySQLi) work together.  Very  much worth the investment of a  month of your time to study it!  It has great explanations and examples and it comes with a downloadable code library that you can copy and modify for your own use.  Now in its fifth printing, it has been a permanent part of my professional library since Edition One.
http://www.sitepoint.com/books/phpmysql5/

best regards, ~Ray
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 15

Assisted Solution

by:Insoftservice
Insoftservice earned 375 total points
ID: 38362371
check the permission of the file
echo shell_exec('sh /home/scripts/ledson.sh');

or

$contents = file_get_contents('/home/scripts/ledson.sh');
echo shell_exec($contents);

or

<?php system("/bin/sh your.sh"); ?>


check it out for the whole path for your shell script


http://php-html.net/tutorials/how-to-write-a-php-script-to-run-shell-commands-from-browser/
0
 
LVL 34

Expert Comment

by:Slick812
ID: 38364478
greetings   IanGP, , u may have considered this already, but u said u were new to PHP, - Many php security settings have any OS calls like -
exec( )
shell_exec( )
Disabled, and may not even issue a warning, or exception if they are called,
usually if you look at the -
phpinfo();
u can see any and all of the disabled commands and options, or ask your server support people.
0
 

Author Comment

by:IanGP
ID: 38380437
Thanks, I'll try the suggestions and let you all know.
Regards
Ian
0
 

Author Comment

by:IanGP
ID: 38380934
OK....

@fosiul01 - ledson.sh is owned by www-data and I've done chmod 775 ledson.sh and I can run it (and ledsoff.sh).

@Ray. I'm working on a Raspberry Pi, playing with the GPIO's
ledson.sh is a little script to switch on LEDs  & Thanks, purchased the ebook.....

@slick812 - tried adding disable_functions = "" to php.ini and commented out the line altogether

@insoftservice and Ray - I have no parameters required for ledson.sh. really just want to toggle on and off.

Thanks
Ian
0
 

Author Comment

by:IanGP
ID: 38381063
Hi,
Still battling with this....
But for the interim I'll use file_put-contents It at least lets me control the output file.
Cheers.
Ian
0
 
LVL 34

Expert Comment

by:Slick812
ID: 38381087
OK, not sure this will help, but linux can have a "what to call to do the job" setting problem, I ran into this BIG TIME when trying to call anything from system in CRON timed run of PHP scripts, sometimes to get the CRON to run PHP, you have to precede the cron call with PHP, so the PHP engine will process and run the PHP script, I remember having to precede a shell script call (work.sh) with the BASH, so the linux now working in PHP mode, would run that script in the shell script engine, , sorry I can not be more specific to your problem, but I am not a Linux command knowledge guy, and I got help in order to get those CRON php runs to work. You may also need to do a full path to the BASH engine, or not?
Have you tried to run any non shell script, linux executables like whois, from exec or shell_exec call from php, did they work?
0
 
LVL 15

Expert Comment

by:Insoftservice
ID: 38385760
hi did u tried with my comments and later tried to do shell script.
If i am not wrong ur shell script was not working proper right in php ?

please elaborate little more.
ini_set('display_errors','on');
and let us know the issue , does redirection work properly
header('Location: http://url.url/leds.php');exit;
0
 

Author Closing Comment

by:IanGP
ID: 38416419
Thanks All.
Although I haven't fully gotten to the bottom of the problem, your insights got me in the right direction.
Thanks
Ian
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month19 days, 11 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question