当前位置:网站首页>MySQL backup and manual execution of shell scripts are OK, and crontab scheduled execution fails
MySQL backup and manual execution of shell scripts are OK, and crontab scheduled execution fails
2022-06-10 20:02:00 【Small Gunner】
Backup scripts
vim mysql_backup.sh
#!/bin/bash
source /etc/profile
source ~/.bash_profile
# Saved Directory
data_dir="****"
# Save the name of the file , Be careful date Yes, it is # Backquotes # Wrapped up
data_name="bak_`date +%Y%m%d%H`.sql"
if [ ! -d $data_dir ]; then
mkdir -p $data_dir
fi
# mysqldump Backup command
# -uroot user name
# -p123456 password
# test Database name
mysqldump -uroot -p123456 test > $data_dir/$data_name
Backup file
vim remove_backup.sh
#/bin/bash
source /etc/profile
source ~/.bash_profile
# Saved Directory
data_dir="****"
# Delete 7 Days ago backup files
find $data_dir -mtime +7 -type f -name "bak_*.sql" ;atime、ctime、mtime
atime: Access time (access time), It refers to the time when the file or directory was last accessed ;
ctime: Change the time (change time), It refers to the last change of file or directory ( What's changed is the original data, that is : attribute ) Time for ;
mtime: Modification time (modify time), It refers to the time when the file or directory was last modified ;
Use stat Command to view three time values : Such as stat filename
find Find based on time
mtime The parameters are as follows :
-mtime n according to ⽂ Change the time to find ⽂ Pieces of ,n Integers .
n surface ⽰⽂ The change time distance is n God
-n surface ⽰⽂ The change time distance of the piece is n Within days
+n surface ⽰⽂ The change time distance of the piece is n Before the day
for example :
-mtime 0 surface ⽰⽂ The modification time distance of this document is currently 0 Days of ⽂ Pieces of , That is less than the current time 1 God (24⼩ when ) Inside ⽂ Pieces of .
-mtime 1 surface ⽰⽂ The modification time distance of this document is currently 1 Days of ⽂ Pieces of , That is, from the current time 1 God (24⼩ when -48⼩ when ) Of ⽂ Pieces of .
-mtime+1 surface ⽰⽂ The modification time of this document is ⼤ On 1 Days of ⽂ Pieces of , That is, from the current time 2 God (48⼩ when ) In addition to the ⽂ Pieces of
-mtime -1 surface ⽰⽂ The modification time of this document is ⼩ On 1 Days of ⽂ Pieces of , That is, from the current time 1 God (24⼩ when ) Within ⽂ Pieces of
Grant authority
chmod 755 ***.shTiming task
crontab -e
0 */2 * * * root mysql_backup.sh # Every time 2 Hours
0 1 * * * root remove_backup.sh # Every morning 1 spot , Automatically delete 7 Days ahead backup
shell The script can be executed manually ,crontab Scheduled execution failed
Problem description :
Shell The script can be executed manually and run normally , And get the right results ; Use Crontab When scheduling ,Shell The amount of result data from the execution of the script is 0.
reason :
Linux Next use crontab Executing scheduled tasks will not default from the user profile Read the environment variable parameters from the file , So it often leads to success when executing a script manually , But to crontab When you try to make it execute regularly, you will make an error . This is because the user logs in Linux When operating the system ,”/etc/profile”, “~/.bash_profile” And other configuration files will be executed automatically , and crontab The configuration file may not be executed during scheduled scheduling .
Solution :
Shell The script defaults to #!/bin/sh start Line break Add... On the first line after
source /etc/profile
source ~/.bash_profile边栏推荐
- MySql的MyISAM引擎切换InnoDB时报错Row size too large (> 8126)解决
- TiDB - 快速入门,集群搭建
- Open the door of the hybrid cloud market, Lenovo xcloud's way to break the situation
- Logback排除指定包/类/方法日志输出
- Harbor image pull voucher configuration
- During the college entrance examination this year, all examination sites were in good order, and no sensitive cases affecting safety occurred
- Deep understanding of lightgbm
- Mongodb 唯一索引
- [advanced C language] advanced pointer [Part 1]
- Does the giraffe's neck grow longer not because it eats leaves from high places? Scientists have found the answer in fossils 17million years ago
猜你喜欢

FPGA state machine

Easily learn pytoch full convolution neural network to realize expression recognition

C (pointer 02)

Micronet practice: image classification using micronet

Spark ShuffleManager

2022.05.29 (lc_6078_rearranges characters to form target string)

专项测试之「 性能测试」总结

How to increase the monthly salary of software testing from 10K to 30K? Only automated testing can do it

8.4v双节锂电池专业充电ic(FS4062A)

深入理解LightGBM
随机推荐
An error row size too large (& gt; 8126) occurs when MySQL's MyISAM engine switches to InnoDB
frp reverse proxy
Dock/rancher2 deploy redis:5.0.9
详细解读TPH-YOLOv5 | 让目标检测任务中的小目标无处遁形
补水仪108K加湿器开发方案_单片机_NY8A051F_单片机开发设计开发
刷脸认证如何实现人脸又快又准完成校验?
HM3416H降压IC芯片PWM/PFM 控制 DC-DC 降压转换器
FS4060A是4.2V/3A充电IC
flink啥时候可以支持在sql-client模式下,往yarn上提交任务可以指定applicati
Summary of "performance test" of special test
融入机器学习,让Chrome浏览器更“懂”你
LLDP协议编写要点
Key and encryption mechanism in financial industry
2022最强版应届生软件测试面试攻略,助你直通大厂
首批!青藤通过信通院CWPP能力评估检验
2022.05.28 (lc_5_longest palindrome substring)
[C language] still don't understand the structure? Take a look at this article to give you a preliminary understanding of structure
Micronet practice: image classification using micronet
One question to explain the past and present life of dynamic planning
Yuntu says that every successful business system cannot be separated from apig