Reputation: 923
I'm having a little issue with adding shebang #! with my php script on RedHat linux. I have a small piece of test code with shebang added (I've tried different variations as well), but I get the following error message everytime I try to run the script.
Error msg:
-bash: script.php: command not found
Test script:
#!/bin/env php
<?php echo "test"; ?>
Shebang #! variations:
#!/usr/bin/php
#!/usr/bin/env php
Upvotes: 35
Views: 37816
Reputation: 4211
find callable shebang for PHP in Linux, Don't memorize this it, learn how to use it
which php
output
zeus@pop-os:~$ which php
/usr/bin/php
then shebang must be
#!/usr/bin/php
Upvotes: 1
Reputation: 3988
In reply to @NVRM's comment regarding only single use of -d
, this is not true.
Start with a chmod +x script
as
#!/usr/bin/php
<?php
phpinfo();
and run script | grep -E 'memory_limit|error_reporting'
, and you'll see
error_reporting => no value => no value
memory_limit => 128M => 128M
Now add some -d
entries so you have
#!/usr/bin/php -d memory_limit=2G -d error_reporting=-1
<?php
phpinfo();
and re-run script | grep -E 'memory_limit|error_reporting'
, and you'll now see
error_reporting => -1 => -1
memory_limit => 2G => 2G
Thus demonstrating you can set multiple options.
In fact, the entire command line is what you are working with here. So you can load extensions, use a different config, etc., everything you can do at the command line.
Upvotes: 5
Reputation: 13082
Leaving here some little notes:
To use a php binary located inside the same folder.
As example a php7.2 executable copied from /usr/bin
is in the same path along a hello
script.
#!./php7.2
<?php
echo "Hello!";
To run it:
./hello
Which behave just as equal as:
./php7.2 hello
This give portability, but beware of system architectures, the php binary might not match the target platform.
Setting allowed memory from the hashbang:
We can set one INI entry from the hashbang line:
#!/usr/bin/php -d memory_limit=2048M
<?php
phpinfo();
exit;
Then to see if php had understood, using phpinfo():
./myphpProg | grep memory
Correct shell output should contain:
memory_limit => 2048M => 2048M
Doing the above is similar as this command line:
php -d memory_limit=2048M myphpProg.**php**
This is why we can set only one ini value in hashbangs, as php accept only one -d parameter at a time.
Upvotes: 0
Reputation: 943635
It should (for most systems) be #!/usr/bin/env php
, but your error isn't related to that.
-bash: script.php: command not found
It says that script.php is not found.
If the problem was the shebang line then the error would say something like:
bash: script.php: /usr/env: bad interpreter: No such file or directory
Presumably, you are typing script.php
and the file is not in a directory on your $PATH
or is not executable.
chmod +x script.php
../script.php
.Instead of 2, you can move/copy/symlink the file to somewhere listed in $PATH
or modify the $PATH
to include the directory containing the script.
Upvotes: 55
Reputation: 1420
If you script is not located in your /usr/local/bin
and is executable, you have to prefix calling your script with php
like this:
php myscrip.php
For shebangs, here is what I use:
Like this:
#!/usr/bin/php
or this:
#!/usr/bin/env php
Upvotes: 22