[root@hanfeng ~]# 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 //星期,0或7都表示週日,也能夠寫成英文的簡寫 # | | | | | # * * * * * user-name command to be executed //用戶,不寫用戶就是root 最後一列,是你要執行的命令 [root@hanfeng ~]#
天天凌晨三點,執行123.sh腳本文件,正確的和錯誤的日誌都輸出到123.log文件中 0 3 * * * /bin/bash /usr/local/sbin/123.sh >/tmp/123.log 2>/tmp/123.log 由於是天天三點執行腳本,因此能夠寫成追加,天天都去記錄日誌 0 3 * * * /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log 若想1-10號,雙月去執行該腳本,後面就不在執行了——>只要 被2 整除,就符合條件 0 3 1-10 */2 * /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log 只要周2和周5執行該文件 0 3 1-10 */2 2,5 /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log
[root@hf-01 ~]# 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@hf-01 ~]#
[root@hf-01 ~]# ls /etc/init.d/ functions netconsole network README [root@hf-01 ~]#
[root@hf-01 ~]# chkconfig network off [root@hf-01 ~]# chkconfig --list //會看到2,3,4級別關閉了 注意:該輸出結果只顯示 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@hf-01 ~]# chkconfig network on [root@hf-01 ~]# chkconfig --list //會看到2,3,4級別又開啓了 注意:該輸出結果只顯示 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@hf-01 ~]#
在系統中有七個級別等級列表:html
在centos6中的 /etc/inittab 中定義開機的級別mysql
在centos7中,已經沒有用了,不須要定義開機的級別了linux
[root@hf-01 ~]# chkconfig --level 3 network off //指定network中的3級別關閉 [root@hf-01 ~]# 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@hf-01 ~]#
[root@hf-01 ~]# chkconfig --level 345 network on //指定network中的3,4,5級別開啓 [root@hf-01 ~]# 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@hf-01 ~]#
# chkconfig: 2345 10 90 # description: Activates/Deactivates all network interfaces configured to \ # start at boot time.
[root@hf-01 ~]# cd /etc/init.d [root@hf-01 init.d]# ls functions netconsole network README [root@hf-01 init.d]# cp network 123 [root@hf-01 init.d]# ls -l 總用量 40 -rwxr-xr-x 1 root root 7293 12月 5 05:27 123 -rw-r--r--. 1 root root 17500 5月 3 2017 functions -rwxr-xr-x. 1 root root 4334 5月 3 2017 netconsole -rwxr-xr-x. 1 root root 7293 5月 3 2017 network -rw-r--r--. 1 root root 1160 10月 20 11:07 README [root@hf-01 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@hf-01 init.d]# chkconfig --add 123 //將123加入到服務列表中 [root@hf-01 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@hf-01 init.d]# chkconfig --del 123 //刪除服務列表中的腳本 [root@hf-01 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@hf-01 init.d]#
[root@hf-01 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@hf-01 init.d]# 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 等等
[root@hf-01 ~]# 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@hf-01 ~]# systemctl enable crond.service //讓服務開機啓動 [root@hf-01 ~]# systemctl disable crond.service //不讓開機啓動 Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service. [root@hf-01 ~]# systemctl enable crond.service Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service. [root@hf-01 ~]#
[root@hf-01 ~]# systemctl status crond ● crond.service - Command Scheduler Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled) Active: active (running) since 二 2017-12-05 01:37:49 CST; 5h 15min ago Main PID: 574 (crond) CGroup: /system.slice/crond.service └─574 /usr/sbin/crond -n 12月 05 01:37:49 hf-01 systemd[1]: Started Command Scheduler. 12月 05 01:37:49 hf-01 systemd[1]: Starting Command Scheduler... 12月 05 01:37:50 hf-01 crond[574]: (CRON) INFO (RANDOM_DELAY will be scaled with ...d.) 12月 05 01:37:50 hf-01 crond[574]: (CRON) INFO (running with inotify support) Hint: Some lines were ellipsized, use -l to show in full. [root@hf-01 ~]#
[root@hf-01 ~]# systemctl is-enabled crond enabled [root@hf-01 ~]# systemctl disable crond.service Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service. [root@hf-01 ~]# systemctl is-enabled crond disabled [root@hf-01 ~]# systemctl enable crond.service Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service. [root@hf-01 ~]#
[root@hf-01 ~]# 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@hf-01 ~]# ls -l /etc/systemd/system/multi-user.target.wants/crond.service //是一個軟鏈接,從軟連接的右邊到左邊 lrwxrwxrwx 1 root root 37 12月 5 06:55 /etc/systemd/system/multi-user.target.wants/crond.service -> /usr/lib/systemd/system/crond.service [root@hf-01 ~]# ls -l /usr/lib/systemd/system/crond.service //這裏纔是文件真正的路徑 -rw-r--r--. 1 root root 263 6月 10 2014 /usr/lib/systemd/system/crond.service [root@hf-01 ~]#
[root@hf-01 system]# 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@hf-01 system]# 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@hf-01 system]# ls /etc/systemd/system/default.target /etc/systemd/system/default.target [root@hf-01 system]# ls -l !$ ls -l /etc/systemd/system/default.target lrwxrwxrwx 1 root root 41 12月 5 07:49 /etc/systemd/system/default.target -> /usr/lib/systemd/system/multi-user.target [root@hf-01 system]#
[root@hf-01 system]# cat /usr/lib/systemd/system/sshd.service [Unit] Description=OpenSSH server daemon After=syslog.target network.target auditd.service [Service] EnvironmentFile=/etc/sysconfig/sshd ExecStartPre=/usr/sbin/sshd-keygen ExecStart=/usr/sbin/sshd -D $OPTIONS ExecReload=/bin/kill -HUP $MAINPID KillMode=process Restart=on-failure RestartSec=42s [Install] WantedBy=multi-user.target [root@hf-01 system]#