1. 程式人生 > >第二階段 定時任務顯示日期

第二階段 定時任務顯示日期

[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