[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]#