如何使用Linux crontab实现定时任务

操作系统
问题描述

在 Linux 系统中,经常会让系统在某个特定时间执行某些任务,例如定时采集服务器的状态信息、负载状况;定时执行某些任务/脚本来对远端进行数据采集等;如何来实现。

问题分析

可以使用 crontab 来实现。

Linux 下的任务调度分为两类:系统任务调度和用户任务调度。系统任务是由 cron (crond) 系统服务来控制的,这个系统服务是默认启动的。用户自己设置的计划任务则使用 crontab 命令。在 velinux 系统中,查看配置文件如下:

root@i-2udbbtytcx3gv6bvmhzb:~# cat /etc/crontab
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

第一行 SHELL 变量指定了系统要使用哪个 shell,这里是 sh;第二行 PATH 变量指定了系统执行命令的路径;

用户定期的任务,比如系统信息收集。用户可以使用 crontab 。用户定义的 crontab 文件都被保存在 /var/spool/cron/crontabs 目录中。文件名为用户名。velinux 如下:

root@i-2udbbtytcx3gv6bvmhzb:/var/spool/cron/crontabs# pwd
/var/spool/cron/crontabs
root@i-2udbbtytcx3gv6bvmhzb:/var/spool/cron/crontabs# cat root 
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.LlqJMX/crontab installed on Thu Mar 24 15:09:50 2022)
# (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $)
# 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
* * * * * /usr/local/go/bin/go run /root/go/src/auto/main.go >> /root/go/src/auto/load.log 2>&1 &
解决方案

1. 编写 go 脚本(前提安装 Go 环境,如没有 Go 环境使用 shell 脚本即可)

root@i-2udbbtytcx3gv6bvmhzb:~/go/src/auto# cat main.go 
package main

import "fmt"

func main(){
        fmt.Println("Hello");
}

2. 编辑 crontab

使用 crontab -e 进行编辑,编辑完某个用户的 cron 设置后,cron 自动在 /var/spool/cron/crontabs 下生成一个与此用户同名的文件,此用户的 cron 信息都记录在这个文件中。

本示例使用每分钟执行一次,如下:

root@i-2udbbtytcx3gv6bvmhzb:/var/spool/cron/crontabs# crontab -e
* * * * * /usr/local/go/bin/go run /root/go/src/auto/main.go >> /root/go/src/auto/load.log 2>&1 &

配置参数:

a、每一分钟执行一次 command(因 cron 默认每 1 分钟扫描一次,因此全为*即可)

*    *    *    *    *  command

b、每小时的第 1 和第 45 分钟执行 command

1,45   *    *    *    *  command

c、每天上午 8-12 点的第 1 和 45 分钟执行 command

1,45  8-12  *  *  *  command

d、每隔 2 天的上午 8-12 点的第 1 和 45 分钟执行 command:

1,45  8-12  */2  *   *  command

e、星期一的上午 8 点到 12 点的第 1 和第 45 分钟执行 command

1,45  8-12   *   *  1 command

3. 查看输出

root@i-2udbbtytcx3gv6bvmhzb:~/go/src/auto# tail load.log 
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello

如果您有其他问题,欢迎您联系火山引擎技术支持服务

107
0
0
0
相关产品
评论
未登录
看完啦,登录分享一下感受吧~
暂无评论