简体   繁体   中英

Crontab does not run bash script

I have created a bash script.

#!/bin/bash
now=$(date +%Y-%m-%d--%H:%M:%S)
mysqldump -uMYUSER -pMYPASSWORD MYDB --no-tablespaces | gzip > ./files/$now.sql.gz

I am trying to run it with a crontab once a day. I typed crontab -e and pasted this:

# m h  dom mon dow   command

0 0 * * * /bin/bash ~/backup/script.sh

I can run the script as-is, but the script never runs by itself.

  1. try to run crontab with root user (sudo crontab -e),
  2. locate script in root user folder ~/backup/script.sh

Suggesting to fix your script:

Inject 2nd line with source to current .bash_profile

This will included environment context and path to your cron script.

 sed -i "source $HOME/.bash_profile" <your_script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM