Linux timed tasks and exercises

Source: Internet
Author: User

1th. Scheduled Tasks 1.1 What is a scheduled task

The equivalent of an alarm clock wakes you up every day

Set a time to do sth.

1.2 System timing Tasks
[[Email protected] ~]#ll-d/etc/cron*drwxr-xr-x. 2 root root 4096 Jul 18:24/etc/CRON.D drwxr-xr-x. 2 root root 4096 Jul 18:24/etc/cron.daily System timed tasks run the contents of this directory every day drwxr-xr-x. 2 root root 4096 Jul 18:23/etc/cron.hourly System timed tasks run the contents of this directory every hour drwxr-xr-x. 2 root root 4096 Jul 18:24/etc/cron.monthly System timed tasks run the contents of this directory every month drwxr-xr-x. 2 root root 4096 Sep 2011/etc/cron.weekly System timed tasks run the contents of this directory weekly -RW-------. 1 root root 0 2016/etc/ blacklist of cron.deny system timed tasks -rw-r--r--. 1 root root 457 Sep 2011/etc/crontabOne of the configuration files for the system timer task
~]#  ll/var/log/messages*/var/log/secure*-rw-------  1 root root   1132  7 15:34/var/ log/messages-RW-------. 1 root root 110667 Jul 07:11/var/log/messages-20180520-rw-------. 1 root root  98541 Jul 12:29/var/log/messages-20180730-rw-------  1 root root   1273 Jul 03:39/var/log/mes SAGES-20180807-RW-------  1 root root   6131  8 17:20/var/log/Secure-rw-------. 1 root root  17996 03:30/var/log/secure-20180520-rw-------. 1 root root  46696 Jul 13:06/var/log/secure-20180730 -RW-------  1 root root   5764  7 09:10/var/log/secure-20180807

1.3 User's Scheduled Tasks
Crontab- View user's Scheduled tasks- Edit user's timer task/var/spool/cron/root   root user's timer task  -L  =  cat/var/spool/cron/-E  =  vi/var/spool/cron/root
1.3.1 Why use Crontab

1, has the grammar check function

2. Convenient

1.3.2 Check if scheduled tasks are running

Timed Task Service Crond

~]#  /etc/init.d/crond statuscrond (PID  is~]#  ps-ef | grep crondroot       1572      1  0 Aug07?        00:00:00 crondroot      27470  26990  0 17:30 pts/2    00:00:00 grep--color Crond
1.3.3 to see if Crond is powered on from boot
[[Email protected] ~] # chkconfig |grep Crond crond          0:off   1:off   2:on    3:on    4:on    5:on    6:off
1.4 Related directories for scheduled tasks
/etc/spool/cron   Timer task configuration file  /var/log/cron     Timer task log file  /etc/cron.deny   blacklist of Scheduled Tasks
1.5 basic format of Scheduled tasks

Time-sharing Week

Note: Weeks and dates cannot be used at the same time as: February 2020 28 weeks Six

Cases:

Every day 8:30 A.M., come to the school to get on the bus (go to school)

* * * * go to School

Every night 12 o'clock on time, go home and drive (go to bed)

XX 0 * * * go to bed

1.6 Common symbols in timed tasks

* per

*/n every n long

-What time is the time

Segmentation

1.7 Timed Task Test
1, command [[email protected]~]#Echo Zeq >/tmp/name.log[[Email protected]~]#Cat/tmp/name.logZeq2, writing timed tasks [[email protected]~]#crontab-e#Print name zhangenqing at 2018.xx.xx* * * * * echo Zeq >>/tmp/Name.log viewing Scheduled tasks [[email protected]~]#crontab-l#Print name zhangenqing at 2018.xx.xx* * * * * echo Zeq >>/tmp/Name.log3, view timed task log [email protected]~]#tail-3/var/log/cron the8 19:36:23 oldboy50 crontab[27563]: (Root) END EDIT (root)8 19:37:01 oldboy50 crond[1572]: (Root) RELOAD (/var/spool/cron/root) the8 19:37:01 oldboy50 crond[27572]: (Root) CMD (echo Zeq >>/tmp/Name.log)4, check the contents of the file [[email protected]~]#Cat/tmp/name.logZeqzeq

1.8 Timing Task Writing process 1.8.1 first milestone

Command-line test to display your name in a file

1.8.2 a second milestone

Put the command in the script

1.8.3 a third milestone

Test whether the script can be used

The fourth milestone of 1.8.4

Writing timed Tasks

    1. Execute every Minute
    2. Change to the required time
The fifth milestone of 1.8.5

Check Results

    1. View the logs for a scheduled task
    2. To see if the command was executed successfully

The 2nd chapter of the scheduled task exercise 2.1 Every Night 12 o'clock Pack backup/etc directory to/tmp below the Script 2.1.1 command
[[Email protected] ~] # tar zcf  /tmp/etc-' Date +%f_%t '. tar.gz  /etc/tar:removingleading '/' from Member namestar:removing leading '/' from hard link targets~]# c12> ll/tmp/9528-rw-r--r--1 root root 9752749  9 15:26 etc-2018-08-09_15:26:26.tar.gz
2.1.2 Writing scripts
[[Email protected] ~]#cat/server/scripts/bak-etc.shTar zcf/tmp/etc-' Date +%f_%t '. tar.gz/etc/
Test script [[email protected]~]#sh/server/scripts/bak-etc.shtar:removing Leading '/'From member namestar:removing Leading '/'From hard link targets[[Email protected]~]#ll/tmp/ Total19056-rw-r--r--1 root root 9752749 9 15:26 etc-2018-08-09_15:26:26. tar.gz-rw-r--r--1 root root 9752749 9 15:29 etc-2018-08-09_15:29:23.tar.gz

2.1.3 Writing timed tasks
[[Email protected] ~] # crontab-  e-XX * * */bin/sh/server/scripts/bak-etc.sh >/dev/null2>&1 ~] # crontab-lXX * * */bin/sh/server/scripts/bak-etc.sh >/dev/null 2>&1~]#  ll/ tmp/28584-rw-r--r--1 root root 9752749  9 15:26 etc-2018-08-09_15:26:26. tar.gz- rw-r--r--1 root root 9752749  9 15:29 etc-2018-08-09_15:29:23. tar.gz-rw-r--r--1 root root 9752749  9 15:41 etc-2018-08-09_15:41:42.tar.gz

2.1.4 Modify the scheduled task time according to the required time
[[Email protected] ~] # crontab-lXX * * * */bin/sh/server/scripts/bak-etc.sh >/dev/null 2>&1
2.1.5 Scheduled Tasks-command or script results (correct and error) orientation

To a black hole (>/dev/null 2>&1) or append to a file >>/tmp/zeq.txt 2>&1

2.2 Corporate Case List

If the end of the scheduled task rule does not add >/dev/null 2>&1 or append to the file >>/tmp/oldboy 2>&1, it is easy to cause the hard disk inode space is full, the system service is not normal.

2.2.1 Scheduled Tasks keep sending you emails

You have new mail in/var/spool/mail/root

2.3 Pack backups every two hours/etc/rc.local/etc/hosts/etc/fstab/etc/sysconfig backup to/backup directory
~]#  cat/server/scripts/bak-conf.shtar zchf  /backup/conf-' Date +%f_%t '. tar.gz   / Etc/rc.local  /etc/hosts/etc/fstab  /etc/sysconfig/~]#  crontab-lXX * * */bin/ sh/server/scripts/bak-etc.sh >/dev/null 2>&100 */2 * * * sh  /server/scripts/bak-conf.sh >/dev/null 2& Gt;&1
2.3.1 Mail software off, scheduled tasks keep sending you messages stored in the temporary directory of the message waiting to be sent

The inode ran out of regular task writing.

2.4 Packed every night 12 o'clock backup/etc/directory Backup to/backup under Backup to/backup/10.0.0.2002.4.1 view IP
[[Email protected] ~] # hostname-i10.0.0.201
2.4.2 Modifying variable IP
[[Email protected] ~] # ip= ' Hostname-i|awk ' {print '} '
2.4.3 Viewing variables
[[Email protected] ~] # Echo $ip10.0.0.201
2.4.4 Test Commands
Create IP directory [[email protected]~]#mkdir-p/backup/$ip[[Email protected]~]#ll/backup/ Total136drwxr-xr-x 2 root root 4096 9 16:20 10.0.0.201-rw-r--r--1 root root 61485 9 15:56 conf-2018-08-09_15:56:52. tar.gz-rw-r--r--1 root root 61485 9 16:00 conf-2018-08-09_16:00:01. tar.gz-rw-r--r--. 1 root root 609 Jul 12 07:49conf.tar.gz Test command [[email protected]~]#tar zcf/backup/$ip/etc-' Date +%f_%t '. tar.gz/etc/tar:removing Leading '/'From member namestar:removing Leading '/'From hard link targets[[Email protected]~]#ll/backup/10.0.0.201/ Total9528-rw-r--r--1 root root 9752749 9 16:21 etc-2018-08-09_16:21:47.tar.gz
2.4.5 Writing scripts
[[Email protected] ~] # cat/server/scripts/bak-etc-adv.sh IP ' {print $} '  -p/backup//backup/$ip/etc-' date  +%f_%t '. tar.gz   /etc/
2.4.6 Writing timed tasks
[[Email protected] ~] # crontab-lXX * * */bin/sh/server/scripts/bak-etc.sh >/dev/null 2>&100 */2 * * * SH  /server/sc ripts/bak-conf.sh >/dev/null 2>&100 XX * * * sh/server/scripts/bak-etc-adv.sh >/dev/null 2>&1
2.5 sh-x Show Script execution process
Lines starting with + indicate that the execution process is not displayed with a line startingwith +/~]#  sh-x  /server/scripts/bak-etc.sh+ + Date +%f_% T+ tar zcf/tmp/etc-2018-08-09_16:29:34.tar.gz/etc/tar:removing leading '/' from Member namestar:removing leading '/' from hard link targets

Linux timed tasks and exercises

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.