Saturday, September 3, 2016

linux - CentOS root user crontab for mysqldump



I am using CentOS 6.6.
I am trying to set up a crontab. I made an .sh script which runs perfectly when executed manually.
The command is following:



mysqldump --skip-lock-tables --single-transaction --hex-blob --flush-logs --master-data=2 -u root -p'password' database1 > database2.sql



However, when I tried to set it up in /etc/crontab file, it won't run.
Here are the contents of crontab file.




SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)

# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
* * * * * root /home/user/public_html/default1.sh


Also, I would like this script to execute in directory




/home/user/public_html


My script is called default1.sh, and I put it in /home/user/public_html directory.



My goal is to execute this command (or now default1.sh script) as a root user in /home/user/public_html every minute.


Answer



There several things you need to check:





  1. Make sure your script has an appropriate permissions. You need to have an x (at least for user) to be executable.

  2. Make sure mysqldump is within crontab PATH. Otherwise, specify absolute path in your script.

  3. I recommend to specify absolute path for output file to confirm whether it is generated successfully or not. Otherwise, you may get confused about file location.


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...