I have a remote .sh script to take backup of mySQL file. Cron is running by .sh file not executing. Content of Crontab are:
#SHELL=/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin
* * * * * /home/ubuntu/dscript/db_backup.sh > /tmp/db.output
File Contents are:
#!/bin/bash
DATE=$(date +%Y%m%d_%H%M)
zipname="db_prod_$DATE.sql"
mysqldump db_prod > $zipname
aws s3 mv $zipname s3://mybackups/$zipname
rm -rf $zipname
zipname="db_staging_$DATE.sql"
mysqldump db_staging > $zipname
aws s3 mv $zipname s3://mybackups/$zipname
rm -rf $zipname
Bash path
bash: /bin/bash /etc/bash.bashrc /usr/share/man/man1/bash.1.gz
I am on Ubuntu.
File is is in +x mode.
#!/usr/bin/bash
DATE=$(date +%Y%m%d_%H%M)
zipname="db_prod_$DATE.sql"
SCRIPT_LOG_FILE="/tmp/backup.log"
echo "[${DATE}] [INFO] Starting personnal backup" > ${SCRIPT_LOG_FILE}
$(which mysqldump) db_prod > $zipname
if [ "$?" -eq "0" ] && [ -f ${zipname} ];then
echo "[$(date +%Y%m%d_%H%M)] [INFO] Mysqldump save file ${zipname}" > ${SCRIPT_LOG_FILE}
# file is created continue job with this file ...
$(which aws) s3 mv $zipname s3://mybackups/$zipname
rm -rf $zipname
else
echo "[$(date +%Y%m%d_%H%M)] [ERROR] Mysqldump can't save file ${zipname}" > ${SCRIPT_LOG_FILE}
fi
exit 0
And crontab -l must be:
* * * * * /home/ubuntu/dscript/db_backup.sh &> /tmp/error.and.result.db.output
check cron error:
try to see error with:
~$ sudo cat /var/log/syslog|grep CRON|grep db_backup.sh
~$ sudo cat /var/log/syslog|grep CRON|grep MY_USERNAME
(Permission denied or other)
where MY_USERNAME is the user execute : crontab -e
check script error:
* * * * * /home/ubuntu/dscript/db_backup.sh > /tmp/result.db.output
* * * * * /home/ubuntu/dscript/db_backup.sh 2> /tmp/error.db.output
or simply
* * * * * /home/ubuntu/dscript/db_backup.sh &> /tmp/error.and.result.db.output
The question is whether the error comes from the execution of cron or is in the script, the above methods will help you find the source of the problem.
Related
I've made a little bash script to backup my nextcloud files including my database from my ubuntu 18.04 server. I want the backup to be executed every day. When the job is done I want to reseive one mail if the job was done (additional if it was sucessful or not). With the current script I reseive almost 20 mails and I can't figure out why. Any ideas?
My cronjob looks like this:
* 17 * * * "/root/backup/"backup.sh >/dev/null 2>&1
My bash script
#!/usr/bin/env bash
LOG="/user/backup/backup.log"
exec > >(tee -i ${LOG})
exec 2>&1
cd /var/www/nextcloud
sudo -u www-data php occ maintenance:mode --on
mysqldump --single-transaction -h localhost -u db_user --password='PASSWORD' nextcloud_db > /BACKUP/DB/NextcloudDB_`date +"%Y%m%d"`.sql
cd /BACKUP/DB
ls -t | tail -n +4 | xargs -r rm --
rsync -avx --delete /var/www/nextcloud/ /BACKUP/nextcloud_install/
rsync -avx --delete --exclude 'backup' /var/nextcloud_data/ /BACKUP/nextcloud_data/
cd /var/www/nextcloud
sudo -u www-data php occ maintenance:mode --off
echo "###### Finished backup on $(date) ######"
mail -s "BACKUP" name#domain.com < ${LOG}
Are you sure about the CRON string? For me this means "At every minute past hour 17".
Should be more like 0 17 * * *, right?
Could anybody please let me know why my code is not executing via cron job. But the same perfectly executing when i run manually.
cronjob
30 19 * * * /backup1/RMAN/dumpremoval.sh > /backup1/RMAN/dumpremoval.log2 2>&1
CODE
cat /backup1/RMAN/dumpremoval.sh
yest=$(date --date='1 day ago' +"%y%y%m%d")
yestdump=SMARTDB_D_$yest
ls -lrt $yestdump* > /backup1/RMAN/dumpexists 2> /dev/null
if [ $? -eq 0 ]
then
rm -rf $yestdump*
fi
Here my intention is, i want to delete yesterday's backup files daily those are having file names
like SMARTDB_D_20200409*
Thank you..
I tried the below command and crontab stopped running any jobs:
echo "#reboot /bin/echo 'test' > /home/user/test.sh"| crontab -
What is the correct way to script adding a job to crontab in linux?
I suggest you read Cron and Crontab usage and examples .
And you can run this:
➜ ( printf -- '0 4 8-14 * * test $(date +\%u) -eq 7 && echo "2nd Sunday"' ) | crontab
➜ crontab -l
0 4 8-14 * * test $(date +\0) -eq 7 && echo "2nd Sunday"
Or
#!/bin/bash
cronjob="* * * * * /path/to/command"
(crontab -u userhere -l; echo "$cronjob" ) | crontab -u userhere -
Hope this helps.
Late answer, but on CentOS I create a new cronjob (for root, change user as needed) from a bash script using:
echo "#reboot command..." >> /var/spool/cron/root
>> will force appending to existing cronjobs or create a new cronjob file and append to it if it doesn't exist.
Im not sure about this
but try this one
echo "* * * * * whatever" > /etc/crontabs/root
then check the "crontab -e" you will see your command there
For those who are using alpaine distribution , do not forget to call "crond" to make your crons start
Below command is working fine:
mysqldump -u root -proot db_name > /home/ronak/$( date +"%Y_%m_%d_%H_%M_%S" ).sql
But, when I put exact same command in crontab I am not getting any output. I tried with changing the file name with test.sql, in that case I am getting that file on desired location. I think something is wrong in date in crontab.
* * * * * root mysqldump -u root -proot db_name > /home/ronak/$( date +"\%Y_\%m_\%d_\%H_\%M_\%S" ).sql
Some distros don't like the $() construct in cron. To test in cron try touching a file in, say, /tmp:
* * * touch "/tmp/$(date +%Y-%m-%d).txt"
* * * touch "/tmp/$(date +\%Y-\%m-\%d).csv"
See if either file appears in /tmp
(a) one appeared in file system after touch:
Perhaps an outer wrapper double quote needed around the filename after > symbol in your problem cron command.
(b) neither appeared:
Create a bash script and fire that off inside cron task as it has more power with dates:
#!/bin/bash
NOW=`/bin/date +"%Y%m%d-%H%M%S"`
if [[ "$?" != "0" ]]; then
NOW="date_not_great"
fi
mysqldump -u root -proot db_name > /home/ronak/$NOW.sql
if [[ "$?" != "0" ]]; then
echo "$0: backup failed with error code $?"
fi
I have cron
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
* * * * * root /usr/bin/flock -xn /var/lock/script.lock -c '/bin/bash /root/Dropbox/1.sh'
my 1.sh
#!/bin/bash -l
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
x=1
while [ $x -le 3 ]
do
URL=$(head -n 1 /root/Dropbox/query.txt)
lynx -dump $URL | egrep '^http' > /root/Dropbox/urls.txt
x=$(( $x + 1 ))
done
Bash as default
# echo $SHELL
/bin/bash
Why cron doesn't run 1.sh ?
remove "root" in line crontab line:
* * * * * /usr/bin/flock -xn /var/lock/script.lock -c '/bin/bash /root/Dropbox/1.sh'
If you need root rights put in crontab of root user.
With "root" you will also find error in syslog or messages logs.
And of course be sure the cron daemon is running: ps -ef | grep cron
ADD:
I've tested it with simple touch a file (on ubuntu):
contab line:
* * * * * /usr/bin/flock -xn /var/lock/script.lock -c '/bin/bash ~/1.sh'
1.sh:
#!/bin/bash
touch /tmp/hallo
ADD: (looking at lynx command)
A version of 1.sh Script it works for me.
#!/bin/bash
x=1
while [ $x -le 3 ]
do
URL="http://www.subir.com/lynx.html"
lynx -dump $URL | egrep '.*\. http' > urls.txt
x=$(( $x + 1 ))
done
I changed the regEx on egrep. Your output of lynx may be different (other version of lynx). And I used a fixed test URL (lynx man page). The urls.txt will be filled. Script is triggered by cron. The while will have no effect noting in loop logic will be change on next run.
stephan#coppi:~$ more urls.txt
1. http://lynx.isc.org/current/index.html
2. http://lynx.isc.org/current/lynx2-8-8/lynx_help/lynx_help_main.html
3. http://lynx.isc.org/current/lynx2-8-8/lynx_help/Lynx_users_guide.html
4. http://lynx.isc.org/lynx2.8.7/index.html
5. http://lynx.isc.org/lynx2.8.7/lynx2-8-7/lynx_help/lynx_help_main.html
6. http://lynx.isc.org/lynx2.8.7/lynx2-8-7/lynx_help/Lynx_users_guide.html
7. http://lynx.isc.org/mirrors.html
8. http://lists.nongnu.org/mailman/listinfo/lynx-dev/
9. http://lynx.isc.org/signatures.html
10. http://validator.w3.org/check?uri=http%3A%2F%2Flynx.isc.org%2Findex.html