How to execute a php file from a bash file?

5,052

You should have problems when you have spaces in the path of the found files.
Try with a while read do ... done loop like:

#!/bin/bash
find /home/ -name "killstat.php"  | while read i
do 
  /usr/bin/php -f "$i";
done

Note the double quotes " in /usr/bin/php -f "$i";


In case your script needs to run from his place

#!/bin/bash
find /home/ -name "killstat.php"  | while read i
do 
  Cdir=$(dirname "$i")
  Cname=$(basename "$i")     # This line can be avoided...
  cd "$Cdir"
  /usr/bin/php -f "$Cname";  # ...if here you use killstat.php [1]
  cd -
done

Note again the double quote for $i just in case you have spaces in the string of directory.
The double are not really needed for $Cname since you know that, in this case, is killstat.php.
[1] You can put directly killstat.php instead of $Cname and avoid to write all the lines with Cname in the script.

Share:
5,052

Related videos on Youtube

Karel
Author by

Karel

Updated on September 18, 2022

Comments

  • Karel
    Karel over 1 year

    My goal is to make a script that I can execute through a cron job.

    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/ -name "killstat.php")
    for i in $NAMETOFIND; do /usr/bin/php -f $i;
    done
    

    killstat.php should be executed to reset my statistics. But it seems the php file is not executed. When I change the -f switch to -l (syntax error checking) the script executes fine!

    Executing php killstat.php from the CLI also works fine and resets the statistics. I'm running as root and also killstat.php and my script are owned by root. The script is chmod to 4755

    Solution provided by Hastur with tiny alteration.

    I place this script in /etc/cron.monthly to reset my statistics.

    #!/bin/bash
    find /home -name "killstat.php"  | while read i
    do
      Cdir=$(dirname "$i")
      Cname=$(basename "$i")     # This line can be avoided..
      cd "$Cdir"
      /usr/bin/php -f "$Cname";  # ...if here you use killstat.php [1]
      cd -
    done
    
    • jet
      jet about 10 years
      the paths in front of killstat.php are missing
    • Karel
      Karel about 10 years
      I think the path is included to $i. I've made a simular script to convert php files from dos to unix which is using the same logic and that works fine.
  • Karel
    Karel about 10 years
    Your script and my script will find the wanted file perfectly, but it just don't want to parse/execute the php file.
  • Karel
    Karel about 10 years
    I suspect I need to set a permission to permit a bash script to parse/execute a php file. Replacing php -f with php -l shows that the files are found and checkt, [code]No syntax errors detected in /home/aaa/var/killstat.php No syntax errors detected in /home/bbb/bbclone/var/killstat.php No syntax errors detected in /home/ccc/var/killstat.php [/code]
  • Hastur
    Hastur about 10 years
    1. If you have a directory named e.g. Directory with spaces in the name without double quote you will have an error. 2. It is possible that killstat.php needs, for some reasons, to be executed from the location where it is: in this case you have to change directory before execute it.
  • Karel
    Karel about 10 years
    At first no success. After a tiny modification, find . -name to find /home -name it works! Will test the script now when executed as a cron job. Thanks for the help.
  • Karel
    Karel about 10 years
    Also as a cronjob the script works fine! Problem resolved! ;)
  • Hastur
    Hastur about 10 years
    Nice to listen :) The key was when I understand you said that the -f switch to -l (syntax error checking) works fine. It means that it finds the files .php and the file was correctly written... on the other side was able to produce correct results so it have to be a problem of path. You're welcome. ps> Did you need to search in the home for all users? It can be long...and dangerous (exploit)...
  • Karel
    Karel about 10 years
    Well the script finishes within a second and find and execute 12 killstat.php files in the home directory. If you have security advise to prevent exploits I'm happy to hear them. But I'm the only user/owner of the VPS.