Bash script to run php script

前端 未结 9 631
悲&欢浪女
悲&欢浪女 2020-12-07 20:30

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

相关标签:
9条回答
  • 2020-12-07 21:01

    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

    0 讨论(0)
  • 2020-12-07 21:03

    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!

    0 讨论(0)
  • 2020-12-07 21:04

    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.

    0 讨论(0)
  • 2020-12-07 21:06

    You just need to set :

    /usr/bin/php path_to_your_php_file
    

    in your crontab.

    0 讨论(0)
  • 2020-12-07 21:07

    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!

    0 讨论(0)
  • 2020-12-07 21:07

    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.

    0 讨论(0)
提交回复
热议问题