Timed and delayed tasks in Linux

Source: Internet
Author: User

How to make the system scheduled to complete the task, how to let the system over a period of time to complete a task autonomously, the following will be detailed introduction of the system timing and delay.

# # # #一, at command # # #

    1. Basic usage of AT command

      At-l # #列出延迟任务

      At-r Task ID # #删除此id任务

      AT-M # #让无输出的命令不产生邮件

      At-c task ID # #查看此id deferred task execution action

At-f File # #用文件内容记录的命令作为延迟任务的执行命令

At timed tasks, perform position monitoring with watch-n 1 tasks

650) this.width=650; "Src=" Https://s4.51cto.com/oss/201711/01/01d01d4b206db3af3f97e1c28b122914.png-wh_500x0-wm_3 -wmp_4-s_3144228851.png "style=" Float:none; "title=" 120.png "alt=" 01d01d4b206db3af3f97e1c28b122914.png-wh_ "/>

650) this.width=650; "Src=" Https://s1.51cto.com/oss/201711/01/1822e93866462899c3110a3bfc15c7dc.png-wh_500x0-wm_3 -wmp_4-s_3410145242.png "style=" Float:none; "title=" 121.png "alt=" 1822e93866462899c3110a3bfc15c7dc.png-wh_ "/>

650) this.width=650; "Src=" Https://s5.51cto.com/oss/201711/01/ddb41f45adebdaaf3e565e2792571e32.png-wh_500x0-wm_3 -wmp_4-s_2301742258.png "style=" Float:none; "title=" 122.png "alt=" Ddb41f45adebdaaf3e565e2792571e32.png-wh_ "/>

Execute touch/mnt/file{1..5} After one minute, press CTRL D to end

650) this.width=650; "Src=" Https://s1.51cto.com/oss/201711/01/4a60bc2061b7f428d5b9ff4e2de84c1a.png-wh_500x0-wm_3 -wmp_4-s_4041860383.png "style=" Float:none; "title=" 123.png "alt=" 4a60bc2061b7f428d5b9ff4e2de84c1a.png-wh_ "/>

View the task ID that will be executed

650) this.width=650; "Src=" Https://s1.51cto.com/oss/201711/01/d64e222a2e4dce32f77e0f8727028b8a.png-wh_500x0-wm_3 -wmp_4-s_1572447316.png "style=" Float:none; "title=" 124.png "alt=" D64e222a2e4dce32f77e0f8727028b8a.png-wh_ "/>

Delete the task ID that will be executed, terminate task execution, ATQ task ID

650) this.width=650; "Src=" Https://s1.51cto.com/oss/201711/01/281e819bf8696c3765e314a865124558.png-wh_500x0-wm_3 -wmp_4-s_835462997.png "style=" Float:none; "title=" 125.png "alt=" 281e819bf8696c3765e314a865124558.png-wh_ "/>

Emptying logs >/var/spool/mail/root MAILQ

650) this.width=650; "Src=" Https://s1.51cto.com/oss/201711/01/b49ba37aeaeaad895b3ef8642f6df472.png-wh_500x0-wm_3 -wmp_4-s_829272962.png "style=" Float:none; "title=" 126.png "alt=" B49ba37aeaeaad895b3ef8642f6df472.png-wh_ "/>

650) this.width=650; "Src=" Https://s4.51cto.com/oss/201711/01/23406bc1ba4ead38bb0519a539ee43ba.png-wh_500x0-wm_3 -wmp_4-s_3089409856.png "style=" Float:none; "title=" 127.png "alt=" 23406bc1ba4ead38bb0519a539ee43ba.png-wh_ "/>

AT-M does not generate mail

650) this.width=650; "Src=" Https://s5.51cto.com/oss/201711/01/0b671d79b6a850e1e4892a595bf0d583.png-wh_500x0-wm_3 -wmp_4-s_3516844553.png "style=" Float:none; "title=" 128.png "alt=" 0b671d79b6a850e1e4892a595bf0d583.png-wh_ "/>

650) this.width=650; "Src=" Https://s5.51cto.com/oss/201711/01/c00761e338266b91beba5c7358b26928.png-wh_500x0-wm_3 -wmp_4-s_1045876762.png "style=" Float:none; "title=" 129.png "alt=" C00761e338266b91beba5c7358b26928.png-wh_ "/>

650) this.width=650; "Src=" Https://s5.51cto.com/oss/201711/01/1d668c01bb594760790a4ec83896bf2d.png-wh_500x0-wm_3 -wmp_4-s_87391745.png "style=" Float:none; "title=" 130.png "alt=" 1d668c01bb594760790a4ec83896bf2d.png-wh_ "/>

Editing files with student users

650) this.width=650; "Src=" Https://s4.51cto.com/oss/201711/01/92bb8ae0141d5564e5664707d9830da2.png-wh_500x0-wm_3 -wmp_4-s_991819725.png "style=" Float:none; "title=" 131.png "alt=" 92bb8ae0141d5564e5664707d9830da2.png-wh_ "/>

650) this.width=650; "Src=" Https://s4.51cto.com/oss/201711/01/92051310cbe3715314ceada745cbd915.png-wh_500x0-wm_3 -wmp_4-s_4058751001.png "style=" Float:none; "title=" 132.png "alt=" 92051310cbe3715314ceada745cbd915.png-wh_ "/>

Use the file content as a self-delay task

650) this.width=650; "Src=" Https://s4.51cto.com/oss/201711/01/441c4ab6f0c66524a057326cbc45f41d.png-wh_500x0-wm_3 -wmp_4-s_2811465172.png "style=" Float:none; "title=" 133.png "alt=" 441c4ab6f0c66524a057326cbc45f41d.png-wh_ "/>

650) this.width=650; "Src=" Https://s4.51cto.com/oss/201711/01/80d6557c1c5adf39558c7388983177cc.png-wh_500x0-wm_3 -wmp_4-s_3512143289.png "style=" Float:none; "title=" 134.png "alt=" 80d6557c1c5adf39558c7388983177cc.png-wh_ "/>

650) this.width=650; "Src=" Https://s4.51cto.com/oss/201711/01/306fd1e7a61b53dbd752d611afa754b5.png-wh_500x0-wm_3 -wmp_4-s_2318011718.png "style=" Float:none; "title=" 135.png "alt=" 306fd1e7a61b53dbd752d611afa754b5.png-wh_ "/>

650) this.width=650; "Src=" Https://s2.51cto.com/oss/201711/01/afda4328e3da8b3dfa6ed44866d95328.png-wh_500x0-wm_3 -wmp_4-s_2403171264.png "style=" Float:none; "title=" 136.png "alt=" Afda4328e3da8b3dfa6ed44866d95328.png-wh_ "/>

View deferred task ID at-l

650) this.width=650; "Src=" Https://s2.51cto.com/oss/201711/01/ee6c3b1613868dfd21c38e0fdfb34d7a.png-wh_500x0-wm_3 -wmp_4-s_1894529684.png "style=" Float:none; "title=" 137.png "alt=" Ee6c3b1613868dfd21c38e0fdfb34d7a.png-wh_ "/>

View Action at-c task ID for deferred task execution

650) this.width=650; "Src=" Https://s2.51cto.com/oss/201711/01/940b6ee251f3e22ed72768852bccdf94.png-wh_500x0-wm_3 -wmp_4-s_514773174.png "style=" Float:none; "title=" 138.png "alt=" 940b6ee251f3e22ed72768852bccdf94.png-wh_ "/>

Use Monitoring for 2.at commands

All users in the default system have the power to execute at command so how to set the black and white list at command

# #用户黑户单

By default, there is no black-and-white list in the system, and the path to this list

/etc/at.deny # #用户黑名单, this list is empty by default and will take effect when the whitelist does not exist

By default, student users can use the AT command

650) this.width=650; "Src=" Https://s5.51cto.com/oss/201711/01/83f79f197f98ffeb00075748635d14b4.png-wh_500x0-wm_3 -wmp_4-s_1787538128.png "style=" Float:none; "title=" 139.png "alt=" 83f79f197f98ffeb00075748635d14b4.png-wh_ "/>

Prohibit student user Login in blacklist

650) this.width=650; "Src=" Https://s3.51cto.com/oss/201711/01/21bb6624fbb941e990e557783093facd.png-wh_500x0-wm_3 -wmp_4-s_1858055412.png "style=" Float:none; "title=" 140.png "alt=" 21bb6624fbb941e990e557783093facd.png-wh_ "/>

650) this.width=650; "Src=" Https://s3.51cto.com/oss/201711/01/950fd372a9ef93c8e655c232f021a56b.png-wh_500x0-wm_3 -wmp_4-s_2435508144.png "style=" Float:none; "title=" 141.png "alt=" 950fd372a9ef93c8e655c232f021a56b.png-wh_ "/>

Student user is forbidden to use the AT command

650) this.width=650; "Src=" Https://s1.51cto.com/oss/201711/01/55c91e67f3e0ef689f46e208ee0c2dfe.png-wh_500x0-wm_3 -wmp_4-s_3620824829.png "style=" Float:none; "title=" 142.png "alt=" 55c91e67f3e0ef689f46e208ee0c2dfe.png-wh_ "/>

White List of 3.at commands

By default, the system does not exist for the at whitelist user.

But when the whitelist appears, the AT command will be used by the user and the root user in the list

Other users cannot execute the AT command

Add student users to whitelist

650) this.width=650; "Src=" Https://s4.51cto.com/oss/201711/01/a829a5d40cb1ef42e9a9891aab160013.png-wh_500x0-wm_3 -wmp_4-s_3181623829.png "style=" Float:none; "title=" 143.png "alt=" A829a5d40cb1ef42e9a9891aab160013.png-wh_ "/>

650) this.width=650; "Src=" Https://s4.51cto.com/oss/201711/01/e4ef274c31f00e284fcc4b63653162ff.png-wh_500x0-wm_3 -wmp_4-s_3864836169.png "style=" Float:none; "title=" 144.png "alt=" E4ef274c31f00e284fcc4b63653162ff.png-wh_ "/>

The root user can use the AT command

650) this.width=650; "Src=" Https://s4.51cto.com/oss/201711/01/bc785e50a4a11fa76b14f6c2b866a6fc.png-wh_500x0-wm_3 -wmp_4-s_2318736322.png "style=" Float:none; "title=" 146.png "alt=" Bc785e50a4a11fa76b14f6c2b866a6fc.png-wh_ "/>

Student users can use the AT command

650) this.width=650; "Src=" Https://s4.51cto.com/oss/201711/01/6264676a6a95fa628034c955a920834e.png-wh_500x0-wm_3 -wmp_4-s_1637929143.png "style=" Float:none; "title=" 147.png "alt=" 6264676a6a95fa628034c955a920834e.png-wh_ "/>

Class user cannot use the AT command

650) this.width=650; "Src=" Https://s2.51cto.com/oss/201711/01/ca15802dd0d259f75932d10429ba914c.png-wh_500x0-wm_3 -wmp_4-s_2179683930.png "style=" Float:none; "title=" 148.png "alt=" Ca15802dd0d259f75932d10429ba914c.png-wh_ "/>

# # # #二, cron####

Perform a specific task at an agreed time

    1. This program is provided by the Crond.service service

    2. The time to write the program is permanently saved

      # #设定方式

      CRONTAB-E-u user name (can not be written)

      Minute hour day month Week event

      7 * * * rm-fr/mnt/* # #每天7:30 Delete all contents of/mnt/

      7 3,16 * * rm-fr/mnt/* # #每月的3号和16号7:30 Delete all contents of/mnt/

      7 3-16 * * rm-fr/mnt/* # #每月的3号到16号7:30 Delete all contents of/mnt/

      7 3,16 2 3 rm-fr/mnt/* # #2月的3号和16号以及二月的每个周三的7:30 Delete all contents of/mnt/

      */30 * * * 1-5 rm-fr/mnt/* # #周一到周五每隔30分钟的7:30 Delete all contents of/mnt/

Start Service Systenctl start Crond.service

650) this.width=650; "Src=" Https://s2.51cto.com/oss/201711/01/1c10f521c0345bc8605e9fa30d9f764b.png-wh_500x0-wm_3 -wmp_4-s_1476718884.png "style=" Float:none; "title=" 149.png "alt=" 1c10f521c0345bc8605e9fa30d9f764b.png-wh_ "/>

Write Program Crontab-e

650) this.width=650; "Src=" Https://s2.51cto.com/oss/201711/01/33040ae34e9baffaf2b6fec51faebf2e.png-wh_500x0-wm_3 -wmp_4-s_2652716705.png "style=" Float:none; "title=" 150.png "alt=" 33040ae34e9baffaf2b6fec51faebf2e.png-wh_ "/>

650) this.width=650; "Src=" Https://s1.51cto.com/oss/201711/01/c74f2a833bc659aa00c47433458cc7df.png-wh_500x0-wm_3 -wmp_4-s_1000677238.png "style=" Float:none; "title=" 151.png "alt=" C74f2a833bc659aa00c47433458cc7df.png-wh_ "/>

Crontab-l # #列出当前用户的cron任务

crontab-r# #删除当前crontab任务

/var/spool/cron/* # #cront任务记录文件存放的位置

650) this.width=650; "Src=" Https://s1.51cto.com/oss/201711/01/917bdc9f81ed52d6eeccbe168c0b5f95.png-wh_500x0-wm_3 -wmp_4-s_4143755999.png "style=" Float:none; "title=" 152.png "alt=" 917bdc9f81ed52d6eeccbe168c0b5f95.png-wh_ "/>

can also be viewed according to the storage location

650) this.width=650; "Src=" Https://s1.51cto.com/oss/201711/01/ed5f797d323bacec1a19358d8a6807ca.png-wh_500x0-wm_3 -wmp_4-s_1584868540.png "style=" Float:none; "title=" 153.png "alt=" Ed5f797d323bacec1a19358d8a6807ca.png-wh_ "/>

Monitoring Watch-n 1 ' ls-l/mnt/'

650) this.width=650; "Src=" Https://s1.51cto.com/oss/201711/01/ae8e6c0f5819641708de11aa18237117.png-wh_500x0-wm_3 -wmp_4-s_2461162901.png "style=" Float:none; "title=" 154.png "alt=" Ae8e6c0f5819641708de11aa18237117.png-wh_ "/>

Black and white list of 2.cron

By default the system only has blacklist, when whitelist is established, blacklist expires

# #黑名单

/etc/cron.deny # #在此名单中的用户不能执行crontab

Class User Write Blacklist

650) this.width=650; "Src=" Https://s1.51cto.com/oss/201711/01/679b361b37560aa0940cf2a44f594376.png-wh_500x0-wm_3 -wmp_4-s_3742084155.png "style=" Float:none; "title=" 155.png "alt=" 679b361b37560aa0940cf2a44f594376.png-wh_ "/>

650) this.width=650; "Src=" Https://s3.51cto.com/oss/201711/01/fc70db4e62176dfb5607b6b4f7bdc459.png-wh_500x0-wm_3 -wmp_4-s_3453940279.png "style=" Float:none; "title=" 156.png "alt=" Fc70db4e62176dfb5607b6b4f7bdc459.png-wh_ "/>

Class cannot execute crontab

650) this.width=650; "Src=" Https://s3.51cto.com/oss/201711/01/f90b96be8381599052b9db578a6b6c89.png-wh_500x0-wm_3 -wmp_4-s_3562666989.png "style=" Float:none; "title=" 157.png "alt=" F90b96be8381599052b9db578a6b6c89.png-wh_ "/>

Student user can perform

650) this.width=650; "Src=" Https://s3.51cto.com/oss/201711/01/37f38509d8dcbb6e9115603c91b050fb.png-wh_500x0-wm_3 -wmp_4-s_1230067814.png "style=" Float:none; "title=" 158.png "alt=" 37f38509d8dcbb6e9115603c91b050fb.png-wh_ "/>

650) this.width=650; "Src=" Https://s3.51cto.com/oss/201711/01/79a42b6b2c85a3cbe96ec1f46994e343.png-wh_500x0-wm_3 -wmp_4-s_246567319.png "style=" Float:none; "title=" 159.png "alt=" 79a42b6b2c85a3cbe96ec1f46994e343.png-wh_ "/>

# #白名单

/etc/cron.allow # #用户白名单, when this list appears, other users except the root user and the list cannot use the crontab command

Write student in white list

650) this.width=650; "Src=" Https://s2.51cto.com/oss/201711/01/1add7e4eba9b3dcdbc3017d57287a115.png-wh_500x0-wm_3 -wmp_4-s_1890083258.png "style=" Float:none; "title=" 166.png "alt=" 1add7e4eba9b3dcdbc3017d57287a115.png-wh_ "/>

650) this.width=650; "Src=" Https://s5.51cto.com/oss/201711/01/f21c3f71d4479bbaa58ce1e96ee36ceb.png-wh_500x0-wm_3 -wmp_4-s_1634818290.png "style=" Float:none; "title=" 167.png "alt=" F21c3f71d4479bbaa58ce1e96ee36ceb.png-wh_ "/>

Student users can use crontab

650) this.width=650; "Src=" Https://s5.51cto.com/oss/201711/01/ed638ecb3c51bb5d2fb30a39e31068ee.png-wh_500x0-wm_3 -wmp_4-s_4138022862.png "style=" Float:none; "title=" 168.png "alt=" Ed638ecb3c51bb5d2fb30a39e31068ee.png-wh_ "/>

650) this.width=650; "Src=" Https://s5.51cto.com/oss/201711/01/8d9745375b116ab00cfedcc69bb8a97a.png-wh_500x0-wm_3 -wmp_4-s_4135592937.png "style=" Float:none; "title=" 169.png "alt=" 8d9745375b116ab00cfedcc69bb8a97a.png-wh_ "/>

Class user cannot use the crontab command

650) this.width=650; "Src=" Https://s5.51cto.com/oss/201711/01/87306571719c2faf5cc18a4752911172.png-wh_500x0-wm_3 -wmp_4-s_2649483571.png "style=" Float:none; "title=" 170.png "alt=" 87306571719c2faf5cc18a4752911172.png-wh_ "/>

3. File Mode definition Crontab

vim/etc/spool/cron/User Name # #以某个用户身份执行cron, the file name must be the same as the user name

Time-of-day month-week action

vim/etc/cron.d/File # #设定多个用户身份执行cron

< > < > < day > < month > < week > < user > < action >

Perform an action on/mnt/, add executable permission to/mnt/

650) this.width=650; "Src=" Https://s5.51cto.com/oss/201711/01/ca3181c8c47b079f9c9c712d98805598.png-wh_500x0-wm_3 -wmp_4-s_4019868033.png "style=" Float:none; "title=" 171.png "alt=" Ca3181c8c47b079f9c9c712d98805598.png-wh_ "/>

650) this.width=650; "Src=" Https://s2.51cto.com/oss/201711/01/7fac6523fa5290adf4e77348c1134459.png-wh_500x0-wm_3 -wmp_4-s_2966943603.png "style=" Float:none; "title=" 172.png "alt=" 7fac6523fa5290adf4e77348c1134459.png-wh_ "/>

Perform cron with student users

650) this.width=650; "Src=" Https://s5.51cto.com/oss/201711/01/6b2e662cac32b3c956b73f33cdf447df.png-wh_500x0-wm_3 -wmp_4-s_2507021387.png "style=" Float:none; "title=" 173.png "alt=" 6b2e662cac32b3c956b73f33cdf447df.png-wh_ "/>

650) this.width=650; "Src=" Https://s2.51cto.com/oss/201711/01/87bd60fa54b82834097e00f83a04b10a.png-wh_500x0-wm_3 -wmp_4-s_1399519148.png "style=" Float:none; "title=" 174.png "alt=" 87bd60fa54b82834097e00f83a04b10a.png-wh_ "/>

Watch-n 1 ' ls-l/mnt/'

650) this.width=650; "Src=" Https://s5.51cto.com/oss/201711/01/b912417f0aa14fb3b0b1f210a6b7d05e.png-wh_500x0-wm_3 -wmp_4-s_3784975927.png "style=" Float:none; "title=" 175.png "alt=" B912417f0aa14fb3b0b1f210a6b7d05e.png-wh_ "/>

650) this.width=650; "Src=" Https://s5.51cto.com/oss/201711/01/da03d5a3dc3e0d5e20d45b49191d23cd.png-wh_500x0-wm_3 -wmp_4-s_569285847.png "style=" Float:none; "title=" 176.png "alt=" Da03d5a3dc3e0d5e20d45b49191d23cd.png-wh_ "/>

Import in Append mode

650) this.width=650; "Src=" Https://s2.51cto.com/oss/201711/01/6ac100f128c01380a8c9e156c31fe878.png-wh_500x0-wm_3 -wmp_4-s_4224837178.png "style=" Float:none; "title=" 177.png "alt=" 6ac100f128c01380a8c9e156c31fe878.png-wh_ "/>

650) this.width=650; "Src=" Https://s2.51cto.com/oss/201711/01/55ef14c4271828693f9c22c2d36878f6.png-wh_500x0-wm_3 -wmp_4-s_2125063969.png "style=" Float:none; "title=" 178.png "alt=" 55ef14c4271828693f9c22c2d36878f6.png-wh_ "/>

Execute cron vim cron.sh as a script

650) this.width=650; "Src=" Https://s3.51cto.com/oss/201711/01/23f67487457068acfe538360b006d6d5.png-wh_500x0-wm_3 -wmp_4-s_3666438595.png "style=" Float:none; "title=" 179.png "alt=" 23f67487457068acfe538360b006d6d5.png-wh_ "/>

650) this.width=650; "Src=" Https://s2.51cto.com/oss/201711/01/5ed45144b7c9b6ca3dcadf3ca0adf257.png-wh_500x0-wm_3 -wmp_4-s_1339783992.png "style=" Float:none; "title=" 180.png "alt=" 5ed45144b7c9b6ca3dcadf3ca0adf257.png-wh_ "/>

Run the script

650) this.width=650; "Src=" Https://s3.51cto.com/oss/201711/01/2ffcf0e036450e770cd545faf242c7b5.png-wh_500x0-wm_3 -wmp_4-s_93588375.png "style=" Float:none; "title=" 181.png "alt=" 2ffcf0e036450e770cd545faf242c7b5.png-wh_ "/>

Monitoring available, script command execution successful


650) this.width=650; "Src=" Https://s2.51cto.com/oss/201711/01/5ef79d2db3a932144e350bb7c4fa4678.png-wh_500x0-wm_3 -wmp_4-s_2886051830.png "style=" Float:none; "title=" 182.png "alt=" 5ef79d2db3a932144e350bb7c4fa4678.png-wh_ "/>

/etc/cron.d/files, there are some management to put our monthly, weekly, daily, hourly files to be executed in separate files to execute

650) this.width=650; "Src=" Https://s5.51cto.com/oss/201711/01/17bc919f97a5bf0c7c183d238460ee1c.png-wh_500x0-wm_3 -wmp_4-s_3157674449.png "style=" Float:none; "title=" 183.png "alt=" 17bc919f97a5bf0c7c183d238460ee1c.png-wh_ "/>



Timed and delayed tasks in Linux

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.