cron Configuration-LINUX

简介: 这个解释得比较细致。。 http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/ How do I add cron job under Linux or UNIX like operatin...

这个解释得比较细致。。

http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/

How do I add cron job under Linux or UNIX like operating system?

Cron job are used to schedule commands to be executed periodically. You can setup commands or scripts, which will repeatedly run at a set time. Cron is one of the most useful tool in Linux or UNIX like operating systems. The cron service (daemon) runs in the background and constantly checks the /etc/crontab file, /etc/cron.*/ directories. It also checks the /var/spool/cron/ directory

crontab is the command used to install, deinstall or list the tables (cron configuration file) used to drive the cron daemon in Vixie Cron. Each user can have their own crontab file, and though these are files in /var/spool/cron/crontabs, they are not intended to be edited directly. You need to use crontab command for editing or setting up your own cron jobs.

Different Types of cron Configuration

There are two different types of configuration files:

  1. The UNIX / Linux system crontab : Usually, used by system services and critical jobs that requires root like privileges. The sixth field (see below for field description) is the name of a user for the command to run as. This gives the system crontab the ability to run commands as any user.
  2. The user crontabs: User can installer their own jobs using the crontab command. The sixth field is the command to run, and all commands run as the user who created the crontab

How Do I Install / Create / Edit My Own Cronjobs?

To edit your crontab file, type the following command at the UNIX / Linux shell prompt:
$ crontab -e

Syntax of crontab (Field Description)

Your cron job looks as follows for user jobs:

 
1 2 3 4 5 /path/to/command arg1 arg2
 

OR

 
1 2 3 4 5 /root/backup.sh
 

Where,

  • 1: Minute (0-59)
  • 2: Hours (0-23)
  • 3: Day (0-31)
  • 4: Month (0-12 [12 == December])
  • 5: Day of the week(0-7 [7 or 0 == sunday])
  • /path/to/command - Script or command name to schedule

Easy to remember format:

* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

Your cron job looks as follows for system jobs:

1 2 3 4 5 USERNAME /path/to/command arg1 arg2

OR

1 2 3 4 5 USERNAME /path/to/script.sh

Example: Install Backup Job Script

If you wished to have a script named /root/backup.sh run every day at 3am, your crontab entry would look like as follows. First, install your cronjob by running the following command:
# crontab -e
Append the following entry:
0 3 * * * /root/backup.sh
Save and close the file.

More Examples

To run /path/to/command five minutes after midnight, every day, enter:
5 0 * * * /path/to/command
Run /path/to/script.sh at 2:15pm on the first of every month, enter:
15 14 1 * * /path/to/script.sh
Run /scripts/phpscript.php at 10 pm on weekdays, enter:
0 22 * * 1-5 /scripts/phpscript.php
Run /root/scripts/perl/perlscript.pl at 23 minutes after midnight, 2am, 4am ..., everyday, enter:
23 0-23/2 * * * /root/scripts/perl/perlscript.pl
Run /path/to/unixcommand at 5 after 4 every Sunday, enter:
5 4 * * sun /path/to/unixcommand

目录
相关文章
|
2月前
|
监控 Unix Linux
|
5月前
|
存储 运维 监控
运维.Linux下执行定时任务(中:Cron的常用替代方案)
本文是关于Linux下执行定时任务系列的第二部分,主要探讨除了Cron之外的常用替代方案。介绍了Systemd Timers、Anacron及at命令三种工具,它们分别适用于不同场景下的定时任务需求。文章详细分析了每种工具的特点、工作原理、基本使用方法及其高级功能,并对比了它们各自的优缺点,帮助读者根据实际情况选择最适合的定时任务解决方案。此外,还提供了指向具体实例和进一步阅读材料的链接。
231 4
运维.Linux下执行定时任务(中:Cron的常用替代方案)
|
4月前
|
Unix Linux Python
Cron定时设置在linux和mac中的使用
文章详细说明了如何在Linux和Mac操作系统中使用Cron进行定时任务的设置,并提供了多个Cron表达式的实例。
49 0
|
5月前
|
存储 监控 Linux
|
5月前
|
存储 运维 监控
|
5月前
|
Linux 调度
在Linux中,如何使用cron和at命令进行任务调度?
在Linux中,如何使用cron和at命令进行任务调度?
|
5月前
|
监控 安全 Linux
在Linux中,如何设置定时任务(cron job)?
在Linux中,如何设置定时任务(cron job)?
|
5月前
|
Linux 调度
在Linux中,如何使用cron进行任务调度?
在Linux中,如何使用cron进行任务调度?
|
5月前
|
监控 Linux 网络安全
在Linux中,什么是cron作业?如何创建一个cron作业?
在Linux中,什么是cron作业?如何创建一个cron作业?
|
5月前
|
Ubuntu Linux Shell
在Linux中,如何定期执行任务,如cron作业设置?
在Linux中,如何定期执行任务,如cron作业设置?