How to execute a php file from a bash file?
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.
Related videos on Youtube
Karel
Updated on September 18, 2022Comments
-
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 4755Solution 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 about 10 yearsthe paths in front of killstat.php are missing
-
Karel about 10 yearsI 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 about 10 yearsYour script and my script will find the wanted file perfectly, but it just don't want to parse/execute the php file.
-
Karel about 10 yearsI 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 about 10 years1. 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 thatkillstat.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 about 10 yearsAt 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 about 10 yearsAlso as a cronjob the script works fine! Problem resolved! ;)
-
Hastur about 10 yearsNice 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 about 10 yearsWell 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.