0

I have 3 users on my server, 2 of them are for FTP uploads.

I want CRON to fire a job every once in a while that deletes files in a folder called 'subdomain' in their respective home dir if they are X days old or older.

This command, when run in a terminal as the user who owns the dir, works;
find /home/derakupload/subdomain -mindepth 1 -mmin +1 -delete

The issues arise with CRON, which refuse to run this command.
I enter CRON using sudo crontab -e and have 2 entries right now;

* * * * * derakupload find '/home/derakupload/subdomain' -mindepth 1 -mmin +1 -delete
* * * * * derakupload /opt/script/delete_files_older_than

The script in the lower job looks like this

#!/bin/bash
find $HOME/subdomain -mindepth 1 -type f -mmin +1 -delete



I have tried to run it at specific times as opposed to just wilfcarding it all, didn't work.
I have no idea what I'm doing wrong anymore.

  • What's it say in the log? grep CRON /var/log/syslog. Also using the $HOME variable when running as sudo will most likely look in the user ROOT home, not derakupload. When you're running commands in a script like that you should specify the whole path explicitly. – Gansheim Apr 06 '17 at 16:54
  • @steeldriver Can you write that as an answer? You are correct. Removing the user from the crontab made it fire. – Gudrik Hansen Apr 06 '17 at 17:00

1 Answers1

0

This is a perennial source of confusion resulting from the difference between system crontabs (/etc/crontab and the associated files in /etc/cron.d) and user crontabs (including that for the user root, usually accessed via sudo crontab -e) - from the DEBIAN SPECIFIC section of man cron:

Support  for  /etc/cron.d  is included in the cron daemon itself, which
handles this location as the system-wide crontab spool.  This directory
can  contain  any  file  defining  tasks  following  the format used in
/etc/crontab, i.e. unlike the user cron spool, these files must provide
the username to run the task as in the task definition.

If you really need to run the find command as user derakupload, then you can either do so directly via their own crontab e.g.

sudo -u derakupload crontab -e

or add it (with the user field) to /etc/crontab.

Alternatively, if the find command can be run as root, simply use root's crontab sudo crontab -e but omit the user field derakupload.

steeldriver
  • 136,215
  • 21
  • 243
  • 336