Bash script to run php script

150,887

Solution 1

If you have PHP installed as a command line tool (try issuing php to the terminal and see if it works), your shebang (#!) line needs to look like this:

#!/usr/bin/php

Put that at the top of your script, make it executable (chmod +x myscript.php), and make a Cron job to execute that script (same way you'd execute a bash script).

You can also use php myscript.php.

Solution 2

Sometimes PHP is placed in non standard location so it's probably better first locate it and then try to execute.

#!/usr/bin/env bash
PHP=`which php`
$PHP /path/to/php/file.php

Solution 3

A previous poster said..

If you have PHP installed as a command line tool… your shebang (#!) line needs to look like this: #!/usr/bin/php

While this could be true… just because you can type in php does NOT necessarily mean that's where php is going to be... /usr/bin/php is A common location… but as with any shebang… it needs to be tailored to YOUR env.

a quick way to find out WHERE YOUR particular executable is located on your $PATH, try.. ➜which -a php ENTER, which for me looks like..

php is /usr/local/php5/bin/php
php is /usr/bin/php
php is /usr/local/bin/php
php is /Library/WebServer/CGI-Executables/php

The first one is the default i'd get if I just typed in php at a command prompt… but I can use any of them in a shebang, or directly… You can also combine the executable name with env, as is often seen, but I don't really know much about / trust that. XOXO.

Solution 4

I'm pretty sure something like this is what you are looking for:

#!/bin/sh

php /pathToScript/script.php

Save that with your desired script name (such as runPHP.sh) and give it execution rights, then you can use it however you want.

Edit: You might as well not use a bash script at all and just add the "php ..." command to the crontab, if I'm not mistaken.

Good luck!

Solution 5

You just need to set :

/usr/bin/php path_to_your_php_file

in your crontab.

Share:
150,887
mmmbaileys
Author by

mmmbaileys

Updated on July 09, 2022

Comments

  • mmmbaileys
    mmmbaileys almost 2 years

    I have a php script that I want to be run using a bash script, so I can use Cron to run the php script every minute or so.

    As far as I'm aware I need to create the bash script to handle the php script which will then allow me to use the Cron tool/timer.

    So far I was told I need to put:

    #!/pathtoscript/testphp.php
    

    at the start of my php script. Im not sure what to do from here...

    Any advice? Thanks.

  • Rican7
    Rican7 over 11 years
    If you're going to use a shebang for command line usage, you REALLY should use "#!/usr/bin/env php", as it allows the script to be more portable, since PHP may not always be installed in "/usr/bin/php". Make sense?
  • Hafenkranich
    Hafenkranich almost 7 years
    In most environments you can't simply call php youscript.php but have to figure out the right path. E.g. using the whole path /usr/bin/php myscript.php or figuring it out dynamically by calling which php and writing the result in a variable you use later on.
  • Kamlesh
    Kamlesh over 4 years
    returning output "unknown whereis"
  • Edward J Beckett
    Edward J Beckett over 4 years
    You'll have to install whereis if its not by default.
  • Kamlesh
    Kamlesh over 4 years
    i have installed ffmpeg on windows system, it works by command line but does not work by php while exec command is enabled. I have also restarted computer and xampp after installing ffmpeg. FYI i have setup environment variables of ffmpeg also. Kindly help, if you can. Thanks a lot :)
  • Edward J Beckett
    Edward J Beckett over 4 years
    You should create a separate poat for that question mate.
  • EML
    EML about 2 years
    @Rican7: ordinarily, yes, but the OP wants to run this as a cron job, which will have a reduced environment. If php isn't at /usr/bin/php, env may not be able to find it anyway. Multiple SO Q/As address this (unix.stackexchange.com/q/29608, for example).
  • EML
    EML about 2 years
    /usr/bin/env php does exactly this (that's most of the point of using env), with the added advantage that you can put your php code directly in the bash script, without having to write an additional 'file.php'.