How to use cron for regular tasks

本文介绍如何使用crontab设置Linux系统中的定时任务。包括crontab的基本概念、常用命令、配置文件格式及示例,并说明了如何启动、停止和重启cron服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

How to use crontab for regular tasks

[Precondition]

OS     : Redhat Fedora 2

 

[Brief introduction]

Cron is a daemon task to allow others tasks to be automatically run at the regular interval. Cron then wakes up every minute, and examining all crontab files stored, and checking each command to see whether it should be run in the current minute. When executing commands, any output is mailed to the owner of the crontab or the user named in the MAILTO environment variable in the crontab, if such exists.

Crontab maintains crontab files for individual users, so we can use crontab command to edit/remove/list crontab files.

 

[Crontab Commands]

crontab –e        Edit crontab file, or create one if it doesn't already exist.

crontab -l      Display your crontab file.

crontab -r      Remove your crontab file.

crontab –u       The user of crontab, if no –u option, it means to use current login user.

 

[Format of crontab file]

* * * * * command to be executed

| | | | |

| | | | +----- day of week (0 - 7) (Sunday=0/7)

| | | +------- month (1 - 12)

| | +--------- day of month (1 - 31)

| +----------- hour (0 - 23)

+------------- minute (0 - 59)

* in the value field above means all legal values as in braces for that column. The value column can have a * or a list of elements separated by commas. An element is either a number in the ranges shown above or two numbers in the range separated by a hyphen (meaning an inclusive range).

For example:

*/2 * * * * echo ‘Append me’ >> /tmp/tmp.txt

It means that ‘Append me’ will be appended into /tmp/tmp.txt every minute. ‘2’ is step, and it can be used front five time elements.

 

*/2 1,3,5 * * * echo ‘Append me’ >> /tmp/tmp.txt

Only at 1th, 3th, 5th hour will append ‘append me’ into /tmp/tmp.txt every minute.

In words, we can use shell scripts to do something and add running commands into cron daemon to do at the background. So crontab –e should be used directly.

 

[How to start/stop/restart cron]

#service crond start/stop/restart

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值