扩展
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。