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

1、初始化crontab

执行命令 crontab -e

no crontab for username - using an empty one
Select an editor.  To change later, run 'select-editor'.
  1. /bin/nano        <---- easiest
  2. /usr/bin/vim.basic
  3. /usr/bin/vim.tiny
  4. /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排查即可。

相关推荐

  1. ubuntu通过crontab创建定时任务执行sh

    2024-04-26 22:06:02       38 阅读
  2. crontab定时任务执行原因排查

    2024-04-26 22:06:02       23 阅读
  3. 定时任务cron与crontab

    2024-04-26 22:06:02       34 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-26 22:06:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-26 22:06:02       101 阅读
  3. 在Django里面运行非项目文件

    2024-04-26 22:06:02       82 阅读
  4. Python语言-面向对象

    2024-04-26 22:06:02       91 阅读

热门阅读

  1. 【QEMU系统分析之启动篇(十八)】

    2024-04-26 22:06:02       34 阅读
  2. 利用blob对象于浏览器保存图片到本地

    2024-04-26 22:06:02       35 阅读
  3. 可使用的 ESRGAN 超分模型

    2024-04-26 22:06:02       33 阅读
  4. c c++编程 fmt:占位符

    2024-04-26 22:06:02       30 阅读
  5. LC 202.快乐数

    2024-04-26 22:06:02       34 阅读
  6. c++ 智能指针 交换函数实验

    2024-04-26 22:06:02       31 阅读
  7. 使用gogs搭建git服务器-注意事项

    2024-04-26 22:06:02       34 阅读
  8. 机器学习day2

    2024-04-26 22:06:02       28 阅读