0
votes

I have created a shell script which deletes subfolder of var/cache folder. Please check below script.

#!/bin/sh
now=$(date +"%Y-%m-%d %T")
if rm -rf var/cache/* ; then
    echo "$now: Deleted"
else
  echo "$now: problem"
fi

When I run this shell file directly by command sh hello.sh it works fine.

But when I run this file using crontab it creates an entry in log file but doesn't delete subfolder of var/cache/..

Please check my crontab as well.

*/1 * * * * /bin/sh /www/html/wp/hello.sh >> /www/html/var/log/redis.flush.cron.log 2>&1

Please suggest how can I run that file using crontab.

1
@NullDev no i haven't added any user in crontab but if i want to run it from different user like abc then what would be the syntax for thatKaran Adhikari
@KaranAdhikari if you want to set a cron job for root which has access to all the folders then you can use the command sudo crontab -e -u rootRajan Sharma
The folders are not getting deleted due to permissions issue i guess, check the permissions of the file ls -laRajan Sharma
what is the current working directory when running directly, also is there a reason not to put the entire path before var/Nahuel Fouilleul
Always use absolute pathsparttimeturtle

1 Answers

1
votes

Try using an absolute path instead of var/cache. When you run it via cron, it will run a) as a specific user, and b) from the home directory of that user. One or both of these might be causing issues for you.

Instead of this:

if rm -rf var/cache/* ; then

Try something like this:

if rm -rf /full/path/to/var/cache/* ; then