Cron使缓存文件不可写

I have a cron job that I've set up to run every five minutes. Because this cron job is not being run through apache, I believe if it ends up touching the website's server-side cache files (which it does frequently), that those cache files end up no longer being writable by apache, and so I get all these errors about not being able to write to the cache files every time I run the website in the browser. Is there any way I can get it to not have this problem? Running the website on Centos 6.4, in case that's relevant.

There are at least two solutions to this problem, both involve using chmod and chown.

Solution 1:

Move whatever your cronjob runs into its own shell script, and tell cron to run your shell script instead.

After your original command runs, run chmod and chown to change the permissions as you want.

#!/bin/sh
printf "Do your stuff here
"
DIR_ROOT="./your-dir-here/"
find $DIR_ROOT -type f -print0 | xargs -0 chmod 664 # make all files user+group r+w
find $DIR_ROOT -type d -print0 | xargs -0 chmod 775 # make all directories user+group r+w+x
chown apache:apache -r $DIR_ROOT

Solution 2:

Instead of using a shell script, you could append a semicolon to the command your cronjob runs, and then add chmod and chown.

*/5 * * * * /my/five/minute/script.sh; chmod 664 ./some-file; chown apache:apache ./some-file

Hope that helps.

Note: Both solutions are intentionally not the same, I want you to try to think your way through this.