D
D
Dmitry Afonchenko2013-12-17 19:51:47
linux
Dmitry Afonchenko, 2013-12-17 19:51:47

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

Second
#!/bin/bash
sudo rsync --archive /home/share --delete /mnt/backup/neon_backup
sudo rsync --archive /var/www/html/vtgr --delete /mnt/backup/neon_backup

The third
#!/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

crontab text
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

I have already tried everything and removed /bin/bash in crontab, because as I read, if the script starts with #!/bin/bash , then this is not necessary.

Answer the question

In order to leave comments, you need to log in

7 answer(s)
D
Dmitry Afonchenko, 2013-12-19
@Indermove

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

3) The scripts themselves really should be deprived of sudo, since they are already run from under the root user.
Example:
#!/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

I
Ilya Evseev, 2013-12-17
@IlyaEvseev

In crontab at the end of each command add:
> /tmp/commandname.log 2>&1
and read /tmp/*.log

E
egor_nullptr, 2013-12-17
@egor_nullptr

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.

J
jcmvbkbc, 2013-12-17
@jcmvbkbc

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

N
Nikolai Vasilchuk, 2013-12-17
@Anonym

1. Get rid of sudo. Execute the cron as a user who has non-sudo rights.
2. Look at the cron logs.

D
Dmitry Afonchenko, 2013-12-18
@Indermove

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.

S
Snow Dimon, 2013-12-19
@Snowdimon

0 1 * * * root /usr/share/script.sh
Remove sudo from scripts.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question