[php] Bash script to run php script

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.

This question is related to php bash shell cron

The answer is


The bash script should be something like this:

#!/bin/bash
/usr/bin/php /path/to/php/file.php

You need the php executable (usually found in /usr/bin) and the path of the php script to be ran. Now you only have to put this bash script on crontab and you're done!


I found php-cgi on my server. And its on environment path so I was able to run from anywhere. I executed succesfuly file.php in my bash script.

#!/bin/bash
php-cgi ../path/file.php

And the script returned this after php script was executed:

X-Powered-By: PHP/7.1.1 Content-type: text/html; charset=UTF-8

done!

By the way, check first if it works by checking the version issuing the command php-cgi -v


a quick way to find out WHERE YOUR particular executable is located on your $PATH, try.

Even quicker way to find out where php is ...

whereis php

I'm running debian and above command showing me

php: /usr/bin/php /usr/share/php /usr/share/man/man1/php.1.gz

Hope that helps.


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

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.


If you don't do anything in your bash script than run the php one, you could simply run the php script from cron with a command like /usr/bin/php /path/to/your/file.php.


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!


You just need to set :

/usr/bin/php path_to_your_php_file

in your crontab.


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to bash

Comparing a variable with a string python not working when redirecting from bash script Zipping a file in bash fails How do I prevent Conda from activating the base environment by default? Get first line of a shell command's output Fixing a systemd service 203/EXEC failure (no such file or directory) /bin/sh: apt-get: not found VSCode Change Default Terminal Run bash command on jenkins pipeline How to check if the docker engine and a docker container are running? How to switch Python versions in Terminal?

Examples related to shell

Comparing a variable with a string python not working when redirecting from bash script Get first line of a shell command's output How to run shell script file using nodejs? Run bash command on jenkins pipeline Way to create multiline comments in Bash? How to do multiline shell script in Ansible How to check if a file exists in a shell script How to check if an environment variable exists and get its value? Curl to return http status code along with the response docker entrypoint running bash script gets "permission denied"

Examples related to cron

How to run a cron job inside a docker container? Run CRON job everyday at specific time How to run a cron job on every Monday, Wednesday and Friday? Spring cron expression for every day 1:01:am How to run a cronjob every X minutes? CronJob not running Scheduling Python Script to run every hour accurately How to set a cron job to run every 3 hours Execute PHP script in cron job How to create a Java cron job