0

I want to backup my database everyday and i have written a script

my.cnf

[client]
host=locahost
user=root
password='pw'

script.sh

#!/bin/bash
/bin/mysqldump --defaults-file=/home/wasifkhalil/my.cnf --set-gtid-purged=OFF testing_dump > /home/wasifkhalil/db_backups/$(date +"%Y-%m-%d_%H%M")_crm_backup.sql
exit

now when i run this script

[root@instance-main db_backups]# /home/wasifkhalil/script.sh

it works perfect and i see the sql file in the folder but when i run it in crontab it doesnt work, what am i doing wrong? (for testing i set it to 16th minute of every hour)

16 * * * * root /home/wasifkhalil/script.sh

this is the version of Cent OS im on

centos-release-7-3.1611.el7.centos.x86_64
  • 1
    You can't run a script by invoking root. You need to use the needed shell, in your case /bin/bash. – Alex May 17 '17 at 10:38
  • can u give an example? do u mean run just "16 * * * * /home/wasifkhalil/script.sh" like this? – Wasif Khalil May 17 '17 at 10:39
  • To run it as root user, you should place it in root's crontab – Alex May 17 '17 at 10:39
  • yes im in root's crontab – Wasif Khalil May 17 '17 at 10:40
  • 1
    16 * * * * /bin/bash /home/wasifkhalil/script.sh – Alex May 17 '17 at 10:40
2

If you want to run a bash script as root, you need edit root's crontab like so: 16 * * * * /bin/bash /path/to/script.sh

Explanation:

16 * * * * = At minute 16th of every hour.
/bin/bash = shell interpretor.
/path/to/script.sh = path to the script that you need to schedule.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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