crontab计划任务文件
任务计划文件保存路径/var/spool/cron/username(若是用户是root,那么打开的就是/var/spool/cron/root)
系统任务调度的配置文件:cat /etc/crontabshell
[root@jinkai ~]# 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 executedvim[root@jinkai ~]#windows
*前四行是用来配置crond任务运行的环境变量
第一行SHELL变量指定了系统要使用哪一个shell,这里是bash
第二行PATH变量指定了系统执行命令的路径
第三行MAILTO变量指定了crond的任务执行信息将经过电子邮件发送给root用户
若是MAILTO变量的值为空,则表示不发送任务执行信息给用户
第四行的HOME变量指定了在执行命令或者脚本时使用的主目录。
星号():表明全部可能的值
逗号(,):能够用逗号隔开的值指定一个列表范围,例如,“1,2,5,7,8,9”
中杠(-):能够用整数之间的中杠表示一个整数范围,例如“2-6”表示“2,3,4,5,6”
正斜线(/):能够用正斜线指定时间的间隔频率,例如“0-23/2”表示每两小时执行一次。
格式:分钟 小时 日 月 周 user-name command
范围:
分钟:0-59;
小时:0-23;
日:0-31;
月:0-12;
周:0-6,0为周日、或7为周日;
User:若是不指定用户,默认就是当前用户**centos
安装:bash
yum install -y crontabsssh
建立或编辑计划任务socket
crontab -e ide
查看当前计划任务:
crontab -l
[root@jinkai logrotate.d]# crontab -l
0 3 1-10 /2 2,5 /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log
1 10 2 /usr/bin/find /tmp/ -type f -mtime +100 |xargs rm -f
[root@jinkai logrotate.d]# cat /var/spool/cron/root
0 3 1-10 /2 2,5 /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log
1 10 2 /usr/bin/find /tmp/ -type f -mtime +100 |xargs rm -f工具
删除定时任务
crontab -r
从/var/spool/cron目录中删除用户的crontab文件,若是不指定用户,则默认删除当前用户的crontab文件,加个-i 删除时提示确认rest
[root@jinkai ~]# crontab -r -i
crontab: really delete root's crontab? n
[root@jinkai ~]#
备份crontab文件
crontab -l > $HOME/mycron
[root@jinkai ~]# echo $HOME
/root
[root@jinkai ~]# crontab -l > $HOME/a.txt
[root@jinkai ~]# cat /root/a.txt
20 1 * echo " " /var/log/slow.log
- 3 0 /bin/sh /usr/local/sbin/backup.sh
10 4 14 /bin/sh /usr/local/sbin/backup_month.sh
0 /8 /usr/sbin/ntpdate time.windows.com
0 1,12,18 /bin/sh /usr/local/sbin/test.sh
0 9-18 /bin/sh /usr/local/sbin/test2.sh
注意1:用命令能够执行,用crontab没法执行的脚本
1,使用绝对路径
2,在shell脚本开头使用一下代码:
#!/bin/bash
. /etc/profile
. ~/bash_profile
3,能够在/etc/crontab中添加环境变量,在能够执行命令前添加
. /etc/profile;/bin/sh
注意2:当crontab忽然失效时,能够尝试/etc/init.d/crond restart解决问题。或者查看日志看某个job有没有执行报错 tail -f /var/log/cron。
注意3:在crontab中%是有特殊含义的,表示换行。若是要用的话必须进行转义\% ,如常常用的date ‘+%Y%m%d’在crontab里是不会被执行的,应该换成date ‘+\%Y\%m\%d’
systemctl enable crond 开机启动
systemctl disable crond 开机不启动
systemctl stop crond 关闭
systemctl start crond 开启
systemctl status crond 服务状态
systemctl is-enabled crond 检查服务是否设置开机启动**
CentOS 6上的服务管理工具为chkconfig,Linux系统全部的预设置服务均可以经过查看/etc/init.d目录获得;
[root@jinkai ~]# ls /etc/init.d/
functions netconsole network README
查看服务以及每一个级别的开启状态
chkconfig --list
[root@jinkai ~]# 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@jinkai ~]#
级别(0~6):为系统启动级别,0、一、6被系统保留。能够参照init命令;
0:为shutdown动做;
1:做为重启到单用户模式;
6:为重启;
2:无NFS支持的多用户模式;
3:彻底多用户模式;
4:保留给用户自定义;
5:图形界面;
更改某个服务对应级别下的状态:
例如:开启或者关闭network下的3级别(还能够省略级别,默认就是针对2,3,4,5操做的)
chkconfig --level 3 network off/on
增长自定义服务
chkconfig --add 服务名
添加一个服务到系统服务中,必须让这个服务在/etc/init.d中
[root@jinkai ~]# cd /etc/init.d/
[root@jinkai init.d]# cp network 123
[root@jinkai init.d]# ls
123 functions netconsole network README
[root@jinkai init.d]# chkconfig --add 123
[root@jinkai init.d]# 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@jinkai init.d]#
删除服务
chkconfig --del 服务名
删除123服务
[root@jinkai init.d]# chkconfig --del 123
[root@jinkai init.d]# chkconfig --list
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
[root@jinkai init.d]#
用于CentOS 7系统下、CentOS 6系统经常使用chkconfig工具;
列出系统全部的服务
systemctl list-units --all --type=service
查看某个服务
systemctl list-unit-files |grep sshd
让服务开机启动
systemctl enable crond.service
服务对应的启动脚本文件
/usr/lib/systemd/system
上面目录中的文件能够归为下面几类:
service:系统服务;
target:多个unit组成的组;
device:硬件设备;
mount:系统挂载点;
automount:自动挂载点;
path:文件或路径;
scope:不是由systemd启动的外部进程;
slice:进程组;
snapshot:systemd快照;
socket:进程间通讯套接字;
swap:swap文件;
timer:定时器;
以上每种类型的文件都为一个unit
列出正在运行的unit
systemctl list-units
列出全部的unit
systemctl list-units --all
列出全部inactive的unit
systemctl list-units --all --state=inactive
列出全部状态的service
systemctl list-units --all --type=service
列出状态为active的service
systemctl list-units --type=service
查看某个unit是否active
systemctl is-active crond.service
target相似于centos6里面启动级别,target支持多个target同时启动。target实际上是多个unit的组合,系统启动说白了就是启动了多个unit
查看当前系统的全部target
systemctl list-unit-files --type=target
查看一个target包含的全部unit,下面以multi-user.target为例
systemctl list-dependencies multi-user.target
查看系统默认的target
systemctl get-default
设置默认的target
例如设置默认target为multi-user.target
systemctl set-default multi-user.target
一个service属于一种类型的unit
多个unit组成一个target
一个target里面包含了多个service
查看service服务文件
查看文件的install,定义了这个service属于哪一个target
[root@jinkai init.d]# vim /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