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:~#

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lisanmengmeng

蚊子腿也是肉

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值