1

I am making backups of all databases of MySQL into separate gnu-zip files using crontab. I also want to delete backup files older then 1 day.

I am using below command to delete the old files, but it only works through terminal. If I set cronjob for same command then it does not work. I don't know what is wrong. Going on my command is below:

find path -type f -mtime +0 -delete

I also can't find any fault in setting up cronjob:

0 0 * * * /path/auto_delete_backup_database.sh >/path/auto_delete_backup_database.log

any help will be appreciated.

UPDATE

as @Kusalananda mentioned i ran ls -l and result is as displayed in below screen shot

enter image description here

So is it because .sh file does not have required permission to be executed? if so, how can I grant that permission?

Paulo Tomé
  • 3,782
  • 1
    Is it not working because the files are not deleted, or is it not working because it does not generate any output? What behaviour are you expecting that you don't get? – Kusalananda Dec 21 '19 at 09:46
  • Append 2>&1 to the cron entry so that errors are captured too – Chris Davies Dec 21 '19 at 09:50
  • Please also include in your question the result of running ls -l /path/auto_delete_backup_database.* – Chris Davies Dec 21 '19 at 09:51
  • What is in /path/auto_delete_backup_database.sh (un-generalised: so no, path show the path. I fear that you have generalised away the error). – ctrl-alt-delor Dec 21 '19 at 11:04
  • @Kusalananda it is working by manually typing that command through terminal but when i setup cronjob for that then it is not working – Divyang patel Dec 27 '19 at 10:21
  • @roaima where should i set 2>&1 in cronjob after .sh or after .log ? and what will ls -l will do – Divyang patel Dec 27 '19 at 10:22
  • @Divyangpatel You should add 2>&1 at the very end. The ls -l will show whether the script file is executable or not. Does the script have a #!-line pointing to some shell? Is it doing anything else than running the find command? – Kusalananda Dec 27 '19 at 10:31
  • no there is nothing else in file other then that one line command and not even #! shell specification. – Divyang patel Dec 27 '19 at 10:36

2 Answers2

1

According to what you are showing in the question and saying in comments, your script is not executable and does not contain a #!-line.

At a bare minimum, you should make the script executable with

chmod +x /path/auto_delete_backup_database.sh

and give it a proper #!-line:

#!/bin/sh

(this needs to be the very first line of the script, with no spaces before it).

When the script is not executable, it will not be able to be used as a script at all. Trying to run such a file would return a "Permission denied" error (your cron daemon may well have tried to send you email messages telling you about this).

Without the #!-line telling the calling shell what shell interpreter to use to run the script, it depends on what shell is calling it what would happen. You should always have the appropriate #!-line at the start of a script. In this case, since no special bash features are used, it's enough to use /bin/sh.

You could obviously also schedule the find command directly with cron:

0 0 * * * find path -type f -mtime +0 -print -delete >/path/auto_delete_backup_database.log 2>&1

I think this is appropriate for single-command cron jobs. Anything more fancy is better scheduled as part of a wrapper script.

I added -print to the find command above, which will make it output the names of the files that it tries to delete (it would be silent otherwise).

Kusalananda
  • 333,661
0

Use the chmod command to set the execute bit eg.

chmod ugo+x auto_delete_backup_database.sh
X Tian
  • 10,463