1. 程式人生 > >Crontab – Quick Reference

Crontab – Quick Reference

Setting up cron jobs in Unix, Solaris & Linux

cron is a Unix, solaris, Linux utility that allows tasks to be automatically run in the background at regular intervals by the cron daemon. 

cron meaning – There is no definitive explanation but most accepted answers is reportedly from Ken Thompson ( author of unix cron ), name cron comes from chron ,the Greek prefix for ‘time.’.
What is cron ? 

– Cron is a daemon which runs at the times of system boot from /etc/init.d scripts. If needed it can be stopped/started/restart using init script or with command service crond start in Linux systems.

This document covers following aspects of Unix, Linux cron jobs to help you understand and implement cronjobs successfully

  1. What is crontab?
  2. What is a cron job or cron schedule?
  3. Crontab Restrictions
  4. Crontab Commands
  5. Crontab file – syntax
  6. Crontab Example
  7. Crontab Environment
  8. Disable Email
  9. Generate log file for crontab activity
  10. Crontab file location

1. What is crontab?

Crontab (CRON TABle) is a file which contains the schedule of cron entries to be run and at specified times. File location varies by operating systems, See Crontab file location at the end of this document.

2.What is a cron job or cron schedule?

Cron job or cron schedule is a specific set of execution instructions specifing day, time and command to execute. crontab can have multiple execution statments.

3. Crontab Restrictions

You can execute crontab if your name appears in the file /usr/lib/cron/cron.allow. If that file does not exist, you can use
crontab if your name does not appear in the file /usr/lib/cron/cron.deny.
If only cron.deny exists and is empty, all users can use crontab. If neither file exists, only the root user can use crontab. The allow/deny files consist of one user name per line.

4. Crontab Commands

export EDITOR=vi ;to specify a editor to open crontab file.

crontab -e    Edit crontab file, or create one if it doesn’t already exist.
crontab -l    crontab list of cronjobs , display crontab file contents.
crontab -r    Remove your crontab file.
crontab -v    Display the last time you edited your crontab file. (This option is only available on a few systems.)

5. Crontab file

Crontab syntax :
A crontab file has five fields for specifying day , date and time followed by the command to be run at that interval.

*     *     *   *    *        command to be executed
-     -     -   -    -
|     |     |   |    |
|     |     |   |    +----- day of week (0 - 6) (Sunday=0)
|     |     |   +------- month (1 - 12)
|     |     +--------- day of        month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (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).
Notes
A. ) Repeat pattern like /2 for every 2 minutes or /10 for every 10 minutes is not supported by all operating systems. If you try to use it and crontab complains it is probably not supported.

B.) The specification of days can be made in two fields: month day and weekday. If both are specified in an entry, they are cumulative meaning both of the entries will get executed .

6. Crontab Examples

A line in crontab file like below removes the tmp files from /home/someuser/tmp each day at 6:30 PM.

30     18     *     *     *         rm /home/someuser/tmp/*

Changing the parameter values as below will cause this command to run at different time schedule below :

min hour day/month month day/week Execution time
30 0 1 1,6,12 * — 00:30 Hrs  on 1st of Jan, June & Dec.
0 20 * 10 1-5 –8.00 PM every weekday (Mon-Fri) only in Oct.
0 0 1,10,15 * * — midnight on 1st ,10th & 15th of month
5,10 0 10 * 1 — At 12.05,12.10 every Monday & on 10th of every month
:

Note : If you inadvertently enter the crontab command with no argument(s), do not attempt to get out with Control-d. This removes all entries in your crontab file. Instead, exit with Control-c.

7. Crontab Environment

cron invokes the command from the user’s HOME directory with the shell, (/usr/bin/sh).
cron supplies a default environment for every shell, defining:
HOME=user’s-home-directory
LOGNAME=user’s-login-id
PATH=/usr/bin:/usr/sbin:.
SHELL=/usr/bin/sh

Users who desire to have their .profile executed must explicitly do so in the crontab entry or in a script called by the entry.

8. Disable Email

By default cron jobs sends a email to the user account executing the cronjob. If this is not needed put the following command At the end of the cron job line .

>/dev/null 2>&1

9. Generate log file

To collect the cron execution execution log in a file :

30 18 * * * rm /home/someuser/tmp/* > /home/someuser/cronlogs/clean_tmp_dir.log

10. Crontab file location

User crontab files are stored by the login names in different locations in different Unix and Linux flavors. These files are useful for backing up, viewing and restoring but should be edited only with crontab command by the users.

  • Mac OS X
    /usr/lib/cron/tabs/
  • BSD Unix 
    /var/cron/tabs/
  • Solaris, HP-UX, Debian, Ubuntu
    /var/spool/cron/crontabs/
  • AIX, Red Hat Linux, CentOS, Ferdora
    /var/spool/cron/

相關推薦

CrontabQuick Reference

Setting up cron jobs in Unix, Solaris & Linux cron is a Unix, solaris, Linux utility that allows tasks to be automatically run in the

The Pragmatic Programmer Quick Reference Guide

designers apple ssi similar face concept rect part nat This page summarizes the tips and checklists found in The Pragmatic Programmer. Fo

a quick reference guide for the CLI command

Top 10 options for docker run — a quick reference guide for the CLI commandThe docker run CLI command has around 100 options, including -d, -i, -t, -v, and

LTE Quick Reference, USIM Parameters

: ACC stands for Access Control Class. The access control class is a parameter to control the access attempts. 15 classes are split into 10 classes random

Typora系列之Quick Start-Markdown Reference

        最近發現了 Markdown 這種從來沒有接觸過的寫作方式,看起來很厲害的樣子。於是用了一個週末的時間研究了好幾個 Markdown 寫作軟體,如Markdown 2,Yu writer,Typora等,綜合對比 Typora 最符合我的口味,該軟體目前還在內

Quick RF Tips for General Reference

可行性 power probably 需要 bstr amount oid closure ear 傳送門:http://www.microwavetools.com/rf-tips-to-make-you-look-smart/ 全文搬運過來的,本篇文章並未有其它意義和目

quick-cocos2d-x教程1:在window上創建第一個項目文件夾,並制作helloworld

項目 編碼 rip pla 命令模式 utf-8 cape tps quick 說明:此教程是針對cocos2dx 2.0系列的,3.0的版本號,如今還沒有公布出來。 1)首先從github.com把這個項目下載到本地。然後裝到d盤的根文件夾,並設置文件夾路徑為d:\

Qt Quick編程(1)

參數表 核心 參數 boolean push 不同之處 ++ 只讀 部分 說道QML,不得不先說一下ECMAScript: ECMAScript語言的標準是由Netscape、Sun、微軟、Borland等公司基於JavaScript和JScript錘煉、定義出來的。 EC

php5.6 CI的問題(Only variable references should be returned by reference)

message error nbsp ble erro only class reference php [轉載] A PHP Error was encountered Severity: Notice Message: Only variable references

crontab計劃任務無法執行(小結)

crontab最近在與朋友梳理運維中遇到的坑的時候,發現大家都遇見過crontab計劃任務沒法正常執行的情況,如是簡單的整理下,主要有如下幾種情況:1、環境變量是否定義說明:crontab執行shell時,只能識別為數不多的環境變量,所有在腳本中最好使用export重新聲明下該變量說明:shell腳本rman

crontab定時任務

roo 刪除 rontab port .py 絕對路徑 n) for 使用 crontab -e 0 4 * * * /root/scripts/mysqlbackup.sh 0 4 * * * /root/scripts/apacherestart.sh 0 5 * *

Crontab命令--Linux

任務 http images .com png cnblogs nbsp 定時 font Crontab命令--定時任務 命令格式 Example: Crontab命令--Linux

[ shell編程 ] crontab使用方法

運行時 控制臺 mce 副本 pps ech 同時 用法 腳本 在學習crontab命令之前,你看下有沒有碰到過如下場景: (1)測試服務器每天都會產生很多垃圾文件,手工每天去清理非常繁瑣,如何讓系統自動按時清理呢? (2)如何定時收集服務器的關鍵信息呢?

Linux定時任務Crontab命令

主目錄 日誌清理 文件中 定義 服務 系統命令 root spool 詳細 linux 系統則是由 cron (crond) 這個系統服務來控制的。Linux 系統上面原本就有非常多的計劃性工作,因此這個系統服務是默認啟動的。另 外, 由於使用者自己也可以設置計劃任務,所以

使用crontab,讓linux定時執行shell腳本

表示 inux 包含 tor span 之間 line mage sun 閱讀目錄 1. cron服務【Ubuntu環境】 2. crontab用法 3. 編輯crontab文件 4. 流程舉例 5. 幾個例子 Linux中,周期執行的任務一般由cron

CentOS Crontab(定時任務)

status stat ntsysv 設置 backup pda 啟動服務 brush 分鐘 安裝crontab: yum install crontabs 說明: service crond start //啟動服務 service crond stop //關閉服務 s

linux定時任務crontab

9.png linux .cn 命令 png alt nbsp ont 技術分享 *  *  *  *  *  command 分 時 日 月 周 命令 linux定時任務crontab

[Shell]crontab 運行任務調用shell腳本,相對路徑無法找到

.net question pytho all call 路徑 con 調用 style 問題出現的場景大概就是 1 cron調用一個python腳本 2 python腳本中調用一個shell腳本(對日誌分析)獲取shell輸出然後發送郵件 類似一個監控任務。

centos 安裝 crontab 和 簡單的使用教程

技術 時間 ffffff table image res start 主程序 主程 crontab是一個非常好用的定時執行任務的程序.以下是操作方式 1 安裝 cron 的 主程序 : yum -y install vixie-cron 2 安裝 cron 守護進程的表格的

Quick-Cocos2d3.2RC1在Code IDE中實現代碼提示

ref 使用 分享 tin doc tails 位置 壓縮包 http 之前寫Lua最痛苦的就是代碼提示問題,如今官方給了IDE很好用。以下說Quick使用IDE加入代碼提示問題。 第一步:制作api提示壓縮包。須要使用控制臺實現方法例如以下: 1、找到framew