任務計劃crontab、服務管理(chkconfig、systemd)
crontab計劃任務文件
任務計劃文件路徑/var/spool/cron/
cat /etc/crontab
[root@shu-test ~]# 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 [root@shu-test ~]#
格式:分鐘 小時 日 月 周 user command
範圍:
分鐘:0-59;
小時:0-23;
日:0-31;
月:0-12;
周:0-6,0為周日、或7為周日;可用格式:
*:表示所有;
1-5:表示範圍為1到5;
1,2,3:表示1 2 3;*
/2:表示被2整除的數字,比如小時,那就是沒隔2小時;**
舉例:
0 3 1-10 */2 2,5 /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log
0分 3點 1到10日 偶數月份 星期2或星期5 使用/bin/bash的內核shell 執行腳本123.sh無論正確錯誤都將輸入123.log日誌;
當偶數月份的1到10日是星期2或星期5的時候,那麽就在淩晨3:00執行後面這個腳本;
新建或編輯模式:
crontab -e
查看內容:
crontab -l
[root@shu-test ~]# crontab -l
0 3 1-10 */2 2,5 /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log
[root@shu-test ~]#
cat /var/spool/cron/root
[root@shu-test ~]# cat /var/spool/cron/root 1 10 * 2 * /usr/bin/find /tmp/ -type f -mtime +100 |xargs rm -f [root@shu-test ~]#
啟動服務:
systemctl start crond
查詢服務是否啟動:
ps aux |grep crond
[root@shu-test ~]# ps aux |grep crond
root 543 0.0 0.1 126268 1664 ? Ss 14:47 0:00 /usr/sbin/crond -n
root 1372 0.0 0.0 112676 980 pts/0 R+ 16:34 0:00 grep --color=auto crond
[root@shu-test ~]#
或者使用命令
systemctl status crond
顯示結果: Active: active (running)
[root@shu-test ~]# systemctl status crond
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since 一 2018-01-29 14:47:53 CST; 1h 47min ago
Main PID: 543 (crond)
CGroup: /system.slice/crond.service
└─543 /usr/sbin/crond -n
1月 29 14:47:53 shu-test systemd[1]: Started Command Scheduler.
1月 29 14:47:53 shu-test systemd[1]: Starting Command Scheduler...
1月 29 14:47:53 shu-test crond[543]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 29% if used.)
1月 29 14:47:53 shu-test crond[543]: (CRON) INFO (running with inotify support)
1月 29 16:33:01 shu-test crond[543]: (root) RELOAD (/var/spool/cron/root)
[root@shu-test ~]#
服務管理-chkconfig工具
CentOS 6上的服務管理工具為chkconfig,Linux系統所有的預設置服務都可以通過查看/etc/init.d目錄得到;
[root@shu-test ~]# ls /etc/init.d
functions netconsole network README
[root@shu-test ~]#
查看服務
chkconfig --list
[root@shu-test ~]# 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:關
[root@shu-test ~]#
級別(0~6):為系統啟動級別,0、1、6被系統保留。可以參照init命令;
0:為shutdown動作;
1:作為重啟到單用戶模式;
6:為重啟;
2:無NFS支持的多用戶模式;
3:完全多用戶模式;
4:自定義級別;
5:圖形界面;
關閉級別:
chkconfig --level 3 network off
關閉network的3級別
[root@shu-test ~]# chkconfig --level 3 network off
[root@shu-test ~]# 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:關
[root@shu-test ~]#
開啟級別:
chkconfig --level 3 network on
開啟network服務的3級別
[root@shu-test ~]# chkconfig --level 3 network on
[root@shu-test ~]# 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:關
[root@shu-test ~]#
增加自定義服務
chkconfig --add 123
將定義好的服務123添加到啟動服務中,使用vim就行編輯123文件;
[root@shu-test ~]# cd /etc/init.d/
[root@shu-test init.d]# ls
functions netconsole network README
[root@shu-test init.d]# cp network 123
[root@shu-test init.d]# ls
123 functions netconsole network README
[root@shu-test init.d]#
[root@shu-test init.d]# chkconfig --add 123
[root@shu-test init.d]# chkconfig --list
註:該輸出結果只顯示 SysV 服務,並不包含
原生 systemd 服務。SysV 配置數據
可能被原生 systemd 配置覆蓋。
要列出 systemd 服務,請執行 ‘systemctl list-unit-files‘。
查看在具體 target 啟用的服務請執行
‘systemctl list-dependencies [target]‘。
123 0:關 1:關 2:開 3:開 4:開 5:開 6:關
netconsole 0:關 1:關 2:關 3:關 4:關 5:關 6:關
network 0:關 1:關 2:開 3:開 4:開 5:開 6:關
[root@shu-test init.d]#
刪除自定義服務
chkconfig --del 123
刪除123服務
[root@shu-test init.d]# chkconfig --del 123
[root@shu-test init.d]# 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:關
[root@shu-test init.d]#
systemd管理服務
用於CentOS 7系統下、CentOS 6系統常用chkconfig工具;
列出系統所有的服務
systemctl list-units --all --type=service
讓服務開機啟動
systemctl enable crond.service
不讓服務開機啟動
systemctl disable crond
查看狀態
systemctl status crond
[root@shu-test init.d]# systemctl status crond
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since 一 2018-01-29 16:37:09 CST; 3h 54min ago
Main PID: 1427 (crond)
CGroup: /system.slice/crond.service
└─1427 /usr/sbin/crond -n
1月 29 16:37:09 shu-test systemd[1]: Started Command Scheduler.
1月 29 16:37:09 shu-test systemd[1]: Starting Command Scheduler...
1月 29 16:37:09 shu-test crond[1427]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 8% if used.)
1月 29 16:37:09 shu-test crond[1427]: (CRON) INFO (running with inotify support)
1月 29 16:37:09 shu-test crond[1427]: (CRON) INFO (@reboot jobs will be run at computer‘s startup.)
1月 29 16:47:01 shu-test crond[1427]: (root) RELOAD (/var/spool/cron/root)
[root@shu-test init.d]#
停止服務
systemctl stop crond
啟動服務
systemctl start crond
重啟服務
systemctl restart crond
檢查服務是否設置開機啟動
systemctl is-enabled crond
[root@shu-test init.d]# systemctl is-enabled crond
enabled
[root@shu-test init.d]#
ls /usr/lib/systemd/system
service:系統服務;
target:多個unit組成的組;
device:硬件設備;
mount:系統掛載點;
automount:自動掛載點;
path:文件或路徑;
scope:不是由systemd啟動的外部進程;
slice:進程組;
snapshot:systemd快照;
socket:進程間通信套接字;
swap:swap文件;
timer:定時器;
unit相關
列出正在運行的unit
systemctl list-units
列出所有的unit
systemctl list-units --all
列出所有inactive的unit
systemctl list-units --all --state=inactive
列出所有狀態的service
systemctl list-units --all --type=service
列出狀態為active的service
systemctl list-units --type=service
查看某個unit是否active
systemctl is-active crond.service
target
target類似於centos6裏面啟動級別,target支持多個target同時啟動。target其實是多個unit的組合;
查看當前系統的所有target
systemctl list-unit-files --type=target
查看一個target包含的所有unit
systemctl list-dependencies multi-user.target
查看系統默認的target
systemctl get-default
[root@shu-test init.d]# systemctl get-default
multi-user.target
[root@shu-test init.d]#
設置默認的target
systemctl set-default multi-user.target
一個service屬於一種類型的unit
多個unit組成一個target
一個target裏面包含了多個service
查看服務文件
[root@shu-test init.d]# cat /usr/lib/systemd/system/sshd.service
[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.service
Wants=sshd-keygen.service
[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
[root@shu-test init.d]#
任務計劃crontab、服務管理(chkconfig、systemd)