当前位置: 首页 > news >正文

linux 操作系统下crontab命令及使用案例介绍

linux 操作系统下crontab命令及使用案例介绍

Linux 操作系统下的 crontab 命令用于设置周期性执行的任务

crontab 命令概述

基本语法

bash
crontab [-u user] file crontab [-u user] [-l | -r | -e] [-i] [-s]

主要功能

  • 创建、编辑和管理用户的计划任务(cron job)。
  • 每个用户都可以有自己的 crontab 文件。
  • cron 守护进程会定期检查 crontab 文件并执行相应的任务。

常用选项

  • -u user: 指定要编辑的用户的 crontab 文件。
  • -l: 列出当前用户的 crontab 内容。
  • -r: 删除当前用户的 crontab 文件。
  • -e: 编辑当前用户的 crontab 文件。
  • -i: 在删除 crontab 时给出提示。

crontab 文件格式

每个 crontab 文件的每一行都包含以下字段:

text
minute hour day-of-month month day-of-week command
  • minute: 0-59
  • hour: 0-23
  • day-of-month: 1-31
  • month: 1-12 或 Jan-Dec
  • day-of-week: 0-6 (0 表示周日) 或 Sun-Sat
  • command: 要执行的命令或脚本

命令:

root@meng:~# which crontab

/usr/bin/crontab

root@meng:~# crontab --help

crontab: invalid option -- '-'

crontab: usage error: unrecognized option

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)

root@meng:~# crontab

^Z^Croot@meng:~#

命令案例:

修改crontab默认编辑器

root@meng:~# select-editor

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

Choose 1-4 [1]: 2

root@meng:~#

命令案例;

root@meng:~# ls

f1.txt.bz2 f2.txt.bz2 m1.txt m1.txt.bak m2.txt meng meng.cpio meng.sh meng.txt meng.txt.bz2 rec00001f1.txt s1.txt s2.txt snap tmp

root@meng:~# pwd

/root

root@meng:~# cat meng

cat: meng: Is a directory

root@meng:~# cat meng.sh

#!/bin/bash

echo "meng"

root@meng:~# crontab -e

no crontab for root - using an empty one

crontab: installing new crontab

root@meng:~# crontab -l

# 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

3 * * * * cd /root/meng; /bin/bash meng.sh

root@meng:~#

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • mysql对于上期同期的时间的处理
  • 【QT】使用QOpenGLWidget后,窗口全屏之后右键菜单出不来的问题
  • 软件测试面试少走弯路
  • burp suite professional 产品介绍
  • 程序员转行方向推荐
  • 99AutoML 自动化机器学习实践--NNI 自动化机器学习工具包
  • Rider使用习惯
  • 【C++】——string类的模拟实现
  • OpenAI o1 Review 大模型PHD水平数理推理能力 OpenAI o1 vs GPT4o vs Gemini vs Claude
  • SVN的使用技巧
  • 《黑神话:悟空》怎么备份游戏存档?
  • 【C++】认识C++(前言)
  • 顶级高效的ChatGPT论文润色提示词和使用技巧
  • AndroidStudio清除重置Http Proxy代理的方式
  • golang中string底层数据结构与上层数据结构的关系
  • 【译】JS基础算法脚本:字符串结尾
  • ES6指北【2】—— 箭头函数
  • [NodeJS] 关于Buffer
  • 2018天猫双11|这就是阿里云!不止有新技术,更有温暖的社会力量
  • C++类中的特殊成员函数
  • GitUp, 你不可错过的秀外慧中的git工具
  • idea + plantuml 画流程图
  • js算法-归并排序(merge_sort)
  • Netty 4.1 源代码学习:线程模型
  • SegmentFault 社区上线小程序开发频道,助力小程序开发者生态
  • ⭐ Unity 开发bug —— 打包后shader失效或者bug (我这里用Shader做两张图片的合并发现了问题)
  • uva 10370 Above Average
  • WinRAR存在严重的安全漏洞影响5亿用户
  • 第三十一到第三十三天:我是精明的小卖家(一)
  • 开源SQL-on-Hadoop系统一览
  • 前端技术周刊 2019-02-11 Serverless
  • 浅谈Golang中select的用法
  • ​七周四次课(5月9日)iptables filter表案例、iptables nat表应用
  • ​油烟净化器电源安全,保障健康餐饮生活
  • (1)虚拟机的安装与使用,linux系统安装
  • (14)Hive调优——合并小文件
  • (3) cmake编译多个cpp文件
  • (附源码)springboot炼糖厂地磅全自动控制系统 毕业设计 341357
  • (七)Activiti-modeler中文支持
  • .NET C# 使用GDAL读取FileGDB要素类
  • .NET Core WebAPI中使用swagger版本控制,添加注释
  • .NET的数据绑定
  • .net获取当前url各种属性(文件名、参数、域名 等)的方法
  • .Net转前端开发-启航篇,如何定制博客园主题
  • @Bean有哪些属性
  • @RequestMapping处理请求异常
  • [ 隧道技术 ] 反弹shell的集中常见方式(四)python反弹shell
  • []指针
  • [20161101]rman备份与数据文件变化7.txt
  • [202209]mysql8.0 双主集群搭建 亲测可用
  • [Angular] 笔记 9:list/detail 页面以及@Output
  • [C/C++]数据结构 堆的详解
  • [cocos2d-x]关于CC_CALLBACK
  • [C语言][C++][时间复杂度详解分析]二分查找——杨氏矩阵查找数字详解!!!
  • [C语言]编译和链接