擴展
1. anacron http://blog.csdn.net/strikers1982/article/details/4787226
2. xinetd服(默認機器沒有安裝這個服務,須要yum install xinetd安裝) http://blog.sina.com.cn/s/blog_465bbe6b010000vi.html
3. systemd自定義啓動腳本 http://www.jb51.net/article/100457.htm
html
crontab命令被用來提交和管理用戶的須要週期性執行的任務,與windows下的計劃任務相似,當安裝完成操做系統後,默認會安裝此服務工具,而且會自動啓動crond進程,crond進程每分鐘會按期檢查是否有要執行的任務,若是有要執行的任務,則自動執行該任務。linux
語法: crontab [options]
Options:
-e:=edit 編輯用戶的計時器設置
-l:=list 列出用戶的計時器設置
-r:=remove 刪除用戶的計時器設置
-u:=user 指定設定計時器的用戶shell
crontab的配置文件: /etc/crontabwindows
[root@yong-01 ~]# 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
* * * * *centos
分 時 日 月 周bash
例如:session
[root@yong-01 ~]# crontab -e 0 3 * * * /bin/bash /user/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log 0 3 1-10 */2 2,5 /bin/bash /user/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log
解析:ssh
第一條命令:
天天凌晨3點(*位置不指定數字就表明天天、月、周),當前用戶(未指定用戶,默認爲當前用戶)執行該命令(123.sh提早寫好的命令腳本),並將正確日誌和錯誤日誌記錄到/tmp/123.log文件中。socket
第二條命令:
每一個偶數月(*/2:表示能被2整除)1號到10號的週二和週五的凌晨3點,當前用戶(未指定用戶,默認爲當前用戶)執行該命令(123.sh提早寫好的命令腳本),並將正確日誌和錯誤日誌記錄到/tmp/123.log文件中。工具
配置完成後須要啓動crond服務:
啓動服務: [root@yong-01 ~]# systemctl start crond 查看crond服務狀態: 方法1: [root@yong-01 ~]# systemctl status crond Active: active (running) since 一 2017-07-17 10:05:11 CST; 4h 48min ago //看這一行active (running) 方法2: 看進程crond [root@yong-01 ~]# ps aux |grep crond root 707 0.0 0.0 126268 1656 ? Ss 20:29 0:00 /usr/sbin/crond -n root 11520 0.0 0.0 112676 984 pts/0 R+ 21:15 0:00 grep --color=auto cro 中止crond服務: [root@yong-01 ~]# systemctl stop crond.service
注意: 在編寫配置文件或者shell腳本時,全部的命令都要使用絕對路徑;每一個計劃任務追加一個日誌。
[root@yong-01 ~]# crontab -l 0 3 * * * /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>123.log 0 3 1-10 */2 2,5 /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>123.log
計劃任務存放位置: /var/spool/cron/,全部的計劃任務存放在該目錄下以用戶名命名的文件中,備份時可使用該文件。
[root@yong-01 ~]# cat /var/spool/cron/root 0 3 * * * /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>123.log 0 3 1-10 */2 2,5 /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>123.log
[root@yong-01 ~]# crontab -r
注: 以上全部操做均可以附加-u選項來指定用戶。
chkconfig命令檢查、設置系統的各類服務。這是Red Hat公司遵循GPL規則所開發的程序,它可查詢操做系統在每個執行等級中會執行哪些系統服務,其中包括各種常駐服務。謹記chkconfig不是當即自動禁止或激活一個服務,它只是簡單的改變了符號鏈接(該命令多用於centos6及之前版本)。
語法: chkconfig [options]
Options:
--list:查看在使用chkconfig命令的服務的狀態
--add:增長指定服務
--del:刪除指定服務
--level:指定某系統服務要在系統某運行級別中開啓或關畢。
[root@yong-02 ~]# 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:關
服務所在位置:/etc/init.d/
[root@yong-02 ~]# ls /etc/init.d/ functions netconsole network README
更改服務狀態
[root@yong-02 ~]# chkconfig network off/on
[root@yong-02 ~]# chkconfig --level 345 network off/on
運行級別配置文件:「/etc/inittab」,centos7已再也不使用該文件。
添加/刪除服務
首先,在添加服務以前必須把該服務的腳本放到「/etc/init.d/」目錄下並添加執行權限。而後執行命令:用cp /etc/init.d/network /etc/init.d/123 來作演示:
[root@yong-02 ~]# cp /etc/init.d/network /etc/init.d/123 [root@yong-02 ~]# ls /etc/init.d/ 123 functions netconsole network README 添加: [root@yong-02 ~]# chkconfig --add /etc/init.d/123 [root@yong-02 ~]# 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@yong-02 ~]# chkconfig --del /etc/init.d/123 [root@yong-02 ~]# 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:關
注: 關於該服務腳本
- systemd是centos7管理的一個服務機制,在centos6或以前的版本中可使用chkconfig工具去管理系統的服務,在centos7中,也可使用,但會提示使用 systemctl list-unit-files ,用它來查看全部的服務。
• systemctl list-units --all --type=service
• 幾個經常使用的服務相關的命令
• systemctl enable crond.service //讓服務開機啓動
• systemctl disable crond //不讓開機啓動
• systemctl status crond //查看狀態
• systemctl stop crond //中止服務
• systemctl start crond //啓動服務
• systemctl restart crond //重啓服務
• systemctl is-enabled crond //檢查服務是否開機啓動
systemctl list-unit-files //查看全部的服務
[root@yong-02 ~]# 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@yong-02 ~]# systemctl list-unit-files //查看全部的服務,裏面不只有service,還有socket,還有target UNIT FILE STATE proc-sys-fs-binfmt_misc.automount static dev-hugepages.mount static dev-mqueue.mount static proc-sys-fs-binfmt_misc.mount static sys-fs-fuse-connections.mount static sys-kernel-config.mount static sys-kernel-debug.mount static tmp.mount disabled brandbot.path disabled 等等
systemctl list-units --all --type=service //列出全部的service
[root@yong-02 ~]# systemctl list-units --all --type=service //列出全部的service UNIT LOAD ACTIVE SUB DESCRIPTION auditd.service loaded active running Security Auditing Service avahi-daemon.service loaded active running Avahi mDNS/DNS-SD Stack brandbot.service loaded inactive dead Flexible Branding Service cpupower.service loaded inactive dead Configure CPU power related crond.service loaded active running Command Scheduler 等等等,只截取了一部分 並在最下面,會告訴你 LOAD,ACTIVE,SUB是什麼意思 LOAD = Reflects whether the unit definition was properly loaded. ACTIVE = The high-level unit activation state, i.e. generalization of SUB. SUB = The low-level unit activation state, values depend on unit type. 還會提醒,若想列出全部的 unit files,請使用 systemctl list-unit-files 命令
[root@yong-02 ~]# systemctl enable crond //讓服務開機啓動 [root@yong-02 ~]# systemctl disable crond //不讓開機啓動 Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service. [root@yong-02 ~]# systemctl enable crond Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.
[root@yong-02 ~]# systemctl status crond ● crond.service - Command Scheduler Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled) Active: active (running) since 六 2018-05-12 01:41:51 CST; 52min ago Main PID: 642 (crond) CGroup: /system.slice/crond.service └─642 /usr/sbin/crond -n 5月 12 01:41:51 yong-02 systemd[1]: Started Command Scheduler. 5月 12 01:41:51 yong-02 systemd[1]: Starting Command Scheduler... 5月 12 01:41:51 yong-02 crond[642]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 62% if used.) 5月 12 01:41:52 yong-02 crond[642]: (CRON) INFO (running with inotify support)
[root@yong-02 ~]# systemctl is-enabled crond enabled [root@yong-02 ~]# systemctl disable crond Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service. [root@yong-02 ~]# systemctl is-enabled crond disabled [root@yong-02 ~]# systemctl enable crond Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.
[root@yong-02 ~]# cat /etc/systemd/system/multi-user.target.wants/crond.service 得到service的配置文件內容 [Unit] Description=Command Scheduler After=syslog.target auditd.service systemd-user-sessions.service time-sync.target [Service] EnvironmentFile=/etc/sysconfig/crond ExecStart=/usr/sbin/crond -n $CRONDARGS KillMode=process [Install] WantedBy=multi-user.target [root@yong-02 ~]# ls -l /etc/systemd/system/multi-user.target.wants/crond.service //是一個軟鏈接,從軟連接的右邊到左邊 lrwxrwxrwx. 1 root root 37 5月 12 02:35 /etc/systemd/system/multi-user.target.wants/crond.service -> /usr/lib/systemd/system/crond.service [root@yong-02 ~]# ls -l /usr/lib/systemd/system/crond.service //這裏纔是文件真正的路徑 -rw-r--r--. 1 root root 284 8月 3 2017 /usr/lib/systemd/system/crond.service
說明: 伴隨某服務的開/關會創建/刪除一個指向該服務的軟連接「/etc/systemd/system/multi-user.target.wants/crond.service」-->「/usr/lib/systemd/system/crond.service」
[root@yong-02 ~]# systemctl list-unit-files --type=target //列出系統中全部的target UNIT FILE STATE basic.target static bluetooth.target static cryptsetup-pre.target static cryptsetup.target static ctrl-alt-del.target disabled default.target enabled emergency.target static 等等等,只截取了一部分
[root@yong-02 ~]# systemctl list-dependencies multi-user.target multi-user.target ● ├─auditd.service ● ├─brandbot.path ● ├─chronyd.service ● ├─crond.service ● ├─dbus.service ● ├─firewalld.service ● ├─irqbalance.service 等等
[root@yong-02 ~]# systemctl get-default multi-user.target
[root@yong-02 ~]# systemctl set-default multi-user.target Removed symlink /etc/systemd/system/default.target. Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target. [root@yong-02 ~]# ll /etc/systemd/system/default.target lrwxrwxrwx. 1 root root 41 5月 12 03:11 /etc/systemd/system/default.target -> /usr/lib/systemd/system/multi-user.target
[root@yong-02 ~]# 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 //看這一行屬於 multi-user.target
一個service屬於一種類型的unit,多個unit組成一個target,一個target包含多個service。