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

news/2024/10/22 11:31:34/

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/news/1439692.html

相关文章

(免费分享)基于springboot,vue学生成绩管理系统

(1) 课程信息的管理&#xff0c;包括课程信息的条件查询、录入、修改和删除。 (2) 课程表的管理&#xff0c;包括排课、录入课程表、修改课程表。 (3) 用户信息管理。包括对系统管理员、教师和学生的信息进行维护管理&#xff0c;可以新增、修改、删除和条件分页查询用户信息…

免费开源线上社交交友婚恋系统平台 可打包小程序 支持二开 源码交付!

婚姻是人类社会中最重要的关系之一&#xff0c;它对个人和家庭都有着深远的影响。然而&#xff0c;在现代社会的快节奏生活中&#xff0c;找到真爱变得越来越困难。在这个时候&#xff0c;婚恋产品应运而生&#xff0c;为人们提供了寻找真爱的新途径。 1.拓宽人际交流圈子 现代…

HTTPS和HTTP有什么区别,如何启用HTTPS

HTTP和HTTPS是两个非常重要的概念。它们分别代表了“超文本传输协议”和“安全超文本传输协议”。虽然它们的名字非常相似&#xff0c;但它们之间却存在着一些关键性的区别。本文将详细解析这两种协议的不同之处&#xff0c;并介绍如何申请HTTPS。 HTTP详解 HTTP是一种用于分…

SSH新功能揭秘:远程工作提升指南【AI写作】

首先&#xff0c;这篇文章是基于笔尖AI写作进行文章创作的&#xff0c;喜欢的宝子&#xff0c;也可以去体验下&#xff0c;解放双手&#xff0c;上班直接摸鱼~ 按照惯例&#xff0c;先介绍下这款笔尖AI写作&#xff0c;宝子也可以直接下滑跳过看正文~ 笔尖Ai写作&#xff1a;…

flask_apscheduler 定时任务框架

简介 Flask_apscheduler是一个在Flask框架中使用的APScheduler库的扩展。APScheduler是一个用于调度任务的Python库&#xff0c;可以在指定的时间间隔调度函数、方法或任意可调用对象的执行。 Flask_apscheduler对APScheduler进行了集成&#xff0c;使得在Flask应用中可以简便…

【紫光同创盘古PGX-Nano教程】——(盘古PGX-Nano开发板/PG2L50H_MBG324第一章)LED灯闪烁实验例程

本原创教程由深圳市小眼睛科技有限公司创作&#xff0c;版权归本公司所有&#xff0c;如需转载&#xff0c;需授权并注明出处&#xff08;www.meyesemi.com) 适用于板卡型号&#xff1a; 紫光同创PG2L50H_MBG324开发平台&#xff08;盘古PGX-Nano&#xff09; 一&#xff1a;…

Python 中的花卉矩阵组合

使用场景描述 (rib) 协议编写脚本的基础知识。通过创建在 3D 空间中转换的基本几何图形,解决了 xyz 坐标系的基础知识。初步渲染是使用基本着色完成的,因此可以更容易地看到几何体。RenderMan 图1 图 1 是我作为作业参考的示例图片,并尝试匹配 中的图片。为了完成这项任务…

数据中台工具:企业数据管理的核心动力_光点科技

在数字经济时代&#xff0c;数据中台工具已经成为各大企业提升效率、优化决策的核心动力。通过集成、分析和管理企业内外的各种数据&#xff0c;数据中台工具不仅可以帮助企业洞察市场趋势&#xff0c;还能够预测用户行为&#xff0c;推动企业实现精准营销以及产品优化。本文将…