1

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
  • the paths in front of killstat.php are missing – jet May 16 '14 at 22:40
  • 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 May 17 '14 at 9:01
1

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.

  • Your script and my script will find the wanted file perfectly, but it just don't want to parse/execute the php file. – Karel May 28 '14 at 21:43
  • 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] – Karel May 28 '14 at 21:50
  • 1
    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. – Hastur May 29 '14 at 6:58
  • 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 May 29 '14 at 11:20
  • Also as a cronjob the script works fine! Problem resolved! ;) – Karel May 29 '14 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.