当前位置:网站首页>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边栏推荐
- Ding Dong grabs vegetables - monitoring and pushing tools for delivery period
- How to add aggregation hotspots in VR panorama? How to add a content module?
- 高通 QC2.0 快速充电智能识别IC FP6719
- Bit operation topic
- 历时2年442位作者132个机构!Google发布语言模型评价新基准BIG-bench,204个任务全面评价语言模型能力,附论文
- How to increase the monthly salary of software testing from 10K to 30K? Only automated testing can do it
- FS2117升压IC输出5V2.4A同步整流
- After the college entrance examination, VR panoramic tour will show you the beautiful scenery of the scenic spot
- Batch detection of specified ports of different URLs (py script)
- 高考开启,VR全景可以这样看考点
猜你喜欢
![[advanced C language] advanced pointer [Part 2]](/img/fe/44e7f7c39bd77239b9a70628768ecc.png)
[advanced C language] advanced pointer [Part 2]

How to add aggregation hotspots in VR panorama? How to add a content module?

深入理解LightGBM

今年高考期间各考点秩序井然,未发生影响安全的敏感案事件

融入机器学习,让Chrome浏览器更“懂”你

2022.05.29 (lc_6078_rearranges characters to form target string)
叮咚抢菜-派送时段监听及推送工具

大厂是怎么写数据分析报告的?

刷脸认证如何实现人脸又快又准完成校验?

DDD landing practice repeat record of theoretical training & Event storm
随机推荐
Routine solution - the problem of horse walking on the chessboard
This article introduces you to j.u.c's futuretask, fork/join framework and BlockingQueue
首批!青藤通过信通院CWPP能力评估检验
Spark ShuffleManager
Source code analysis of Tencent libco collaboration open source library (III) -- Exploring collaboration switching process assembly register saving and efficient collaboration environment
Ding Dong grabs vegetables - monitoring and pushing tools for delivery period
一文详解EventMesh落地华为云的探索及实践
Harbor image pull voucher configuration
Cet article vous donne un aperçu de la tâche future de j.u.c, du cadre Fork / join et de la file d'attente de blocage
大学生毕业季找房,VR全景看房帮你线上筛选
Looking for a room in the graduation season of college students, VR panoramic viewing helps you screen Online
First batch! Sinomenine has passed CWPP capability assessment and inspection of Xintong Institute
Batch detection of specified ports of different URLs (py script)
软件测试月薪10K如何涨到30K,只有自动化测试能做到
Mongodb index unique
FS4100 锂电充电管理IC输入12V给8.4V充电IC
高考结束,VR全景云游为你展现景区的美好风光
企业级存储发展趋势谈:开源存储的冷思考
2022.05.28 (lc_516_longest palindrome subsequence)
一文帶你了解J.U.C的FutureTask、Fork/Join框架和BlockingQueue