2

I have some trouble to execute a Python file from a Codeigniter controller's function.

~/application/controllers/Lights.php :

header("Access-Control-Allow-Origin: *");
class Lights extends CI_Controller {
    public function __construct() {
        parent::__construct ();
        $this->load->helper ( array ('url', 'form') );
    }
    public function turnOn() {
        system('sudo python test.py > /dev/null 2>/dev/null &');
    }
}

but when I try to execute a system call, like :

system("sudo gpio mode 15 out");

this works perfectly.

2 Answers 2

3

Provide the full path to python binary and script, i.e.:

system('/full/path/to/python /full/path/to/test.py > /dev/null 2>/dev/null &');

If the python binary is already on the PATH of your apache user (apache/www-user), you may ignore the full path.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, that was the solution :)
The only additional remark I'd make here is that some PHP environments don't mirror the PATH configured in a standard user account. They'll run under something like www-user, which will have a much more minimal path configured.
Based on my experience, python is already on the PATH of the apache user, although, I've updated my answer mentioning that.
-1

yes I has a problem like that and Thank you for Pedro Lobito answer. There need a full path to call python file from CI from the root folder.

such as

system('sudo -u root -S python /var/www/4led/turnon1.py');

*sudo -u root -S is a command for using root permission with no root password required :D

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.