Ubuntu crontab detailed rules and solutions for non-execution

Source: Internet
Author: User
Tags ssh

detailed usage of CRONTAB commands in Linux systems:


If you do not see this command in your system, install the following two packages.

Vixie-cron

Crontabs

Crontab is used to allow the user to execute a program at a fixed time or at a fixed interval, in other words, a user-like schedule. -u user is the time table that specifies the user, as long as you have permission (for example, root) to specify the schedule for others. If you do not use-u user, it means setting your own schedule.

Common parameters:

Crontab-l//view cron tasks under current user

CRONTAB-E//Edit the current user's timed task

Crontab-u linuxso-e//Edit user Linuxso timing Tasks

specific usage and format:

Basic format:
* * * * command
Time-sharing and Lunar Week command

The 1th column represents minutes 1~59 per minute with * or */1
The 2nd column represents the hour 1~23 (0 for 0 points)
The 3rd column represents the date 1~31
The 4th column represents the month 1~12
The 5th list of the week 0~6 (0 for Sunday)
6th column the command to run

Some examples of crontab files:

* * * */usr/local/etc/rc.d/lighttpd restart
The above example represents 21:30 restart Apache per night.

4 1,10,22 * */USR/LOCAL/ETC/RC.D/LIGHTTPD restart
The above example represents the 4:45 reboot of Apache for 1, 10, and 22nd per month.

1 * * 6,0/USR/LOCAL/ETC/RC.D/LIGHTTPD restart
The above example shows the 1:10 reboot of Apache every Saturday and Sunday.

0,30 18-23 * * * */usr/local/etc/rc.d/lighttpd restart
The example above shows that Apache is restarted every 30 minutes from 18:00 to 23:00 every day.

0 * * 6/USR/LOCAL/ETC/RC.D/LIGHTTPD restart
The above example indicates that 11:00 PM restarts Apache every Saturday.

* */1 * * * */usr/local/etc/rc.d/lighttpd restart
Restart Apache every hour

* 23-7/1 * * * */usr/local/etc/rc.d/lighttpd restart
From 11 o'clock to 7 in the morning, restart Apache every hour.

0 4 * mon-wed/usr/local/etc/rc.d/lighttpd restart
4th per month with 11 points per Monday to Wednesday restart Apache

0 4 1/USR/LOCAL/ETC/RC.D/LIGHTTPD * Restart
Restart Apache at 4 o ' January 1.

Name: crontab

Use Rights: All users

How to use:

crontab file [-u user]-replaces the current crontab with the specified files.

Crontab-[-u user]-replaces the current crontab with standard input.

crontab-1[user]-lists the user's current crontab.

crontab-e[user]-Edit user's current crontab.

crontab-d[user]-deletes the user's current crontab.

Crontab-c dir-Specifies the directory for crontab.

crontab file format: M H d M D cmd.

M: Minutes (0-59).

H: Hours (0-23).

D: Days (1-31).

M: Month (1-12).

D: Days of the week (0~6,0 for Sunday).

CMD to run the program, the program was sent to SH execution, this shell only user,home,shell these three environment variables

Description

Crontab is used to allow the user to execute a program at a fixed time or at a fixed interval, in other words, a user-like schedule. -u user refers to the setting specified

User's time table, the premise is that you have to have permission (for example, root) to specify other people's schedule. If you do not use-u user, it means that you set the

Set your own schedule.

Parameters:

CRONTAB-E: Execute the text editor to set the schedule, the default text editor is VI, if you want to use another text editor, please set the VISUAL environment variables

To specify the use of that text editor (for example, Setenv VISUAL Joe)

Crontab-r: Deletes the current schedule table

Crontab-l: List the current schedule

crontab file [-u user]-replaces the current crontab with the specified files.

The format of the schedule table is as follows:

F1 F2 F3 f4 f5 program

Where F1 is the minute, F2 represents the Hour, F3 represents the day of the month, the F4 represents the month, and the F5 represents the day of the one week. program says to hold

Line of the program.

When F1 is *, it means that the PROGRAM,F2 is executed every minute for *, and the rest of the program is executed every hour.

When F1 is a-b to be executed from the time of the first a minute to the B minute, the F2 is a-b to be performed from A to a B-hour, and the remainder of the analogy

When F1 is */n, it is executed once every n minutes, F2 is performed once per N-hour intervals for */n, and the rest

When F1 is a, B, C,... The first A, B, C,... Minutes to execute, F2 for a, B, C,... The first is a, B, c ... An hour to execute, and the rest of the analogy

The user can also store all the settings in the file file, using crontab file to set the schedule.

Example: Data collation www.linuxso.com Linux safety net

0 */2 * * * */sbin/service httpd restart means to restart Apache every two hours

7 * * * */sbin/service sshd start with SSH service 7:50 every day

* * * * */sbin/service sshd stop means shut down SSH service 22:50 every day

0 0 1,15 * * fsck/home 1th and 15th monthly check/home disk

1 * * * * * */home/bruce/backup The first part of the hour to execute/home/bruce/backup this file

* * 1-5 find/home "*.xxx"-mtime +4-exec rm {}/; Every Monday to Friday 3 o'clock, in the directory/home, locate the file named *.xxx and delete the file 4 days ago.
6 */10 * * ls means 1, 11, 21, 31st of every month Yes 6:30 execute the LS command once

#每天早上7点执行一次/bin/ls:

0 7 * * */BIN/LS

In December, every day at 6 to 12, every 3 hours of the morning/usr/bin/backup are performed:

0 6-12/3 */usr/bin/backup

Send a letter to Alex@domain.name from Monday to Friday every 5:00:

0 * * 1-5 mail-s "HI" Alex@domain.name </tmp/maildata

Every month at midnight 0:20, 2:20, 4:20 .... Execute echo "haha"

0-23/2 * * * echo "haha"

Attention:

When the program is executed at the time you specify, the system will send you a letter showing the execution of the program, and if you do not wish to receive such a letter, please leave a blank space in each line

After adding >/dev/null 2>&1

Example 2:

#每天早上6点10分

6 * * * Date

#每两个小时

0 */2 * * * Date

#晚上11点到早上8点之间每两个小时, 8 in the morning.

0 23-7/2,8 * * * Date

#每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点

0 4 * mon-wed date

#1月份日早上4点

0 4 1, * date

Example

$crontab-L lists the user's current crontab.

crontab The command does not perform the workaround:


Add the following sentence to the second line of the script file

Path=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

The following is an analysis of the steps to resolve the problem:

1. Failure to perform is caused by environmental variables

2. The default environment variable configuration method for configuring Cron is not currently found

Vim/etc/crontab can see that the environment variables in this file are correct.

3. * * * * * * * * */root/test.sh >ifconfig.txt 2>&1 & Every Minute test.sh

Test.sh's content is as follows, you can see the output environment variable is/usr/bin:/bin

#!/bin/bash

#PATH =/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

Ifconfig

Echo $PATH

Output:

/root/test.sh:line 3:ifconfig:command not found

/usr/bin:/bin

4. View the location of the Ifconfig

root@ubuntu:~# Whereis Ifconfig

Ifconfig:/sbin/ifconfig/usr/share/man/man8/ifconfig.8.gz

5. Comment out the second line of the test.sh script to see the output as follows

Eth0 Link encap:ethernet hwaddr aa:00:04:00:0a:04

inet addr:192.168.1.60 bcast:0.0.0.0 mask:255.255.255.0

Inet6 ADDR:FE80::20C:29FF:FEF8:73B6/64 Scope:link

Up broadcast RUNNING multicast mtu:1500 metric:1

RX packets:2093659 errors:0 dropped:0 overruns:0 frame:0

TX packets:1532815 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:519122457 (519.1 MB) TX bytes:447226592 (447.2 MB)

Lo Link encap:local Loopback

inet addr:127.0.0.1 mask:255.0.0.0

Inet6 addr::: 1/128 scope:host

Up loopback RUNNING mtu:16436 metric:1

RX packets:433596 errors:0 dropped:0 overruns:0 frame:0

TX packets:433596 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:0

RX bytes:23228192 (23.2 MB) TX bytes:23228192 (23.2 MB)

/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

The 6.test.sh script only temporarily changes the value of path, and it only takes effect on the current script

  Come from http://blog.chinaunix.net/uid-26675196-id-3243278.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.