?
linux任務計劃cron
1.crontab命令任務計劃配置文件
[root@bogon ~]# 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@bogon ~]#
2.編輯計劃任務crontab -e
[root@bogon ~]# crontab -e
3.? 每天三點執行并把正確的和錯誤的輸出到123.log文件中?0 3 * * * /bin/bash /usr/loacl/sbin/123.sh >/tmp/123.log 2>/tmp/123.log,如果是追加將>改成>>會將每天的輸出以追加的方式寫入文件
4.一號到十號的三點執行0 3 1-10 * * /bin/bash /usr/loacl/sbin/123.sh >/tmp/123.log 2>/tmp/123.log
5.一號到十號的每兩個月(能被2整除的月份)三點執行0 3 1-10 */2 * /bin/bash /usr/loacl/sbin/123.sh >/tmp/123.log 2>/tmp/123.log
6.一號到十號的每兩個月(能被2整除的月份)的周二和周五三點執行0 3 1-10 */2 2,5 /bin/bash /usr/loacl/sbin/123.sh >/tmp/123.log 2>/tmp/123.log
7.啟動并查看crond服務
[root@sunyujun01 ~]# systemctl start crond
[root@sunyujun01 ~]# ps aux|grep crond
root 1393 0.0 0.0 124144 508 ? Ss 09:44 0:00 /usr/sbin/crond -n
root 16427 0.0 0.0 112672 972 pts/2 R+ 11:55 0:00 grep --color=auto crond
[root@sunyujun01 ~]#
8.也可以用systemctl status crond命令查看crond啟動狀態
[root@sunyujun01 ~]# systemctl status crond
● crond.service - Command SchedulerLoaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)Active: active (running) since 三 2018-01-24 09:44:22 CST; 3h 26min agoMain PID: 1393 (crond)CGroup: /system.slice/crond.service└─1393 /usr/sbin/crond -n1月 24 09:44:22 localhost.localdomain systemd[1]: Started Command Scheduler.
1月 24 09:44:22 localhost.localdomain systemd[1]: Starting Command Scheduler...
1月 24 09:44:23 localhost.localdomain crond[1393]: (CRON) INFO (Syslog will ...
1月 24 09:44:23 localhost.localdomain crond[1393]: (CRON) INFO (RANDOM_DELAY...
1月 24 09:44:23 localhost.localdomain crond[1393]: (CRON) INFO (running with...
Hint: Some lines were ellipsized, use -l to show in full.
[root@sunyujun01 ~]#
9.停掉crond
[root@sunyujun01 ~]# systemctl stop crond
10.如果腳步中的命令沒有執行,可能是命令沒有加入到crond變量里面,也可以直接寫絕對路徑
11.這個文件的內容是crontab -l列出來的計劃任務列表,如果備份的話直接拷貝這個文件就行
[root@sunyujun01 ~]# cat /var/spool/cron/root
12.crontab -r 刪除
chkconfig工具
?1.chkconfig --list查看使用chkconfig工具的服務?
[root@sunyujun01 ~]# chkconfig --list注意:該輸出結果只顯示 SysV 服務,并不包含原生 systemd 服務。SysV 配置數據可能被原生 systemd 配置覆蓋。 如果您想列出 systemd 服務,請執行 'systemctl list-unit-files'。欲查看對特定 target 啟用的服務請執行'systemctl list-dependencies [target]'。mysql 0:關 1:關 2:開 3:開 4:開 5:開 6:關
netconsole 0:關 1:關 2:關 3:關 4:關 5:關 6:關
network 0:關 1:關 2:開 3:開 4:開 5:開 6:關
nginx 0:關 1:關 2:開 3:開 4:開 5:開 6:關
php-fpm 0:關 1:關 2:開 3:開 4:開 5:開 6:關
[root@sunyujun01 ~]#
2.對應的服務都在/etc/init.d/目錄下
[root@sunyujun01 ~]# ls /etc/init.d/
functions mysql netconsole network nginx php-fpm README
[root@sunyujun01 ~]#
3.關閉network服務 chkconfig network off
[root@sunyujun01 ~]# chkconfig network off
[root@sunyujun01 ~]# chkconfig --list注意:該輸出結果只顯示 SysV 服務,并不包含原生 systemd 服務。SysV 配置數據可能被原生 systemd 配置覆蓋。 如果您想列出 systemd 服務,請執行 'systemctl list-unit-files'。欲查看對特定 target 啟用的服務請執行'systemctl list-dependencies [target]'。mysql 0:關 1:關 2:開 3:開 4:開 5:開 6:關
netconsole 0:關 1:關 2:關 3:關 4:關 5:關 6:關
network 0:關 1:關 2:關 3:關 4:關 5:關 6:關
nginx 0:關 1:關 2:開 3:開 4:開 5:開 6:關
php-fpm 0:關 1:關 2:開 3:開 4:開 5:開 6:關
[root@sunyujun01 ~]#
4.network的6個級別都變成關閉狀態了
5.重新啟動network
0 停機 1 單用戶模式 2 多用戶,沒有 NFS 3 完全多用戶模式 4 沒有用到 5 圖形界面 6 重新啟動 S s Single user mode
[root@sunyujun01 ~]# chkconfig network on
[root@sunyujun01 ~]# chkconfig --list 注意:該輸出結果只顯示 SysV 服務,并不包含原生 systemd 服務。SysV 配置數據可能被原生 systemd 配置覆蓋。 如果您想列出 systemd 服務,請執行 'systemctl list-unit-files'。欲查看對特定 target 啟用的服務請執行'systemctl list-dependencies [target]'。mysql 0:關 1:關 2:開 3:開 4:開 5:開 6:關
netconsole 0:關 1:關 2:關 3:關 4:關 5:關 6:關
network 0:關 1:關 2:開 3:開 4:開 5:開 6:關
nginx 0:關 1:關 2:開 3:開 4:開 5:開 6:關
php-fpm 0:關 1:關 2:開 3:開 4:開 5:開 6:關
[root@sunyujun01 ~]#
?6.在centos7之前的版本可以通過編輯文件定義系統級別
[root@sunyujun01 ~]# vim /etc/inittab
7. 設定某個運行級別開關
[root@sunyujun01 ~]# chkconfig --level 3 network off
8.也可以寫多個345
[root@sunyujun01 ~]# chkconfig --level 345 network off
9.將123服務添加到服務中
[root@sunyujun01 init.d]# cp network 123^C
[root@sunyujun01 init.d]# ls -l
總用量 60
-rwxr-xr-x 1 root root 6630 1月 24 14:17 123
-rw-r--r--. 1 root root 13948 9月 16 2015 functions
-rwxr-xr-x. 1 root root 10880 2月 10 2017 mysql
-rwxr-xr-x. 1 root root 2989 9月 16 2015 netconsole
-rwxr-xr-x. 1 root root 6630 9月 16 2015 network
-rwxr-xr-x. 1 root root 2652 2月 10 2017 nginx
-rwxr-xr-x. 1 root root 2401 2月 10 2017 php-fpm
-rw-r--r--. 1 root root 1160 11月 22 2016 README
[root@sunyujun01 init.d]# chkconfig --list注意:該輸出結果只顯示 SysV 服務,并不包含原生 systemd 服務。SysV 配置數據可能被原生 systemd 配置覆蓋。 如果您想列出 systemd 服務,請執行 'systemctl list-unit-files'。欲查看對特定 target 啟用的服務請執行'systemctl list-dependencies [target]'。mysql 0:關 1:關 2:開 3:開 4:開 5:開 6:關
netconsole 0:關 1:關 2:關 3:關 4:關 5:關 6:關
network 0:關 1:關 2:開 3:開 4:開 5:開 6:關
nginx 0:關 1:關 2:開 3:開 4:開 5:開 6:關
php-fpm 0:關 1:關 2:開 3:開 4:開 5:開 6:關
[root@sunyujun01 init.d]# chkconfig --add 123
[root@sunyujun01 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:關
mysql 0:關 1:關 2:開 3:開 4:開 5:開 6:關
netconsole 0:關 1:關 2:關 3:關 4:關 5:關 6:關
network 0:關 1:關 2:開 3:開 4:開 5:開 6:關
nginx 0:關 1:關 2:開 3:開 4:開 5:開 6:關
php-fpm 0:關 1:關 2:開 3:開 4:開 5:開 6:關
[root@sunyujun01 init.d]#
10.刪除一個服務
[root@sunyujun01 init.d]# chkconfig --del 123
[root@sunyujun01 init.d]#
systemd管理服務
1.列出所有service服務,如果想列出所有用systemctl list-unit-files
[root@sunyujun01 init.d]# systemctl list-units -all --type=service
unit介紹
1.7個運行級別一一對應的軟連接
[root@sunyujun01 system]# ls -l runlevel*
lrwxrwxrwx. 1 root root 15 2月 10 2017 runlevel0.target -> poweroff.target
lrwxrwxrwx. 1 root root 13 2月 10 2017 runlevel1.target -> rescue.target
lrwxrwxrwx. 1 root root 17 2月 10 2017 runlevel2.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 2月 10 2017 runlevel3.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 2月 10 2017 runlevel4.target -> multi-user.target
lrwxrwxrwx. 1 root root 16 2月 10 2017 runlevel5.target -> graphical.target
lrwxrwxrwx. 1 root root 13 2月 10 2017 runlevel6.target -> reboot.target
?
target介紹(一個target是多個unit的組合)
?
?
?
?
?