- 每天特定的时间做的任务,称为例行性任务计划:
- 一次性的任务计划:at (由atd服务提供,centos7 查看atd服务是否开启: systemctl status atd)
- 周期性的任务计划:crontab(由crond服务提供,centos7 查看crond服务是否开启: systemctl status crond)
at:
at [-mldcf] TIME
-l: at -l相当于atq命令,列出目前系统上面所有用户的at队列
-d: at -d相当于atrm命令,后跟一个number可以取消一个在at队列中的任务
-f: 定时执行一个脚本
-c: 后跟一个number,查看指定序号任务内容
TIME:
HH:MM
HH:MM YYYY-MM-DD
crontab:
crontab分系统cron和用户cron:
系统cron:
只能root有权限设置,由/etc/crontab配置文件实现
[root@centos7 tmp]#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
[root@centos7 tmp]#
注意:对于运行后的结果,默认会发邮件反馈给用户,也可以在命令里指定丢弃结果,如下:
COMMAND > /dev/null #错误信息依然反馈给用户
COMMAND &> /dev/null
COMMAND > /dev/null 2>&1
用户cron:
crontab -e :编辑当前用户的cron,文件保存在/etc/spool/cron/目录下,退出时会检测语法
root用户可用-u 指定用户给用户添加cron任务,如:
crontab -u sc -e #root用户给sc用户添加任务
/etc/下有四个目录,此目录下的可执行文件将分别每小时、每天、每周、每月执行一次。
/etc/cron.hourly/
/etc/cron.daily/
/etc/cron.weekly/
/etc/cron.monthly/
可在/etc/cron.deny文件中每行写入一用户,禁止此用户使用crontab功能
例子:每12个小时备份并压缩/etc/目录到/backup目录中,文件名带上时间格式
0 */12 * * * root /usr/bin/tar -zcf /backup/etc-`date +\%F-\%T`.tar.gz /etc/
注意点:建议在cron任务中所使用的命令都用绝对路径,如果使用了%,则需要进行转义\%。
*********************************************************************************************************************
yum仓库创建:
一、开启http服务,目录路径为/var/www/html/
[root@centos7 myyum]#yum -q install httpd #静默模式安装httpd
[root@centos7 myyum]#systemctl start httpd #启动httpd服务
[root@centos7 myyum]#
=================================================================================
二、在/var/www/html/下创建如下结构的目录
[root@centos7 www]#tree -d html/
html/
└── myyum
└── Packages
2 directories
[root@centos7 www]#
把整理好的rpm包全部拷贝到/var/www/html/myyum/Packages目录下
=================================================================================
三、对myyum目录创建仓库数据
[root@centos7 html]#createrepo myyum/
Spawning worker 0 with 3243 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete
[root@centos7 html]#
=================================================================================
四、另外一台主机上配置yum源信息
在/etc/yum.repos.d/目录下新建一个repo文件,设置为:
[my]
name=my
baseurl=http://192.168.1.77/myyum/
gpgcheck=0
安装插件:
[root@centos6 yum.repos.d]#yum install yum-plugin-priorities
然后在repo中添加priority=1,设置此仓库优选使用,如下:
[my]
name=my
baseurl=http://192.168.1.77/myyum/
gpgcheck=0
priority=1
=================================================================================
关闭yum仓库所在主机的防火墙和selinux,然后就可以正常使用此仓库安装程序了
*********************************************************************************************************************
shell脚本while,case语句应用:
#!/bin/bash
#
#
cat << EOF
disk: show disk info
mem: show memory info
cpu: show cpu info
quit
EOF
while true; do
read -p "please input your choice:" option
case $option in
disk)
fdisk -l
;;
mem)
free
;;
cpu)
lscpu
;;
quit)
exit 1
;;
*)
echo "your choice error."
;;
esac
echo -e "\n"
done
*********************************************************************************************************************
sed命令:行编辑器
用法:
sed [option]... {script} [input-file]... [action]
-r: 支持扩展正则表达式,模式支持基础正则表达式
-n: 静默模式
-i: 直接修改源文件
-f: 从指定文件中读取运行
-e: 指定多脚本运行 ex: -e script1 -e script2 -e script3
====================================================================================
地址界定:
#: 指定行
$: 最后一行
/part/: 任意能被part匹配的行
n,m: 从n行开始到m行
#,+m: 从#行开始后直到m行
first~step: 从first行开始,以step为步长 ex: 1~2为奇数行
====================================================================================
编辑命令:
d: 删除
=: 为模式空间中的行打印行号
a \text: 在匹配到的行的下一行追加文本text
i \text: 在匹配到的行的上一行插入文本text
c \text: 用text替换匹配到的行
p: 打印模式空间中的行
s/part1/part2/: 用part2替换part1(支持其他分隔符,如@@@),后加g:全局替换;i:不区
分大小写,支持反向引用(正则表达式语法)
w /path/to/somefile: 把匹配到的行另存为/path/to/somefile
r /path/from/somefile: 把/path/from/somefile文件中内容插入指定位置
====================================================================================
实例:
echo 一个文件路径给sed命令,分别取出路径名和基名:
1.取路径名:要考虑最后带'/'的问题
[root@centos7 test]#echo '/etc/init.d/network/' | sed 's@[^/]\+/\?$@@'
/etc/init.d/
[root@centos7 test]#
2.取基名:使用反向引用替换
[root@centos7 test]#echo '/etc/init.d/network/' | sed 's@^/.*/\([^/]\+/\?$\)@\1@'
network/
[root@centos7 test]#