Ubuntu18 scheduled task cron

In Linux systems, scheduled tasks are generally  cron undertaken by . We can set  cron them to start automatically when booting. cron After starting, it will read all its configuration files (global configuration file  /etc/crontab , and each user's scheduled task configuration file), and then  cron call the work tasks on time based on the command and execution time.

cron It is a scheduled execution tool under Linux that can run jobs without manual intervention. Because  cron it is a built-in service of Linux, but it does not start automatically, you can use the following methods to start and shut down the service:

/sbin/service cron start //启动服务 
/sbin/service cron stop //关闭服务  

/sbin/service cron restart //重启服务 
/sbin/service cron reload //重新载入配置

/etc/init.d This directory contains scripts for all services. You can open, close or restart the corresponding services by executing the script and adding parameters.

Commonly used corn services

Cron allows the system to perform a specified job at a specified time. We can use  crontab instructions to manage the cron mechanism.

crontab parameters:

-u:编辑其他人的crontab,如果没有加上这个参数的话就会开启自己的crontab
crontab -u 使用者名称
-l:可以列出crontab的内容
-r:可以移除crontab
-e:可以使用系统预设的编辑器,开启crontab
-i:可以移除crontab,会跳出系统信息让你再次确定是否移除crontab

Example:

*/5 * * * * 每五分钟执行
0 * * * * 每小时执行
0 0 * * * 每天执行  
0 0 * * 0 每周执行   
0 0 1 * * 每月执行      
0 0 1 1 * 每年执行

Commonly used commands:

重启cron:/etc/init.d/cron restart
查看cron:crontab -l
查看cron的状态(开启或关闭):service cron status
打开cron文件:crontab -e
查看cron日志:/var/log/syslog

crontab time format description

minute(分):可以设置0-59分
hour(小时):可以设置0-23小时
day of month(日期):可以设置1-31号
month(月份):可以设置1-12月
day of week(星期):可以设置0-7星期几,其中0和7都代表星期天,也可以使用名称来表示星期天到星期一,例如sun表示星期天,mon表示星期一
"*"代表取值范围内的数字
"/"代表"每"
"-"代表从某个数字到某个数字
","分开几个离散的数字

Create a  cron file:

cd ~
touch ihdu_crontab

Write timing commands:

The corresponding python script is called every 5 minutes to perform a dial-up connection in the intranet environment.

*/5 * * * * python3 /home/g2080/ihdu3.py

Add  cron the file to the scheduled service:

crontab ihdu_crontab

View scheduled services:

crontab -l

Start the scheduled service:

service cron start

Check the status of scheduled tasks:

service cron status

Bingo! So far it has been successful~

cron Common command reference:

For detailed  cron usage, please refer to: crontab command_Linux crontab command usage details: Submit and manage user tasks that need to be executed periodically .

# 将文件中的定时任务添加至服务中
crontab xk_wangcron 
# 查看添加至服务中的任务【需要2分钟之后才会更新状态】
crontab -l
# 修改【增加】定时任务至服务中,不通过刚刚创建xk_wangcron定时任务文件
crontab -e
# 启动这些任务
service cron start
# 若是修改定时任务内容之后,可以重启定时任务
service cron restart
# 查看定时任务的状态
service cron status
# 修改rsyslog服务,将 /etc/rsyslog.d/50-default.conf  文件中的 #cron.* 前的 # 删掉;
# 用以下命令重启rsyslog服务:
service rsyslog restart;
# 查看定时任务日志
cat /var/log/cron.log
# 查看定时任务日志的最近5个记录
tail -n 5 /var/log/cron.log

Set up a scheduled task at startup

After setting up the scheduled task, we may also want this scheduled task to be automatically started every time the machine is restarted, instead of just being limited to the current running environment:

In this way,  you can sysv-rc-conf set whether to start  cron the timer service at startup. For  sysv-rc-conf instructions, please refer to: Using sysv-rc-conf to manage services under Ubuntu_OSKernelLAB-CSDN blog .

You can see that  cron the service has been set up and started at startup.

If  cron the service is not started, execute the following code to add it to auto-start at boot:

chkconfig –level 35 crond on

Guess you like

Origin blog.csdn.net/lau_jw/article/details/126720102