17-Linux系统定时任务

crontab服务管理

注意点

使用前先确认crontab的守护进程crond是否是打开的状态,一般是开机自启的。

[root@192 mnt]# systemctl status crond # 查看crond进程是否开启。当前是开启的
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since 六 2024-01-20 22:07:50 CST; 24 years 0 months left
 Main PID: 1072 (crond)
    Tasks: 1
   CGroup: /system.slice/crond.service
           └─1072 /usr/sbin/crond -n

1月 20 22:07:50 192.168.80.100hodoop100 systemd[1]: Started Command Scheduler.
1月 20 22:07:50 192.168.80.100hodoop100 crond[1072]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 73% if used.)
1月 20 22:07:52 192.168.80.100hodoop100 crond[1072]: (CRON) INFO (running with inotify support)
1月 01 05:05:01 192.168.80.100hodoop100 crond[1072]: (*system*) RELOAD (/etc/crontab)
1月 01 05:05:02 192.168.80.100hodoop100 crond[1072]: (*system*) RELOAD (/etc/cron.d/0hourly)
1月 01 05:05:02 192.168.80.100hodoop100 crond[1072]: (*system*) RELOAD (/etc/cron.d/raid-check)
1月 01 05:05:02 192.168.80.100hodoop100 crond[1072]: (*system*) RELOAD (/etc/cron.d/sysstat)

语法:

crontab 【选项】

选项:

-e:编辑crontab定时任务

-l:查看crontab定时任务

-r:删除当前用户所有的crontab定时任务

如何编辑定时任务

  1. 使用crontab -e进入编辑页面
  2. 格式:

*****指定的任务

 

 编写定时任务案例

# 案例一:每分钟向test.txt文件中追加一句话
[root@192 ~]# crontab -e # 进入定时任务的编辑页
# 编写内容如下:
*/1 * * * * /bin/echo "hello linux" >> /root/test.txt

 

案例演示

[root@192 ~]# crontab -l # 查看当前的定时任务
*/1 * * * * /bin/echo "hello linux" >> /root/test.txt

[root@192 ~]# crontab -r # 删除当前用户所有的定时任务
[root@192 ~]# crontab -l # 删除后再次查看
no crontab for root

 

posted @ 2024-01-21 03:41  邵杠杠  阅读(9)  评论(0编辑  收藏  举报