参考地址:
https://blog.csdn.net/cy309173854/article/details/53186516
问题:shell脚本在手动执行的时候可以正常打印输出,在定时任务的时候不会打印执行命令后的输出内容
原因:是某些命令无法使用crontab调用,因为用户登陆Linux操作系统的时候,/etc/profile
, ~/.bash_profile
等配置文件会被自动执行,所以手动执行脚本能够成功,但是crontab执行失败
解决办法:
1.脚本内的命令全部用绝对路径
1 2 3 4 5
| #!/bin/bash date=$(date +%d-%m-%Y) /usr/local/mysql/bin/mysqldump -u root -p'xxx' xxx > /data/sdv1/mysqlbak/$date-xxx.sql echo `date +%Y-%m-%d` >> /data/sdv1/mysqlbak/$date.log echo 'Backup Successfully Completed' >> /data/sdv1/mysqlbak/$date.log
|
2.脚本文件内容添加环境变量
1 2 3 4 5 6
| #!/bin/bash . /etc/profile . ~/.bash_profile /usr/local/mysql/bin/mysqldump -u root -p'xxx' xxx > /data/sdv1/mysqlbak/$date-xxx.sql echo `date +%Y-%m-%d` >> /data/sdv1/mysqlbak/$date.log echo 'Backup Successfully Completed' >> /data/sdv1/mysqlbak/$date.log
|