cron chmod script does not change owner

1.1k views Asked by At

i need to run cron job that changes owner and group for selected files.

i have a script for this:

#!/bin/bash
filez=`ls -la /tmp | grep -v zend | grep -v textfile | awk '$3 == "www-data" {print $8}'`
for ff in $filez; do
        /bin/chown -R tm:tm /tmp/$ff
done

if i run it manually - it works perfectly. if i add this to root's cron

* * * * *               /home/scripts/do_script

it does not change owner/group. file has permissions "-rwsr-xr-x".

any idea how this might be solved?

2

There are 2 answers

1
that other guy On BEST ANSWER

On my system, field $8 is the hour/year, not the filename. Maybe that's the case for your root user as well. This is why you should never try to parse ls. Even if you fix this issue, half a dozen more will remain to break the system in the future.

Use find instead:

find /tmp ! -name '*zend*' ! -name '*textfile*' -user www-data \
    -exec chown -R tm:tm {} \;
0
thom On

if you are adding to root's cron (/etc/crontab) then be aware that the syntax is different from a normal user's crontab.

# m h dom mon dow user      command
  * *  1   *   *  root      /usr/bin/selfdestruct --immediately

Also give the whole path to your command: Cron has not really a rich environment. Make sure that the commands in your script also have the full path and don't use environment variables.