Bash script to run php script

Bash script to run php script

php execute shell script with parameters
bash call php script with parameters
run php function from command line
php execute shell command
how to run php file in apache server in linux
convert php to bash script
run php script from command line
bash script create php file

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.


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.

bash - Call and run php script from shell script, The problem here is that you are quoting the entiore command you are trying to run as a single variable. As a result, you're not running php with  I know it's old, but I just ran into this today. Whenever a Bash script does not execute a PHP script (despite having correct permissions and the "php" command working on its own), I found using the full path to PHP resolves it. i.e.


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

How to Use and Execute PHP Codes in Linux Command Line, You can run a PHP script simply as, if it is a shell script. First Create a PHP sample script in your current working directory. # echo -e '#!/usr/bin/  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).


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.

Running PHP scripts as shell scripts, As well as running PHP scripts via a web server such as Apache, you can also run PHP scripts from the command line (it's also possible to write GUI apps with  You learned how to write a simple shell script and run a script in Linux operating system with help of chmod and other commands. Please see the following tutorials for more information on bash shell scripting under Linux or Unix-like operating systems: Hello, World! Tutorial from the Linux shell scripting wiki. Hello World Bash Shell Script


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!

run PHP file from shell script, You should setup a cron task by yourself instead. Just type crontab -e to edit the crontab and add the following to run your script every days at  How do I run .sh file shell script in Linux? The procedure to run the .sh file shell script on Linux is as follows: Set execute permission on your script: chmod +x script-name-here.sh; To run your script, enter:./script-name-here.sh Another option is as follows to execute shell script: sh script-name-here.sh OR bash script-name-here.sh


You just need to set :

/usr/bin/php path_to_your_php_file

in your crontab.

shell_exec - Manual, shell_exec — Execute command via shell and return the complete output as a string Shell scripts that start with #!/usr/bin/bash return their output properly. Bash is a Unix shell, which is a command line interface (CLI) for interacting with an operating system (OS). Any command that you can run from the command line can be used in a bash script. Scripts are used to run a series of commands. Bash is available by default on Linux and macOS operating systems.


How to run a PHP script from a Unix command line, PHP FAQ: How can I run a PHP script from the Linux/Unix command line? If you use double-quotes the Unix shell will attempt to interpret  Online Bash Compiler, Online Bash Editor, Online Bash IDE, Bash Coding Online, Practice Bash Online, Execute Bash Online, Compile Bash Online, Run Bash Online, Online Bash Interpreter, Execute Bash Shell Online (GNU Bash v4.4)


How to execute a php file from a bash file?, The script needs to find several killstat.php files in multiple directories and execute them. This is what I have so far: #!/bin/bash NAMETOFIND=$(find /home/​  Notice we used #!/usr/bin/php in the first line of this PHP script as we use to do in shell script (/bin/bash). The first line #!/usr/bin/php tells the Linux Command-Line to parse this script file to PHP Interpreter. Second make it executable as: # chmod 755 phpscript.php and run it as, # ./phpscript.php 5. You will be surprised to know you can create simple functions all by yourself using the interactive shell.


How to run a PHP script as root, This post shows how you can execute PHP scripts as a root user on Create a shell script that executes the commands on behalf of the  As a side note, using the appropriate shebang in your PHP script you don't even need to specify the program to use to run your script. Add the following at the beginning of your PHP file : #!/usr/bin/php Then your crontab will just look like this : 00 11 * * * /path/to/myphppage.php