第二阶段 定时任务显示日期

Posted yangjuncheng0826

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第二阶段 定时任务显示日期相关的知识,希望对你有一定的参考价值。

[BEGIN] 2018/11/12 15:05:16
 
案列:每分钟显示当前系统的时间年-月 日_周和当前系统的IP地址,追加到/tmp/ip.log 中
[2018/11/12 15:09:16] [[email protected] ~]# date +%F_%w
[2018/11/12 15:09:16] 2018-11-12_1
[2018/11/12 15:09:20] [[email protected] ~]# ifconfig eth0|awk -F "[: ]+" ‘NR==2{print $4}‘
[2018/11/12 15:09:20] 10.0.0.200
[2018/11/12 15:10:40] [[email protected] ~]# vim /server/scripts/ip.sh
[2018/11/12 15:11:01] [[email protected] ~]# cat /server/scripts/ip.sh
[2018/11/12 15:11:01] #!/bin/bash
[2018/11/12 15:11:01] #desc(描述) show ip address
[2018/11/12 15:11:01] #author:yangjuncheng
[2018/11/12 15:11:01] #time:    20181121
[2018/11/12 15:11:01] date +%F_%w
[2018/11/12 15:11:15] [[email protected] ~]# sh /server/scripts/ip.sh
[2018/11/12 15:11:15] 2018-11-12_1
[2018/11/12 15:11:15] 10.0.0.200
[2018/11/12 15:13:56] [[email protected] ~]# /bin/sh /server/scripts/ip.sh
[2018/11/12 15:13:56] 2018-11-12_1
[2018/11/12 15:13:56] 10.0.0.200
[2018/11/12 15:15:18] [[email protected] ~]# #/bin/sh /server/scripts/ip.sh(/bin/sh方便操作)
2018/11/12 15:16:30] [[email protected] ~]# /bin/sh /server/scripts/ip.sh >>/tmp/ip.log
[2018/11/12 15:16:45] [[email protected] ~]# cat /tmp/ip.log
[2018/11/12 15:16:45] 2018-11-12_1
[2018/11/12 15:16:45] 10.0.0.200
[2018/11/12 15:17:35] [[email protected] ~]# #3.写到定时任务里面
[2018/11/12 15:17:35] You have new mail in /var/spool/mail/root
[2018/11/12 15:18:25] [[email protected] ~]# crontab -e
[2018/11/12 15:27:28] crontab: installing new crontab
[2018/11/12 15:27:35] [[email protected] ~]# crontab -l
[2018/11/12 15:27:35] # touch /tmp/test
[2018/11/12 15:27:35] # touch yangjuncheng is linux  student
[2018/11/12 15:27:35] 30 8 * * * go to bed
[2018/11/12 15:27:35] ##this is getup time at
[2018/11/12 15:27:35] 00 00 * * * go sleep
[2018/11/12 15:27:35] ### this is test ntp
[2018/11/12 15:27:35] */1 * * * *  /usr/sbin/ntpdate ntp1.aliyun.com    >/dev/null 2>&1
[2018/11/12 15:27:35] #this is test
[2018/11/12 15:27:35]
[2018/11/12 15:27:35] 00 23,00-07/1 * * * mkdir test
[2018/11/12 15:27:35] #this is test date scripts
[2018/11/12 15:27:35]
[2018/11/12 15:27:35] * * * * * /bin/sh /server/scripts/date.sh  >>/tmp/test.log
[2018/11/12 15:27:35] # test ce shi jiao ben
[2018/11/12 15:27:35] * * * * *  /bin/sh touch /tmp/testa.txt >/mnt/oldboy 2>&1
[2018/11/12 15:27:35] # this is print date scripts
[2018/11/12 15:27:35] * * * * *  /bin/sh /tmp/date.sh >>/mnt/date.log 2>&1
[2018/11/12 15:27:35]
[2018/11/12 15:27:35] # print date + ip file by  lee(人名) at 20181112
[2018/11/12 15:27:35]  * * * * * /bin/sh /server/scripts/ip.sh >>/tmp/ip.log         2>&1
[2018/11/12 15:27:35] #(注意先写五个*号,然后把命令原封不动的复制到计划任务里面,因为是追加到ip.log 日志里面所以后面要加2>&1)
[2018/11/12 15:27:35]  
[2018/11/12 15:28:51] [[email protected] ~]# tail -f /var/log/cron
Nov 12 15:50:01 oldboy-200 CROND[3753]: (root) CMD (/bin/sh /server/scripts/ip.sh >>/tmp/ip.log   
[2018/11/12 15:50:02] Nov 12 15:50:01 oldboy-200 CROND[3753]: (root) CMD (/bin/sh /server/scripts/ip.sh >>/tmp/ip.log         2>&1)
[2018/11/12 15:29:51] [[email protected] ~]# # 检查定时任务的日志
[2018/11/12 15:30:38] [[email protected] ~]# tail -f /tmp/ip.log
[2018/11/12 15:30:38] 2018-11-12_1
[2018/11/12 15:30:38] 10.0.0.200
[2018/11/12 15:30:38] 2018-11-12_1
2018/11/12 15:31:12] [[email protected] ~]# tail -f /tmp/ip.log
[2018/11/12 15:31:30] [[email protected] ~]# #检查文
[2018/11/12 15:36:50] [[email protected] ~]# vim /server/scripts/ip.sh
[2018/11/12 15:38:52] [[email protected] ~]# tail -f /tmp/ip.log
[2018/11/12 15:38:52] 2018-11-12_1
[2018/11/12 15:41:15] [[email protected] ~]# which ifconfig
[2018/11/12 15:41:15] /sbin/ifconfig
[2018/11/12 15:43:07] [[email protected] ~]# # 因为ifconfig /sbin下,PATH 环境变量只认识/bin /usr/bin 这两个路径
[2018/11/12 15:43:57] [[email protected] ~]# which ntpdate
[2018/11/12 15:43:57] /usr/sbin/ntpdate
[2018/11/12 15:49:42] [[email protected] ~]# # 因为ifconfig /sbin下,PATH 环境变量只认识/bin /usr/bin 这两个路径,也就是说其它路径的脚本定时任务是不认识的,解决的办法就是在在脚本里面使用绝对路径就行。
[2018/11/12 15:50:01] [[email protected] ~]# tail -f /var/log/cron
[2018/11/12 15:50:30] [[email protected] ~]# tail -f /tmp/ip.log
[2018/11/12 15:50:30] 2018-11-12_1
[2018/11/12 15:50:30] 10.0.0.200
[2018/11/12 15:50:30] 2018-11-12_1
 
 

以上是关于第二阶段 定时任务显示日期的主要内容,如果未能解决你的问题,请参考以下文章

我重构定时任务服务时,运用的那些编程思想

SpringBoot | 第二十二章:定时任务的使用

APScheduler定时任务框架

springboot定时任务,去掉指定日期

Spring 定时任务 cron 表达式

java后端开发第二篇:springboot定时任务及异步任务简记