Answer the question
In order to leave comments, you need to log in
Why is the cron script not running?
Help the inexperienced. Operating system CentOS 6.3. I'm trying to set up a folder and database backup via rsync. Scripts through the console run normally, everything works, cron does not give errors when saving, it just writes "installing new crontab". If I understand everything correctly, then everything seems to be correct. But for some reason nothing works. The rights to the folder where the scripts are 777.
The scripts themselves:
First
#!/bin/bash
cd /mnt/backup/neon_backup/MySQL_backup
mysqldump -u root -pnppwd --all-databases > mysql_backup.sql
tar cvjf back_mysql.tar.bz2 mysql_backup.sql
#!/bin/bash
sudo rsync --archive /home/share --delete /mnt/backup/neon_backup
sudo rsync --archive /var/www/html/vtgr --delete /mnt/backup/neon_backup
#!/bin/bash
sudo rsync --archive /var/www/html/vtgr --delete /mnt/backup/neon_backup/vtgr_weekly
cd /mnt/backup/neon_backup/vtgr_weekly/MySQL_weekly
mysqldump -u root -pnppwd --all-databases > mysql_backup.sql
tar cvjf back_mysql.tar.bz2 mysql_backup.sql
0 1 * * * /bin/bash /usr/share/script.sh
0 2 * * * /bin/bash /usr/share/script2.sh
* * * * 1 /bin/bash /usr/share/script3.sh
30 4 * * * /var/www/vtgr/script/updateControlWorker.php
30 4 * * * /var/www/vtgr/script/createTack.php
Answer the question
In order to leave comments, you need to log in
Everyone found the answer to their question:
1) Crontab needs to be run like this: sudo crontab -e - this is necessary for cron to run scripts from under root.
2) Instructions for cron should be like this. It is necessary to write bash before specifying the path to the script. After specifying the path to the script add >/dev/null 2>&1
Example:
0 1 * * * bash /bin/bash /usr/share/script.sh >/dev/null 2>&1
0 2 * * * bash /bin/bash /usr/share/script2.sh >/dev/null 2>&1
* * * * 1 bash /bin/bash /usr/share/script3.sh >/dev/null 2>&1
#!/bin/bash
rsync --archive /home/share --delete /mnt/backup/neon_backup
rsync --archive /var/www/html/vtgr --delete /mnt/backup/neon_backup
#!/bin/bash
cd /mnt/backup/neon_backup/MySQL_backup
mysqldump -u root -pnppwd --all-databases > mysql_backup.sql
set > /tmp/script-environment
tar cvjf back_mysql.tar.bz2 mysql_backup.sql
In crontab at the end of each command add:
> /tmp/commandname.log 2>&1
and read /tmp/*.log
It's all about sudo, don't use it in scripts unless it's configured to run without a password. If you want to run as root, then move the cron tasks to the root cron.
1. /bin/bash is not needed in crontab. Throw it out and make the scripts executable.
2. applications from cron are launched with a minimal environment, if mysqldump is not in /bin or /usr/bin, script.sh will not find it. Add set > /tmp/script-environment to the script to see what they run with.
3. sudo -- need nopasswd
1. Get rid of sudo. Execute the cron as a user who has non-sudo rights.
2. Look at the cron logs.
An example of the correct design of assignments would really help me, I just tried everything, and there is simply no one to consult. It would help a lot if someone just wrote the first script correctly, and how to cron it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question