crontab命令被用来提交和管理用户的须要周期性执行的任务,与windows下的计划任务相似,当安装完成操做系统后,默认会安装此服务工具,而且会自动启动crond进程,crond进程每分钟会按期检查是否有要执行的任务,若是有要执行的任务,则自动执行该任务。linux
语法: crontab [options]
Options:
-e:=edit 编辑用户的计时器设置
-l:=list 列出用户的计时器设置
-r:=remove 删除用户的计时器设置
-u:=user 指定设定计时器的用户nginx
crontab的配置文件: /etc/crontabweb
[root@3 grep]# 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
eg:shell
[root@3 grep]# 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
解析:windows
第一条命令:
天天凌晨3点(*位置不指定数字就表明天天、月、周),当前用户(未指定用户,默认为当前用户)执行该命令(123.sh提早写好的命令脚本),并将正确日志和错误日志记录到/tmp/123.log文件中。centos
第二条命令:
每一个偶数月(*/2:表示能被2整除)1号到10号的周二和周五的凌晨3点,当前用户(未指定用户,默认为当前用户)执行该命令(123.sh提早写好的命令脚本),并将正确日志和错误日志记录到/tmp/123.log文件中。bash
配置完成后须要启动crond服务:ssh
启动服务: [root@3 grep]# systemctl start crond 查看crond服务状态: 方法1: [root@3 grep]# systemctl status crond.service ● crond.service - Command Scheduler Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled) Active: active (running) since 一 2017-07-17 10:05:11 CST; 4h 48min ago Main PID: 501 (crond) …… 方法2: [root@3 grep]# ps aux |grep crond root 501 0.0 0.1 126224 1624 ? Ss 10:05 0:00 /usr/sbin/crond -n 中止crond服务: [root@3 grep]# systemctl stop crond.service
注意: 在编写配置文件或者shell脚本时,全部的命令都要使用绝对路径;每一个计划任务追加一个日志。socket
[root@3 grep]# crontab -l no crontab for root
计划任务存放位置: /var/spool/cron/,全部的计划任务存放在该目录下以用户名命名的文件中,备份时可使用该文件。工具
[root@3 grep]# crontab -r
注: 以上全部操做均可以附加-u选项来指定用户。
chkconfig命令检查、设置系统的各类服务。这是Red Hat公司遵循GPL规则所开发的程序,它可查询操做系统在每个执行等级中会执行哪些系统服务,其中包括各种常驻服务。谨记chkconfig不是当即自动禁止或激活一个服务,它只是简单的改变了符号链接(该命令多用于centos6及之前版本)。
语法: chkconfig [options]
Options:
--list:查看在使用chkconfig命令的服务的状态
--add:增长指定服务
--del:删除指定服务
--level:指定某系统服务要在系统某运行级别中开启或关毕。
[root@3 grep]# 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@3 grep]# ls /etc/init.d/ functions netconsole network README
更改服务状态
[root@3 grep]# chkconfig network off/on
[root@3 grep]# chkconfig --level 345 network off/on
运行级别配置文件:“/etc/inittab”,centos7已再也不使用该文件。
添加/删除服务
首先,在添加服务以前必须把该服务的脚本放到“/etc/init.d/”目录下并添加执行权限。而后执行命令:
[root@3 ~]# ls /etc/init.d/ 123 functions netconsole network README 添加/删除: [root@3 ~]# chkconfig --add /etc/init.d/123 [root@3 ~]# chkconfig --list 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@3 init.d]# chkconfig --del /etc/init.d/123
注: 关于该服务脚本
>systemctl命令是系统服务管理器指令,它实际上将 service 和 chkconfig 这两个命令组合到一块儿。
[root@3 ~]# systemctl disable crond Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service. [root@3 ~]# systemctl enable crond Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.
说明: 伴随某服务的开/关会创建/删除一个指向该服务的软连接“/etc/systemd/system/multi-user.target.wants/crond.service”-->“/usr/lib/systemd/system/crond.service”
节选于:http://www.jb51.net/article/100457.htm
设置自定义开机启动的方法:
systemd有系统和用户区分:系统(/user/lib/systemd/system/),用户(/etc/lib/systemd/user/)。
通常系统管理员手工建立的单元文件建议存放在/etc/systemd/system/目录下面。
[Unit] Description=nginx - high performance web server Documentation=http://nginx.org/en/docs/ After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/run/nginx.pid ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
[Unit]
Description : 服务的简单描述
Documentation : 服务文档
Before、After:定义启动顺序。Before=xxx.service,表明本服务在xxx.service启动以前启动。After=xxx.service,表明本服务在xxx.service以后启动。
Requires:这个单元启动了,它须要的单元也会被启动;它须要的单元被中止了,这个单元也中止了。
Wants:推荐使用。这个单元启动了,它须要的单元也会被启动;它须要的单元被中止了,对本单元没有影响。
[Service]
Type=simple(默认值):systemd认为该服务将当即启动。服务进程不会fork。若是该服务要启动其余服务,不要使用此类型启动,除非该服务是socket激活型。
Type=forking:systemd认为当该服务进程fork,且父进程退出后服务启动成功。对于常规的守护进程(daemon),除非你肯定此启动方式没法知足需求,使用此类型启动便可。使用此启动类型应同时指定 PIDFile=,以便systemd可以跟踪服务的主进程。
Type=oneshot:这一选项适用于只执行一项任务、随后当即退出的服务。可能须要同时设置 RemainAfterExit=yes 使得 systemd 在服务进程退出以后仍然认为服务处于激活状态。
Type=notify:与 Type=simple 相同,但约定服务会在就绪后向 systemd 发送一个信号。这一通知的实现由 libsystemd-daemon.so 提供。
Type=dbus:若以此方式启动,当指定的 BusName 出如今DBus系统总线上时,systemd认为服务就绪。
Type=idle: systemd会等待全部任务(Jobs)处理完成后,才开始执行idle类型的单元。除此以外,其余行为和Type=simple 相似。
PIDFile:pid文件路径
ExecStart:指定启动单元的命令或者脚本,ExecStartPre和ExecStartPost节指定在ExecStart以前或者以后用户自定义执行的脚本。Type=oneshot容许指定多个但愿顺序执行的用户自定义命令。
ExecReload:指定单元中止时执行的命令或者脚本。
ExecStop:指定单元中止时执行的命令或者脚本。
PrivateTmp:True表示给服务分配独立的临时空间
Restart:这个选项若是被容许,服务重启的时候进程会退出,会经过systemctl命令执行清除并重启的操做。
RemainAfterExit:若是设置这个选择为真,服务会被认为是在激活状态,即便因此的进程已经退出,默认的值为假,这个选项只有在Type=oneshot时须要被配置。
[Install]
Alias:为单元提供一个空间分离的附加名字。
RequiredBy:单元被容许运行须要的一系列依赖单元,RequiredBy列表从Require得到依赖信息。
WantBy:单元被容许运行须要的弱依赖性单元,Wantby从Want列表得到依赖信息。
Also:指出和单元一块儿安装或者被协助的单元。
DefaultInstance:实例单元的限制,这个选项指定若是单元被容许运行默认的实例。
systemctl enable nginx.service
就会在/etc/systemd/system/multi-user.target.wants/目录下新建一个/usr/lib/systemd/system/nginx.service 文件的连接。
#启动服务 $ sudo systemctl start nginx.service #查看日志 $ sudo journalctl -f -u nginx.service — Logs begin at 四 2015-06-25 17:32:20 CST. — 6月 25 10:28:24 Leco.lan systemd[1]: Starting nginx – high performance web server… 6月 25 10:28:24 Leco.lan nginx[7976]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok 6月 25 10:28:24 Leco.lan nginx[7976]: nginx: configuration file /etc/nginx/nginx.conf test is successful 6月 25 10:28:24 Leco.lan systemd[1]: Started nginx – high performance web server. #重启 $ sudo systemctl restart nginx.service #重载 $ sudo systemctl reload nginx.service #中止 $ sudo systemctl stop nginx.service
unit所在目录: /usr/lib/systemd/system/
系统为了方便管理,因此使用target来管理unit。
一个service属于一种类型的unit,多个unit组成一个target,一个target包含多个service。
查看一个service属于哪一个target:
[root@3 system]# cat /usr/lib/systemd/system/sshd.service 看Install部分! ……… [Install] WantedBy=multi-user.target