美文网首页
Ubuntu 计划任务(定时任务)

Ubuntu 计划任务(定时任务)

作者: O无为学长O | 来源:发表于2021-02-24 05:04 被阅读0次

本文转载自: 原文地址 (如有侵权,请私信删除)

Windows 自带定时执行任务的工具叫做“计划任务”,Linux 下我们使用 Cron 实现这一功能。

启动 cron 服务

通常 ubuntu 下自带 cron,如果没有也可以通过以下命令进行安装:

$ apt-get install cron

若已经安装,输入以下命令判断 cron 服务是否启动:

$ pgrep cron

如果有 pid (一串数字)输出则说明 cron 服务已经启动,没有任何输出说明需要手动启动 cron 服务。

启动 cron 服务:

$ service cron start

管理任务计划文件

cron 的所有任务计划都记录在 crontab 任务计划文件中,通过 crontab 命令对该任务文件进行管理。

usage: crontab [-u user] file
       crontab [ -u user] [ -i ] { -e | -l | -r }
               (default operation is replace, per 1003.2)
       -e      (edit user's crontab)
       -l      (list user's crontab)
       -r      (delete user's crontab)
       -i      (prompt before deleting user's crontab)

参数说明:
-u user 指定用户
-e 编辑某个用户的计划任务文件,若不指定用户,默认编辑当前用户的计划任务文件
-l 显示某个用户的计划任务文件,若不指定用户,默认显示当前用户的计划任务文件
-r 删除某个用户的计划任务文件,若不指定用户,默认删除当前用户的计划任务文件
-i 在删除之前推送确认提示

使用示例:

$ crontab -u foo -e     #编辑用户 foo 的计划任务文件

$ crontab -e            #编辑当前用户的计划任务文件

$ crontab -u foo -l     #显示用户 foo 的计划任务文件

$ crontab -l            #显示当前用户的计划任务文件

$ crontab -r            #删除当前用户的计划任务文件

编辑任务计划文件

初次使用 crontab -e,可能需要选择编辑器,输入编辑器序号点击回车后进入计划任务文件编辑模式。若直接进入编辑模式忽略以上内容。

进入编辑模式后,按照指定格式添加任务计划。

任务计划的语法格式如下:

m h dom mon dow   command
0-59 0-23 1-31 1-12 0-7  command
  • m: 表示分钟
  • h: 表示小时
  • dom: 表示日期
  • mon: 表示月份
  • dow: 表示星期
  • command: 预执行的命令

另外需要使用一些特殊符号实现灵活的配置:

  • * 代表所有值
  • / 代表“每”
  • - 代表范围
  • , 分割数字

任务示例如下:

## 指定具体执行时间
2   *  *  *  * ls    #每个小时的第2分钟执行一次 ls 命令
30  7  *  *  * ls    #每天7:30执行一次 ls 命令
30 20  *  *  2 ls    #每周二,20:30执行一次 ls 命令(0和7表示星期天)

## 指定间隔时间
*/2 *  *  *  * ls    #每隔2分钟执行一次 ls 命令

## 指定时间段
30  7 3-6 *  * ls    #每个月的3,4,5,6号的7:30分各执行一次 ls 命令

## 指定多个时间
30  7 3,6 *  * ls    #每月的3号和6号的7:30分各执行一次 ls 命令

另外,使用 run-parts 可以运行指定目录下所有的脚本(注意脚本必须加上 “#!/bin/bash",否则 run-parts 会调用失败).

30 7 * * * run-parts /home   #每天7:30运行 /home 目录下的所有脚本

按照指定格式添加任务,保存后,任务生效。

下面是一个实际的计划任务文件,包含系统自带注释和一个每两分钟执行一次输出字符串 ”Hello World“ 到 /home 目录下 cron_test 文件的计划任务。

# 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
*/2 * * * * echo "Hello World" >> /home/cron_test

本文转载自: 原文地址 (如有侵权,请私信删除)

相关文章

  • 计划任务

    Linux计划任务 1)说明概 念:约定时间执行指定任务举例1:通过计划任务定时发布文章举例2:通过计划任...

  • Linux如何使用crontab命令

    计划任务 定时执行 crontab -l 查看当前计划任务tail -n100 /var/log/cron 查看计...

  • ubuntu下的定时器编写

    ubuntu下的定时器编写 一、cron简介 在Linux系统中,计划任务一般是由cron承担,我们可以把cron...

  • Linux计划任务

    1、说明 概 念:约定时间执行指定任务举例1:通过计划任务定时发布文章举例2:通过计划任务凌晨3点将测试服务器项...

  • Linux计划任务(1)

    01 何为计划任务 计划任务,顾名思义:计划在未来某个时间点要执行的任务,也可称为定时任务。有一次性的计划任务,也...

  • JAVA架构师之路-教你如何去实现一个分布式定时任务

    什么是分布式定时任务: 首先,我们要了解计划任务这个概念,计划任务是指由计划的定时运行或者周期性运行的程序。我们最...

  • LINUX----计划任务(定时任务)

    有的时候我们需要指定时间执行某项操作,这个时候就需要定时任务,linux有着简单的定时任务。 计划任务种类: ...

  • 计划任务(定时任务)

    计划任务分为一次性计划任务与长期性计划任务。 一次性计划任务 长期性计划任务 一次性计划任务 长期性计划任务 cr...

  • 【UiPath 学习笔记 04】怎么定义定期任务?

    问题 UiPath 中怎么定时运行某些任务?比如定时发送邮件? 答案 系统计划任务,后端通过 orchestrat...

  • Linux定时任务

    1.计划任务时间管理 参数含义-e编辑定时任务-l查看定时任务-r删除定时任务-u指定其他用户 00 02 * *...

网友评论

      本文标题:Ubuntu 计划任务(定时任务)

      本文链接:https://www.haomeiwen.com/subject/kfqyfltx.html