ubuntu implements scheduled tasks

Regular or periodic execution of some tasks in the linux environment is usually done by the daemon process cron, which is a relatively convenient system tool that comes with the system.

Preface

crontab: is
a simple example of commands used to execute programs on a regular basis . You can understand how to create and execute your own commands . Crontab can perform various timing tasks-including file backup, script execution, file writing and so on.
Introduction

1:
Regular or periodic execution of some tasks in the linux environment is usually completed by the daemon process of cron, which is a relatively convenient system tool that comes with the system.
2: The
cron process can meet these requirements for timed tasks. With cron and shell scripts, there is no problem with very complex instructions.
3: The
crontab command is short for cron table. It is the configuration file of cron, or it can be called the job list. We can find the relevant configuration files in the following folders.

crontab scripting

After the scheduled time, there is a task that needs to be done. Here I divide it into two tasks: because we will use the yoyo.log file, it is used to store the information we output.
First create a yoyo.log file, its path is arbitrary.

$ touch yoyo.log

Note : The compilation of crontab is changed and added under /etc. If you change and add in other directories, cross-mixing will occur, resulting in unsuccessful timing! !
1: After the timing is up, output the real-time time to a log file yoyo.log

:/etc$ crontab -e

Add tasks directly under the /etc path, the next step will be like this:

# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command

After writing directly:

# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command
*/1 * * * * date >> ~/andriod/yoyo.log

The line I added means: every minute, output the real-time time to the text of ~/andriod/yoyo.log.
Then press to contr+xexit, and then save.
Restart cron: (let the newly added command take effect)

sudo service cron restart

Result : You need to cat it yourself

$ cat yoyo.log
2020年 11月 09日 星期一 18:01:01 CST
2020年 11月 09日 星期一 18:02:01 CST
2020年 11月 09日 星期一 18:03:01 CST
$

2: After the time is up, execute a script, the function of the script is to output the current time to the previous log file yoyo.log.
According to the previous method, directly display the changed result:

# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command
*/1 * * * * date >> ~/andriod/yoyo.log
*/1 * * * *  ~/andriod/shutdown.sh

The first of these two lines is added by the previous 1, and the second line is now added.
The second line: Every minute, the shutdown.sh script under this path of ~/andriod will be executed:

#!/bin/sh
echo $(date) >> ~/andriod/yoyo.log
echo "00000" >> ~/andriod/yoyo.log

Use sh parser. Each time the script is executed, the real-time time is stored on yoyo.log, and then 00000 is stored on yoyo.log.
Result : You need to cat it yourself

$ cat yoyo.log 
2020年 11月 09日 星期一 18:03:01 CST
00000
2020年 11月 09日 星期一 18:04:01 CST
00000
$

basic knowledge

The content is very succinctly finished. Maybe for some beginners, I don’t understand the rules. Let me popularize the knowledge points: the
first time you use crontab, the following prompt will appear:

root@ubuntu:# crontab -e
Select an editor.  To change later, run 'select-editor'.
  1. /bin/ed
  2. /bin/nano        <---- easiest
  3. /usr/bin/vim.basic
  4. /usr/bin/vim.tiny

This prompt is for the user to choose the editor. Normally, we can choose the third one. If you make a wrong selection, you can execute the select-editor command to select once.
Use crontab -e to enter the current user's worksheet to edit
other commands:

 crontab常用的几个命令格式
crontab -l //显示用户的crontab文件的内容
crontab -e //编辑用户的crontab文件的内容
crontab -r //删除用户的crontab文件
service cron start    //启动服务
service cron stop     //关闭服务
service cron restart  //重启服务
service cron reload   //重新载入配置
service cron status   //查看服务状态 
 
tail /var/log/syslog  //使用crontab进行设置定时任务,任务没有执行,查看系统日志排查
 
ps -ef | grep cron  //查看cron 进程是否存在

The syntax of crontab timing tasks is as follows:

minute   hour   day   month   week  user  command     #顺序:分 时 日 月 周 用户 命令
minute: 表示分钟,可以是从 0 到 59 之间的任何整数。
hour:表示小时,可以是从 0 到 23 之间的任何整数。
day:表示日期,可以是从 1 到 31 之间的任何整数。
month:表示月份,可以是从 1 到 12 之间的任何整数。
week:表示星期几,可以是从 0 到 7 之间的任何整数,这里的 0 或 7 代表星期日。
user:linux的用户身份,例如root,或者其他用户
command:要执行的命令,可以是系统命令,也可以是自己编写的脚本文件。
星号(*):代表所有可能的值,例如 month 字段如果是星号,则表示在满足其它字段的制约条件后每月都执行该命令操作。
逗号(,):可以用逗号隔开的值指定一个列表范围,例如,“1,2,5,7,8,9”
中杠(-):可以用整数之间的中杠表示一个整数范围,例如“2-6”表示“2,3,4,5,6”
正斜线(/):可以用正斜线指定时间的间隔频率,例如“0-23/2”表示每两小时执行一次。同时正斜线可以和星号一起使用,\
例如*/10,如果用在 minute 字段,表示每十分钟执行一次。

Guess you like

Origin blog.csdn.net/weixin_42271802/article/details/109582954