PHP execution via passthru(), exec(), shell_exec(), …: sh: /php: No such file or directory

Want to help support this blog? Try out Oh Dear, the best all-in-one monitoring tool for your entire website, co-founded by me (the guy that wrote this blogpost). Start with a 10-day trial, no strings attached.

We offer uptime monitoring, SSL checks, broken links checking, performance & cronjob monitoring, branded status pages & so much more. Try us out today!

Profile image of Mattias Geniar

Mattias Geniar, March 29, 2012

Follow me on Twitter as @mattiasgeniar

If you’re running PHP scripts through the command line (crontab or via a bash shell), you can get the following output:

localuser@host ~# php myscript.php
...
sh: /php: No such file or directory
...

That mostly happens when you have code like this active.

passthru("php other.php");
exec("php other.php");
shell_exec("php other.php");
...

First thing to check would be if “php” is a registered binary. Run the following as the user that is running your PHP script (hopefully not the root user).

localuser@host ~# which php
/usr/bin/php

If you get no response, try to add the full path to the PHP binary into your script.

passthru("/usr/local/php other.php");
exec("/usr/local/php other.php");
shell_exec("/usr/local/php other.php");

If you do get a response, doublecheck if safe_mode is On for the script you are running. With safe_mode on, it will block the execution of other scripts via the passthru() or exec() calls.



Want to subscribe to the cron.weekly newsletter?

I write a weekly-ish newsletter on Linux, open source & webdevelopment called cron.weekly.

It features the latest news, guides & tutorials and new open source projects. You can sign up via email below.

No spam. Just some good, practical Linux & open source content.