您的位置:首页 > 运维架构 > Linux

171204 8周1次课 linux任务计划cron

2017-12-04 22:44 274 查看
crontab -u、-e、-l、-r
格式:分 时 日 月 周 user command
文件/var/spool/cron/username
分范围0-59,时范围0-23,日范围0-31,月范围1-12,周0-6
可用格式1-5表示一个范围1到5
可用格式1,2,3表示1或者2或者3
可用格式*/2表示被2整除的数字,比如小时,那就是每隔2小时
要保证服务是启动状态
systemctl start crond.service
[root@node73 ~]# 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

每天凌晨3点钟执行指定脚本,并且将正确重定向或错误的重定向到指定文件日志里

0 3 * * * /bin/bash /usr/local/sbin/123.sh >> /tmp/123.log 2>> /tmp/123.log
每两个月的前10天且这十天内刚好为周二和周五的时间执行指定脚本
0 3 1-10 */2 2,5 /bin/bash /usr/local/sbin/111.sh >> /tmp/123.log 2>> /tmp/123.log

要保证服务是启动状态
[root@node73 ~]# systemctl start crond
[root@node73 ~]# 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-04 16:18:12 CST; 5h 25min ago
Main PID: 1073 (crond)
CGroup: /system.slice/crond.service
└─1073 /usr/sbin/crond -n

12月 04 16:18:12 node73 systemd[1]: Started Command Scheduler.
12月 04 16:18:12 node73 systemd[1]: Starting Command Scheduler...
12月 04 16:18:12 node73 crond[1073]: (CRON) INFO (RANDOM_DELAY will be scaled wit....)
12月 04 16:18:13 node73 crond[1073]: (CRON) INFO (running with inotify support)
12月 04 21:43:01 node73 crond[1073]: (root) RELOAD (/var/spool/cron/root)
Hint: Some lines were ellipsized, use -l to show in full.
[root@node73 ~]# ps aux | grep crond
root       1073  0.0  0.0 126252  1660 ?        Ss   17:19   0:00 /usr/sbin/crond -n
root       8225  0.0  0.0 112668   972 pts/3    S+   21:43   0:00 grep --color=auto crond

查看crondtab  -l
[root@node73 ~]# crontab -l
0 3 1-10 */2 2,5 /bin/bash /usr/local/sbin/111.sh >> /tmp/123.log 2>> /tmp/123.log1 10 * 2 * /usr/bin/find /tmp/ -type f -mtime +100 |xargs rm -f
[root@node73 ~]# cat /var/spool/cron/root
0 3 1-10 */2 2,5 /bin/bash /usr/local/sbin/111.sh >> /tmp/123.log 2>> /tmp/123.log1 10 * 2 * /usr/bin/find /tmp/ -type f -mtime +100 |xargs rm -f

删除crondtab -r

Linux系统服务管理-chkconfig

chkconfig --list
chkconfig --level 3 network off
chkconfig --level 345 network off
chkconfig --del network
chkconfig --add network
[root@node73 ~]# 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@node73 ~]# ls /etc/init.d/
functions  netconsole  network  README
指定级别
[root@node73 ~]# chkconfig --level 3 network off
[root@node73 ~]# 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:关


linux系统服务管理----systemd

•systemctllist-units --
all --type=service
[root@node73 ~]# systemctl list-units --all --type=service
[root@node73 ~]# systemctl enable crond.service    ###让服务开机启动

[root@node73 ~]#  systemctl disable crond ####不让服务开机启动
[root@node73
~]# systemctl status crond    ####查看状态
[root@node73
~]# systemctl stop crond     ####停止服务
[root@node73
~]# systemctl start crond    ####启动服务
[root@node73
~]# systemctl restart crond    ####重启服务
[root@node73
~]# systemctl is-enabled crond    #####检查服务是否开机启动



Linux系统服务管理-systemd


•sys

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  centos