Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash script to run php script

Tags:

bash

shell

php

cron

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.

like image 728
mmmbaileys Avatar asked Mar 31 '11 22:03

mmmbaileys


People also ask

How do I run a PHP file in bash?

Show activity on this post. 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.

How do I run a PHP program?

php” file is placed inside the “htdocs” folder. If you want to run it, open any web browser and enter “localhost/demo. php” and press enter. Your program will run.


2 Answers

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.

like image 97
Rafe Kettler Avatar answered Sep 22 '22 01:09

Rafe Kettler


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 
like image 40
FDisk Avatar answered Sep 24 '22 01:09

FDisk