Linux Timer Task tool crontab detailed and system time synchronization

Source: Internet
Author: User
Tags cst time set time centos vps vps

How to perform tasks regularly under Linux

In Linux you should first enter CRONTAB-E, then there will be a VI editing interface, and then enter 0 3 * * 1/clearigame2 content into the inside: Wq save exit.

In Linux, the tasks performed by the cycle are typically handled by the cron Daemon (ps-ef|grep cron). Cron reads one or more configuration files that contain the command line and its invocation time.

The cron configuration file is called "crontab" and is shorthand for "cron table".

First, cron find the configuration file in 3 places:
1,/var/spool/cron/This directory is stored in each user including root crontab task, each task is named after the creator, such as Tom built crontab task corresponding to the file is/var/spool/cron/tom.
Generally, a user has only one crontab file at most.

/etc/crontab This document is responsible for arranging the crontab of maintenance systems and other tasks developed by the system administrator.

/etc/cron.d/This directory is used to store any crontab files or scripts to be executed.

Iv. Permissions
Crontab permissions issue to/var/adm/cron/next look, file Cron.allow and Cron.deny exist
Use the following:
1. If none of the two files exist, only the root user can use the crontab command.
2, if the Cron.allow exists but Cron.deny does not exist, only the users listed in the Cron.allow file can use the crontab command, if the root user is not inside, then the root user can not use crontab.
3, if the Cron.allow does not exist, Cron.deny exist, only the users listed in the Cron.deny file can not use the crontab command, other users can use.
4, if two files exist, then listed in the Cron.allow file and not listed in Cron.deny users can use crontab, if two files have the same user,
If the user is in the Cron.allow file, you can use the crontab command if there is a user in the Cron.allow.

How to enter the command and time to execute in the crontab file. Each row in the file contains six fields, the first five of which are the time the specified command was executed, and the last field is the command to be executed.
Spaces or tabs are used to separate each field. The format is as follows:
Minute hour day-of-month month-of-year day-of-week commands
Legal value 00-59 00-23 01-31 01-12 0-6 (0 is Sunday) commands (represents the script to be executed)
In addition to the numbers there are several special symbols that are "*", "/" and "-", ",", * represent all the values within the range of the number, "/" for each meaning, "/" for each of the 5 units, "-" represents from a number to a number, "," separate several discrete numbers.

Basic format:

Command

Time-sharing Weekly command

The 1th column represents minutes 1~59 per minute with * or */1

The 2nd column represents the hour 1~23 (0 means 0 points)

The 3rd column represents the date 1~31

The 4th column represents the month 1~12

5th Column Identification Number Week 0~6 (0 = Sunday)

6th List of commands to run

Some examples of crontab files:

#每晚的21:30 Restart Apache.

* * * * */usr/local/etc/rc.d/lighttpd restart

#每月1, 10, 22nd

4 1,10,22 * */USR/LOCAL/ETC/RC.D/LIGHTTPD restart

#每天早上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 Jan * Date

Many times, we plan a task that needs to be done in seconds, and it's easy to perform tasks in seconds, depending on the following methods. The following methods will executes once every 10 seconds
1234567 # crontab -e*****/bin/date >>/tmp/date.txt*****sleep 10/bin/date >>/tmp/date.txt*****sleep 20/bin/date >>/tmp/date.txt*****sleep 30/bin/date >>/tmp/date.txt* ****sleep 40/bin/date >>/tmp/date.txt** ***sleep 50/bin/date >>/tmp/date.txt

  

Note that if you use% for the command, you need to escape
123 # backup mysql0001***mysqldump -u root --password=passwd-d mustang > /root/backups/mustang_$(date +\%Y\%m\%d_\%H\%M\%S).sql0101***mysqldump -u root --password=passwd-t mustang > /root/backups/mustang-table_$(date +\%Y\%m\%d_\%H\%M\%S).sql

Use the date command to set the system time.

1. Check the system time

Date

2, set the current system time is February 20, 2017 12:57 0 seconds

Date-s "2017-2-20 12:57:00"

The execution results are as follows:

Linux Time Synchronization Solutions

Due to the hardware reasons, the machine more or less the root standard time is not on, one months of error in a few seconds to a few minutes. For the server time is not allowed, there will be a lot of trouble. For example, when paying, cannot order, the game cannot log in and so on. Your own PC does not matter, manually change it on the line

One, update time from the time server with Ntpdate

If your Linux system doesn't ntpdate this command at all,

Yum Install NTP

After the installation, you do not have to do any configuration, and do not need to test directly

Copy CodeThe code is as follows:
[Email protected] ~]# ntpdate time.nist.gov
OCT 21:11:43 ntpdate[5014]: Adjust time server 207.200.81.113 offset-0.018788 sec

If you go out above the description, the synchronization was successful. Then add the following to the crontab.

1.*/10 * * * * * ntpdate time.nist.gov #域名或IP

Synchronize every 10 minutes. Several time servers are recommended.

Time.nist.gov
Time.nuri.net
asia.pool.ntp.org
asia.pool.ntp.org
asia.pool.ntp.org
asia.pool.ntp.org

Second, use NTP to build your own time server

Above, we use other people's time server to synchronize time, these time servers are more authoritative. When we build our own time server, we don't have to crontab to run regularly.

1, install time server NTP

1.yum Install NTP

2, configuring NTP

Copy CodeThe code is as follows:
[[email protected] ~]# cat/etc/ntp.conf |awk ' {if ($!~/^$/&& $!~/^#/) {print $}} '
Restrict default ignore//does not allow modification or querying of NTP, and does not receive special packets
Restrict 127.0.0.1//Give all permissions to this machine
Restrict 192.168.1.0 mask 255.255.255.0 notrap nomodify//The machine has synchronization time permissions to the LAN machine
Server time.nist.gov prefer//Set time server, plus prefer for priority
Server 0.asia.pool.ntp.org
Server 1.asia.pool.ntp.org
Server 2.asia.pool.ntp.org
Server 127.127.1.0 # Local clock
Fudge 127.127.1.0 Stratum 10
Driftfile/var/lib/ntp/drift
Keys/etc/ntp/keys

3, start NTP

[[email protected] ~]#/etc/init.d/ntpd start

4, view and test

Copy CodeThe code is as follows:
[Email protected] ~]# NETSTAT-UPNL |grep ntpd//view time
[[email protected] ~]# NTPQ-PN//view synchronized server IP
Remote refID St T when poll reach delay offset jitter
==============================================================================
50.77.217.185. Init. u-64 0 0.000 0.000 0.000
202.90.158.4. Init. u-64 0 0.000 0.000 0.000
202.71.100.89. Init. u-64 0 0.000 0.000 0.000
202.134.1.10. Init. u-64 0 0.000 0.000 0.000
*127.127.1.0. Locl. Ten L 18 64 377 0.000 0.000 0.001
[[email protected] ~]# ntpstat//sync Results
Synchronised to local net at Stratum 11
Time correct to within MS
Polling server every S

Remote: The IP or host name of the NTP host. Note that the leftmost symbol, if by "+" represents the upper level NTP that is currently acting on the clock, if "*" indicates that there is also a connection, but as a secondary online NTP host.
refID: Reference to the address of the previous layer of NTP host
ST: the stratum stratum
When: A few seconds ago there was a time synchronization update operation
Poll: Next update after a few seconds
Reach: Number of updates that have been requested for the upper NTP server
Delay: The time the network transmission process clock is delayed
Offset: The result of time compensation
Jitter:linux time difference between system time and BIOS hardware time

Recommend the first method, although the establishment of a time server, or relatively simple, but I do not think it is necessary. If you use someone else's time server, synchronize every 10 minutes, can be accurate to milliseconds.

Author: submarine Eagle

CentOS system time inconsistent with UTC time workaround

Install CentOS to find out that date is not the time we wanted, swollen.

1, Vi/etc/sysconfig/clock #编辑时间配置文件

Zone= "Asia/shanghai"
Utc=false #设置为false, hardware clock is not the same as UTC time
Arc=false

2, Ln-sf/usr/share/zoneinfo/asia/shanghai/etc/localtime #linux的时区设置为上海时区

3, Ntpdate 192.43.244.18 #对准时间

If you do not have an NTP server installed, you just need to execute the following command first:

Yum Install NTP #安装ntp服务器

4,/sbin/hwclock--SYSTOHC #设置硬件时间和系统时间一致并校准

Well, our CentOS Linux system time and computer hardware time is finally the CST time, and all are Shanghai time zone, finally is normal.

CentOS VPS Modified time zone out of Sync Ultimate solution

Due to the first use of the domestic Hyper-V architecture VPS and to buy before the installation of Windows or CentOS system, because the wood has a heavy panel, for the use of Debian I really torture, the operation of the process encountered a variety of small problems. Ask the customer service directly a sentence only understand winows environment, let you install win you have to install Linux. Well, CentOS can only find its own. Because Hyper-V is Microsoft's virtualization product, earlier than Linux, and because of Debian's corporate operations, so that Debian into a high-speed development phase, even Google also abandoned the Linux customization and into the Debian development camp, which makes Microsoft feel some pressure , so Microsoft pulled up CentOS, which is why Hyper-V only supports CentOS, not other Linux-derived versions, and the support is not so reliable. When you're finished, let's take a look at the chase.

The point: The Hyper-V architecture of the centos5.5 VPS is out of sync with lnmp time, causing some features of the site to be unusable.

On the internet to find a variety of methods have failed. Then I carefully find some documents finally fix, tears ran AH.

The time management of Linux is strange that he divides the system time and hardware time into what UTC time is also broken into. -_-| | |.

Hardware time is fundamentally a CMOS clock

Linux Viewing hardware time: Hwclock--show

Linux viewing system time: Date-r

The time zone of the CentOS VPS is synchronized.

System Time Section:

1. Edit the TimeZone profile: Vi/etc/sysconfig/clock

Zone= "Asia/shanghai"

Utc=false #把UTC设置为false关闭状态

Arc=false

2. Run the following command

#删除默认配置

Rm-rf/etc/localtime

#重新指定配置

Ln-s/usr/share/zoneinfo/asia/shanghai/etc/localtime

#同步系统时间

Ntpdate-u pool.ntp.org

3. Hardware Time section:

#将硬件时间调整成与目前的系统时钟一致.

Hwclock--SYSTOHC
or clock--SYSTOHC

#强制系统时间写入CMOS中防止重启失效

Hwclock-w
or clock-w

The above steps were successful, but my Hyper-V architecture's CentOS system time is not allowed, 1 days can be as fast as a few minutes.

Use crontab to synchronize the time can be solved.

#编辑计划任务表

Vi/etc/crontab

#表中最后添加下面这行内容

5 * * * root/usr/sbin/ntpdate-u pool.ntp.org &&/sbin/clock--SYSTOHC

#每天5点半同步时间, Clock--SYSTOHC is the time synchronization system time of the hard disk meaning.

How to see if the Crontab timed task executes

Http://jingyan.baidu.com/article/73c3ce28e7b298e50343d925.html

Linux Timer Task tool crontab detailed and system time synchronization

Related Article

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.