All notes
Crontab

Crontab

Configurations

The cron service (daemon) runs in the background (cron 最低侦测限制是分钟) and constantly checks: /etc/crontab, /etc/cron.*/*, and also the /var/spool/cron/*.

In Suse, cron service checkes /etc/crontab and /etc/cron.d/ every minute.

/etc/crontab vs /etc/cron.d

/etc/crontab is the init script. wcfNote: /etc/cron.{hourly,daily,weekly,monthly} are taken care of by /etc/crontab. Custom crons is recommended to be put under /etc/cron.d/. With command line crontab, its result is written to /var/spool/cron/[username], e.g. for user1, /var/spool/cron/user1.

Difference bwteen /etc/cron.d and /etc/crontab (linuxQuestions.org):

A reason for putting a file in /etc/cron.d instead of /etc/cron.hourly can be a finer scheduling control, which you also have in /etc/crontab (but that includes the environment variables).

/etc/crontab 里面默认定义出四项工作任务,分别是每小时、每天、每周及每个月分别进行一次的工作。但是在五个字段后面接的并不是指令,而是一个新的字段,那就是执行指令的身份。这与 crontab -e 不同。由于使用者自己的 crontab 并不需要指定身份,但 /etc/crontab 里面当然要指定身份。以上表的内容来说,系统默认的例行性工作是以 root 来运行的。

crontab 是读到内存当中的,所以在修改完 /etc/crontab 后,可能并不会马上执行,这个时候请重启 crond: /etc/init.d/crond restart.

Log file: /var/log/cron

User Allow and Deny control is same as "at". See below.

Conventional works in Linux:

It is recommended using /etc/cron.d over crontab commandline.


SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
01 * * * * root run-parts /etc/cron.hourly

Why /etc/cron.d/myScript is not working

wcfNote: Do remember to add a blank line in the end of the script file. The cron service reads its config files line by line.

Why lots of mail to ROOT

Do remember to add:



yourScript >/dev/null 2>&1

Command line

crontab [-u username] [-l|-e|-r]

NOTE: "crontab -l" reports on cron entries in /var/spool/cron/crontabs/ - ie the per user crontabs. /etc/cron.d files are system crontabs and not reported by "crontab -l".

Time 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)

"0 3,6 * * * command", 3 or 6 o'clock.
"20 8-10 * * * command", 8:20, 9:20, and 10:20.
"*/5 * * * * command", every 5 minutes.
"* */6 * * * command", run at 00:[01-59]:01, e.g. once every minute, at 0:00, 6:00, 12:00, 18:00.

周数字为 0 or 7 时,都代表星期天。
周与日月不可同时并存。

Examples

Ref.



# Run the command every minute. wcfNote: the command must be executable.
* * * * *  /path/to/command

# 假若你的女朋友生日是 5 月 2 日,你想要在 5 月 1 日的 23:59 發一封信給他,這封信的內容已經寫在 /home/dmtsai/lover.txt 內了,該如何進行?
59 23 1 5 * mail kiki < /home/dmtsai/lover.txt

# 假如你每星期六都與朋友有約,那麼想要每個星期五下午 4:30 告訴你朋友星期六的約會不要忘記,則:
30 16 * * 5 mail [email protected] < /home/dmtsai/friend.txt

# 假如每五分鐘需要執行 /home/dmtsai/test.sh 一次,又該如何?
*/5 * * * * /home/dmtsai/test.sh

Span Mid-night

wcfNote: execute scriptA every 10 minutes during 22:00 to midnight:


*/10 22-23 * * * scriptA

# It will run on 22:10, 22:20, ... 23:00, 23:10, ..., 23:50.

Script to be run in crontab to keep the service up


netstat -tulen | grep 8081 &>/dev/null
res=$?
if [ ! -f /tmp/jiraStarting ] && [ "${res}" == "1" ]; then
	touch /tmp/jiraStarting
	/home/jira/atlassian-jira-6.3.6-standalone/bin/start-jira.sh
	sleep 60
	rm -rf /tm/jiraStarting
fi

Spcial symbols

@

Special string	Meaning
@reboot	Run once, at startup.
@yearly	Run once a year, "0 0 1 1 *".
@annually	(same as @yearly)
@monthly	Run once a month, "0 0 1 * *".
@weekly	Run once a week, "0 0 * * 0".
@daily	Run once a day, "0 0 * * *".
@midnight	(same as @daily)
@hourly	Run once an hour, "0 * * * *".

Examples:
# Run ntpdate command every hour:
@hourly /path/to/ntpdate

-

serverFault.com: crontab dash.

In SuSE /etc/crontab:


-*/15 * * * * root test -x /usr/lib/cron/run-crons && /usr/lib/cron/run-crons >/dev/null 2>&1
If the uid of the owner is 0 (root), he can put a "-" as first character of a crontab entry. This will prevent cron from writing a syslog message about this command getting executed.

How cron handles cron.daily

Code walkthrough on SUSE

/etc/crontab: "/usr/lib/cron/run-crons"

If "DAILY_TIME" is set in /etc/sysconfig/cron, then cron.daily will run at that specified time. Otherwise, "run-crons" use "find -cmin +1440 -or -cmin 1440".

In this first step, "/var/spool/cron/lastrun" lock files are removed due to the find result.


ls -lc /var/spool/cron/lastrun
# total 0
# -rw-r--r-- 1 root root 0 Sep 29 02:30 cron.daily
# -rw-r--r-- 1 root root 0 Sep 29 08:00 cron.hourly
# -rw-r--r-- 1 root root 0 Sep 16 14:30 cron.monthly
# -rw-r--r-- 1 root root 0 Sep 28 02:45 cron.weekly

Debug

en.opensuse.org: Cron.

wcfSuggest: if your /etc/cron.d/yourScript is not working, try to put its content to /etc/crontab to debug.


systemctl status cron
# cron.service - Command Scheduler
#    Loaded: loaded (/usr/lib/systemd/system/cron.service; enabled)
#    Active: active (running) since Thu 2017-08-24 16:54:13 UTC; 2 weeks 3 days ago
#  Main PID: 41079 (cron)
#    CGroup: /system.slice/cron.service
#            └─41079 /usr/sbin/cron -n

cron -nx pars,load

# Available debug flags: ext,sch,proc,pars,load,misc,test,bit.
# If you use "test" flag, syslog will say: "debug DTEST is on, not exec'ing command." It means the command is not executed.

Open syslog for cron

To open syslog for cron, edit in "/etc/sysconfig/cron" as follows:


SYSLOG_ON_NO_ERROR="yes"

Then in /var/log/syslog, logs are written as "run-crons[PROCESS_ID]":

run-crons[30397]

anacron

anacron 并不是用来取代 crontab 的,anacron 的目的在于处理非 24小时一直运行的Linux 系统 crontab 的执行。所以 anacron 并不能指定何时执行某项任务,而是以天为单位或者是在开机后立刻运行 anacron,它会去侦测停机期间应该运行但是并没有运行的 crontab 任务,并将该任务执行一遍后,anacron 就会自动停止了。

at

Allow, Deny

  1. /etc/at.allow, 写在这个档案中的使用者才能使用at。
  2. 如果/etc/at.allow不存在,就寻找/etc/at.deny. 没有在这个 at.deny 档案中的使用者,就可以使用 at。
  3. 如果两个档案都不存在,那么只有root可以使用at。

一般 distributions 当中,由亍假设系统上所有用户都是可信任的,因此系统通常会保留一个空 /etc/at.deny 档案,意思是允许所有人使用 at 指令。

Options

at [-mldv] TIME
at -c jobNum
atq
atrm jobNum
batch

Time format


# Start atd.
/etc/init.d/atd restart

# Configure it on.
chkconfig atd on