Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a script on my Centos server that is supposed to delete newly created log files. It contains the following:

cd /var/log/kloxo
rm maillog-* courier-* pureftp.log-* smtp.log-*

.. this script has the following ownership / permissions:

-bash-3.2# ls -l /home/admin/cronscripts/logDeleteCronJob.sh
-rwxr-xr-x 1 root root 79 Jan 16 12:26 /home/admin/cronscripts/logDeleteCronJob.sh

.. the target folder looks like this:

drwxr-xr-x 2 root     root        4096 Jan 20 03:39 kloxo

.. and the folder contents look like this (these permissions are automatically set by the server when the log files are created):

-bash-3.2# ls -l /var/log/kloxo/
total 562064
-rw-r--r-- 1 root root 127423120 Jan 20 08:57 courier
-rw-r--r-- 1 root root 127145668 Jan 19 03:42 courier-1
-rw-r--r-- 1 root root 127375976 Jan 20 03:39 courier-2
-rw------- 1 root root  49875972 Jan 20 08:55 maillog
-rw------- 1 root root  49699233 Jan 19 03:42 maillog-1
-rw------- 1 root root  49857831 Jan 20 03:38 maillog-2
-rw------- 1 root root   7925642 Jan 20 08:58 pureftpd.log
-rw-rw-r-- 1 root root  11889944 Jan 20 08:57 smtp.log
-rw-r--r-- 1 root root  11842503 Jan 19 03:42 smtp.log-1
-rw-r--r-- 1 root root  11884360 Jan 20 03:39 smtp.log-2

The problem is that the script executes on schedule but fails to delete the files and I receive the following email from the cron daemon:

rm: cannot remove `maillog-1': Permission denied 
rm: cannot remove `courier-1': Permission denied 
rm: cannot remove `pureftp.log-*': No such file or directory 
rm: cannot remove `smtp.log-1': Permission denied

Please could someone show me how to do this properly. Thanks

share|improve this question
    
Wich user is the cron running as, root? If so, please paste the output from "crontab -l -u root" –  pauska Jan 20 '11 at 9:13
    
How do I know which user cron is running as? –  Xoundboy Jan 20 '11 at 9:41
    
I found it! The cron user is 'admin' and the crontab is: 0 0 * * * /home/admin/cronscripts/logDeleteCronJob.sh –  Xoundboy Jan 20 '11 at 9:45

3 Answers 3

up vote 3 down vote accepted

You're running the cron script as the user "admin", while the file system permission only permits root to write files.

Either run the cron job as root, or chown+chmod all the files/folders so that admin can write/delete/etc.

share|improve this answer
    
Thanks. The log files are autoatically created by the server with root ownership and 600 permissions and I've no idea how to change that - I imagine I would have to edit the config for each of these services which sounds complex. The cron job is created by the Kloxo server panel which runs under admin user. How do I change it so it runs under root user? –  Xoundboy Jan 20 '11 at 11:30
    
Remove the cron job from user admin, and insert it to root user. (crontab -e -u admin, cut out the lines, crontab -e -u root, insert the lines) –  pauska Jan 20 '11 at 11:52
    
That makes sense. I just did what you suggested and will find out if it works tonight at midnight when the job is scheduled to run... –  Xoundboy Jan 22 '11 at 11:14

If you look at the permissions of the files your scripts is failing to delete you'll see that they're 600 for user root, which means that root and only root can delete those files.

Either fix the permissions or run your script as root instead of using the admin user, that'll do the job

share|improve this answer

The scipt permissions does not matter here as long as it is executeable. What matters is the user the script executed as.

Show the cron job entry. It this a user cronjob or a systemwide one?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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