查看配置文件:/etc/crontablinux
[root@linux ~]# cat /etc/crontab SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root # For details see man 4 crontabs # Example of job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | # * * * * * user-name command to be executed
從配置文件中能夠看出,5個星號從左到右分別表示: 分(取值範圍:0-59) 時(取值範圍:0-23) 日(取值範圍:1-31) 月(取值範圍:1-12)或者 jan,feb,mar,apr等英文簡寫表示 星期幾(取值範圍:0-6,星期天等於0或者7)或者mon,tue,wed,thu,fri,sat,sunbash
星號(*):表明全部可能的值,例如month字段若是是星號,則表示在知足其它字段的制約條件後每個月都執行該命令操做。網絡
逗號(,):能夠用逗號隔開的值指定一個列表範圍,例如,「1,2,5,7,8,9」session
中槓(-):能夠用整數之間的中槓表示一個整數範圍,例如「2-6」表示「2,3,4,5,6」socket
正斜線(/):能夠用正斜線指定時間的間隔頻率,例如「0-23/2」表示每兩小時執行一次。同時正斜線能夠和星號一塊兒使用,例如*/10,若是用在minute字段,表示每十分鐘執行一次。rest
crontab任務以用戶來區分,對應用戶的任務計劃文件位置:/var/spool/cron/ 該目錄下,目錄名錶明用戶名,每一個用戶設定的任務計劃在各自的目錄中,如需備份直接複製該目錄中的crontab文件便可code
啓動:systemctl start crond.servicecrontab
關閉:systemctl stop crond.serviceip
重啓:systemctl restart crond.serviceci
查看狀態:systemctl status crond.service (active爲綠色即運行中,白色未運行)
-u 參數:指定用戶(查看、新增、刪除指定用戶的任務計劃)
-e 參數:編輯crontab任務計劃,如不指定用戶,默認編輯當前用戶的crontab文件
-l 參數:顯示任務計劃內容,如不指定用戶,默認顯示當前用戶的任務計劃
-r 參數:刪除某個用戶的crontab文件,如不指定用戶,默認刪除當前用戶的crontab文件
備份crontab:
1.拷貝/var/spool/cron/目錄下的文件
2.crontab -l > /home/cronbak
恢復:crontab /home/cronbak
實例1:每1分鐘執行一次command
* * * * * command
實例2:每小時的第1和第30分鐘執行
1,30 * * * * command
實例3:在上午7點到10點的第1和第30分鐘執行
1,30 8-11 * * * command
實例4:每隔兩天的上午7點到10點的第1和第30分鐘執行
1,30 7-10 */2 * * command
實例5:每一個星期一的上午7點到10點的第1和第30分鐘執行
1,30 8-11 * * 1 command
實例6:每晚的22:30重啓firewalld
30 22 * * * /usr/bin/systemctl restart firewlld.service
實例7:每個月一、十、25日的5 : 30重啓firewalld
30 5 1,10,22 * * /usr/bin/systemctl restart firewlld.service
實例8:每週6、週日的2 : 30重啓firewalld
30 2 * * 6,0 /usr/bin/systemctl restart firewlld.service
實例9:天天19:00至23:00之間每隔20分鐘重啓一次firewalld
0,20,40 19-23 * * * /usr/bin/systemctl restart firewlld.service
實例10:每星期六的晚上11 : 00 pm重啓firewalld
0 23 * * 6 /usr/bin/systemctl restart firewlld.service
實例11:每小時重啓一次firewalld
* */1 * * * /usr/bin/systemctl restart firewlld.service
實例12:晚上10點到早上8點之間,每隔一小時重啓firewalld
* 22-8/1 * * * /usr/bin/systemctl restart firewlld.service
實例13:每個月的4號與每週一到週三的11點重啓firewalld
30 2 5 * mon-wed /usr/bin/systemctl restart firewlld.service
實例14:一月一號的5點重啓firewalld
0 5 1 jan * /usr/bin/systemctl restart firewlld.service
實例15:五月每隔7天重啓firewalld * * */7 5 * /usr/bin/systemctl restart firewlld.service
查看系統服務:
[root@linux ~]# chkconfig --list 注:該輸出結果只顯示 SysV 服務,並不包含 原生 systemd 服務。SysV 配置數據 可能被原生 systemd 配置覆蓋。 要列出 systemd 服務,請執行 'systemctl list-unit-files'。 查看在具體 target 啓用的服務請執行 'systemctl list-dependencies [target]'。 netconsole 0:關 1:關 2:關 3:關 4:關 5:關 6:關 network 0:關 1:關 2:開 3:開 4:開 5:開 6:關
系統顯示了netconsole和network兩個服務,對應的值表示各個運行級別下的服務開關狀態,若是是開,該服務將開機啓動,服務腳本路徑在/etc/init.d/下:
[root@linux ~]# ls /etc/init.d/ functions netconsole network README
運行級別: runlevel命令查看
0:表示關機狀態 1:表示單用戶模式 2:表示多用戶模式(與3級別相比沒有NFS服務) 3:多用戶模式 4:未使用,系統保留 5:圖形界面的多用戶模式 6:表示重啓
開啓服務:
[root@linux ~]# chkconfig network on
關閉服務:
[root@linux ~]# chkconfig network off
指定運行級別下關閉或開啓操做:
[root@linux ~]# chkconfig --level 345 network off [root@linux ~]# chkconfig --list network 0:關 1:關 2:開 3:關 4:關 5:關 6:關
--level :指定操做的運行級別,示例中指定3,4,5級別下關閉
添加自定義腳本到chkconfig管理:
1.先將腳本放入目錄 /etc/init.d/中
[root@linux ~]# cp /etc/init.d/network /etc/init.d/test [root@linux ~]# ls /etc/init.d/ functions netconsole network README test
2.添加服務:
[root@linux ~]# chkconfig --add test [root@linux ~]# chkconfig --list test 0:關 1:關 2:開 3:開 4:開 5:開 6:關
3.刪除服務:
[root@linux ~]# chkconfig --del test
在Centos7如下版本,主要使用chkconfig管理系統服務,Centos7中主要使用systemd管理,Centos7也保留了chkconfig命令
列出系統服務:
[root@linux ~]# systemctl list-unit-files
該命令會列出全部系統服務,以及服務的狀態(enabled、disabled、static:表示該服務與其餘服務關聯,不能單獨設置狀態,可能須要等待其餘服務喚醒)
只查看service服務:
[root@linux ~]# systemctl list-units --all --type=service
不加- -all 只列出在運行的(active)服務,未運行(inactive)的服務不會顯示
啓動服務:
[root@linux ~]# systemctl restart crond.service
關閉服務:
[root@linux ~]# systemctl stop crond.service
重啓服務:
[root@linux ~]# systemctl restart crond.service
查看服務狀態:
[root@linux ~]# systemctl status crond.service
開機禁止啓動:
[root@linux ~]# systemctl disable crond.service
開機啓動:
[root@linux ~]# systemctl enable crond.service
查看服務是否開機啓動:
[root@linux ~]# systemctl is-enabled crond.service enabled
unit:
服務文件路徑:
root@linux ~]# ls /usr/lib/systemd/system
該目錄中包含系統全部服務的文件,將這些服務文件稱爲unit,unit分爲如下類型: service 系統服務 target 多個unit組成的組 device 硬件設備 socket 網絡通訊的套接字 swap sawp文件 等等……
顯示正在運行的unit:
[root@linux ~]# systemctl list-units
- -all 顯示全部,包括inactive的unit
顯示狀態爲inactive的unit;
[root@linux ~]# systemctl list-units --all --state=inactive
顯示狀態爲active而且類型爲service的unit:
[root@linux ~]# systemctl list-units --type=service
查看某個服務是否爲active:
[root@linux ~]# systemctl is-active crond active
target:
target中包含多個unit,能夠理解爲用於管理unit,一個service屬於一種類型的unit,多個unit組成了一個target
查看系統中的target:
[root@linux ~]# systemctl list-unit-files --type=target
查看指定的target中包含了哪些unit:
[root@linux ~]# systemctl list-dependencies multi-user.target
target中還能夠包含其餘target
查看系統默認的target:
[root@linux ~]# systemctl get-default multi-user.target
設置系統默認的target:
[root@linux ~]# systemctl set-default reboot.target
若是將reboot.target設爲默認target,那麼系統開機後會一直重啓
查看指定unit屬於哪一個target:(服務文件中的 [Install] 項)
[root@linux ~]# cat /usr/lib/systemd/system/crond.service [Unit] Description=Command Scheduler After=auditd.service systemd-user-sessions.service time-sync.target [Service] EnvironmentFile=/etc/sysconfig/crond ExecStart=/usr/sbin/crond -n $CRONDARGS ExecReload=/bin/kill -HUP $MAINPID KillMode=process [Install] WantedBy=multi-user.target #屬於mulit-user.target