ubuntu通过crontab创建定时任务,并执行sh

embedded/2024/9/22 23:28:54/

1、初始化crontab

执行命令 crontab -e

no crontab for username - using an empty one
Select an editor.  To change later, run 'select-editor'.1. /bin/nano        <---- easiest2. /usr/bin/vim.basic3. /usr/bin/vim.tiny4. /bin/ed

选择第一项 /bin/nano即可,确认后输入定时任务的执行命令

# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command
# 添加一个执行脚本,每天凌晨1点运行一个nginx的重启
0 1 * * * /root/script/reloadNginx.sh

2、创建脚本文件reloadNginx.sh

# 进入目录
cd /root/script/
#编辑文件
vim reloadNginx.sh
#录入内容
main(){docker exec -i "nginx-proxy" /bin/bash -c "nginx -s reload && exit"
}
main
# 保存文件并退出
:wq 
#赋予文件权限,不设置,定时任务执行时会报错权限不足
chmod 777 ./reloadNginx.sh

3、修改crontab为输出日志文件

crontab执行默认会发送邮件,如果不进行相关配置,可能会导致定时任务不执行。我们不需要邮件,修改命令,令其输出日志文件即可

#编辑crontab
crontab -e#修改定时任务结束后的输出
# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command
# 添加一个执行脚本,每天凌晨1点运行一个nginx的重启
0 1 * * * /root/script/reloadNginx.sh >> /root/script/reloadNginx.log 2>&1

这样定时任务就执行完毕了,如果产生了异常,通过日志reloadNginx.log排查即可。


http://www.ppmy.cn/embedded/23773.html

相关文章

k8s-pod 控制器

文章目录 k8s-pod 控制器无状态服务与有状态服务无状态服务pod 控制器ReplicationController(RC)ReplicaSet(RS)Label 和 Selector Deployment创建滚动更新回滚版本扩容/缩容暂停和恢复 StatefulSet创建扩容/缩容更新RollingUpdate->金丝雀发布OnDelete 删除 DaemonSet节点选…

设计模式之责任链模式

一、详细介绍 责任链模式是一种行为型设计模式&#xff0c;它允许将请求的发送者与接收者解耦&#xff0c;使多个对象都有机会处理请求&#xff0c;从而形成一条处理请求的责任链。当一个对象接收到请求时&#xff0c;它要么亲自处理请求&#xff0c;要么将请求转发给链上的下一…

【工具】--- Adobe Illustrator 下载-入门绘图

文章目录 软件下载入门项目可看课程 尝试使用Adobe Illustrator&#xff08;设计师常用软件&#xff09;进行科研绘图。 软件下载 阿里云盘下载 入门项目 绘制一个箭头并保持为SVG&#xff0c; 直线->画线->窗口->描边->选择想要的箭头样式->颜色->改为蓝…

【Flutter 面试题】 如何让 AppBar 的标题居中?

【Flutter 面试题】 如何让 AppBar 的标题居中? 文章目录 写在前面口述回答补充说明写在前面 🙋 关于我 ,小雨青年 👉 CSDN博客专家,GitChat专栏作者,阿里云社区专家博主,51CTO专家博主。2023博客之星TOP153。 👏🏻 正在学 Flutter 的同学,你好! 😊 Flutte…

react useEffect中removeEventListener没生效问题解决

在useEffect中写入window.removeEventListener没有生效&#xff0c;代码如下 useEffect(() > {const handleResize () > {console.log(window.innerWidth, window.innerHeight);};window.addEventListener(resize, handleResize);return () > {window.removeEventLi…

动态规划-子序列问题1

文章目录 1. 最长递增子序列&#xff08;300&#xff09;2. 摆动序列&#xff08;376&#xff09;3. 最长递增子序列的个数&#xff08;673&#xff09;4. 最长数对链&#xff08;646&#xff09; 1. 最长递增子序列&#xff08;300&#xff09; 题目描述&#xff1a; 状态表…

CSS接触

标签&#xff1a; 块标签&#xff0c;用来给网页分块 默认占一行<div></div> 无意义标签 <span></span> 背景颜色 background-color:颜色 字体颜色color: 颜色; 使用css&#xff1a; 一、使用css- 内嵌样式&#xff1a; 1. 在h…

stringRedisTemplate.opsForValue().increment(key)报空指针异常

解决办法&#xff1a;https://www.jianshu.com/p/789b33b5943e BUG复现满足以下条件可触发&#xff1a; 1.在RedisConfig开启Redis事务 redisTemplate.setEnableTransactionSupport(true);2.业务中开启事务 Transactional3.同一个业务下用生产多点id就报这个错误了 Cannot in…