Summary01 - cron任务、grep、find命令

xiaoxiao2021-02-28  44

############################################################## cron计划任务 cron任务概述 • 用途:按照设置的时间间隔为用户反复执行某一项固定的系统任务 • 软件包:cronie、crontabs • 系统服务:crond • 日志文件:/var/log/crond 如何编写crontab任务记录 –  分     时     日     月       周      任务命令行(绝对路径)      *      *       *       *        *               * : 匹配范围内任意时间 , : 分隔多个不连续的时间点 - : 指定连续时间范围 /n :指定时间频率,每n ... • 使用 crontab 命令

– 编辑:crontab -e [-u 用户名]

 

每分钟记录    当前系统的时间, 写入到/opt/time.txt

虚拟机Server [root@server0 ~]# date

2018年 03月 15日 星期四 10:10:27 CST

[root@server0 ~]# crontab -e -u root */1  *  *  *  *    date >> /opt/time.txt [root@server0 ~]# cat /opt/time.txt

例子: 每天早上6点 0 6 * * * echo "Good morning." >> /tmp/test.txt //注意单纯echo,从屏幕上看不到任何输出,因为cron把任何输出都email到root的信箱了。每两个小时 0 */2 * * * echo "Have a break now." >> /tmp/test.txt  每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点 0 11 4 * 1-3 command line1月1日早上4点 0 4 1 1 * command line SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root //如果出现错误,或者有数据输出,数据作为邮件发给这个帐号 HOME=/ 每小时执行/etc/cron.hourly内的脚本01 * * * * root run-parts /etc/cron.hourly每天执行/etc/cron.daily内的脚本0 24 * * * root run-parts /etc/cron.daily 每星期执行/etc/cron.weekly内的脚本22 4 * * 0 root run-parts /etc/cron.weekly 每月去执行/etc/cron.monthly内的脚本 42 4 1 * * root run-parts /etc/cron.monthly 注意: "run-parts"这个参数了,如果去掉这个参数的话,后面就可以写要运行的某个脚本名,而不是文件夹名。   每天的下午4点、5点、6点的5 min、15 min、25 min、35 min、45 min、55 min时执行命令。 5,15,25,35,45,55 16,17,18 * * * command每周一,三,五的下午3:00系统进入维护状态,重新启动系统。 00 15 * * 1,3,5 shutdown -r +5每小时的10分,40分执行用户目录下的innd/bbslin这个指令: 10,40 * * * * innd/bbslink 每小时的1分执行用户目录下的bin/account这个指令: 1 * * * * bin/account

#######################################################

grep命令扩展

  匹配空行:  ^$

[root@server0 ~]#grep -Pv "^(#|$)" /etc/dnsmasq.conf   //去除注释和空行

[root@server0 ~]# grep -v '^$'  /etc/default/useradd

# useradd defaults file GROUP=100 HOME=/home INACTIVE=-1 EXPIRE= SHELL=/bin/bash SKEL=/etc/skel CREATE_MAIL_SPOOL=yes   显示一个/etc/login.defs 配置文件有效配置(去掉以#开头的 注释   去掉空行) [root@server0 ~]# grep -v  '^#' /etc/login.defs[root@server0 ~]# grep -v  '^#' /etc/login.defs     |  grep -v '^$'   # grep -v  '^#' /etc/login.defs     |   grep -v '^$'  |   cat -n ############################################################### find 按条件查找文件 • 根据预设的条件递归查找对应的文件 – find [目录] [条件1] [-a|-o] [条件2] ... – 常用条件表示:   -type 类型(f文件、d目录、l快捷方式)   -name "文档名称"   -size +|-文件大小(k、M、G)   -user 用户名[root@server0 ~]# find /boot/ -type l     #查找是快捷方式 /boot/grub/menu.lst [root@server0 ~]# ls -l /boot/grub/menu.lst  [root@server0 ~]# find /boot/ -type d     #查找是目录 [root@server0 ~]# find /boot/ -type f     #查找是文本文件 请显示/etc目录下以 .conf结尾的?(包含子目录)[root@server0 ~]# find /etc  -name  "*.conf"  请显示/etc目录下以 .conf结尾的?(不包含子目录)[root@server0 ~]# ls /etc/*.conf [root@server0 ~]# mkdir /root/nsd1802 [root@server0 ~]# touch /root/nsd01.txt [root@server0 ~]# touch /root/nsd18.txt [root@server0 ~]# find /root/ -name "nsd*" [root@server0 ~]# find /root/ -name "nsd*"      -type f [root@server0 ~]# find /root/ -name "nsd*"      -type d -size +|-文件大小(k、M、G) [root@server0 ~]# find /boot/ -size +10M [root@server0 ~]# find /boot/ -size +300k [root@server0 ~]# find /boot/ -size -10M  -user 用户名[root@server0 ~]# find / -user student  -type f [root@server0 ~]# useradd wangwu [root@server0 ~]# find / -user wangwu    [root@server0 ~]# find / -user wangwu  -type d   #############################################################  find结果处理 [root@server0 ~]# rm -rf /opt/* [root@server0 ~]# ls /opt/ [root@server0 ~]# find /boot/ -size +300k • 使用find命令的 -exec 操作 – find .. .. -exec 处理命令 {} \; – 优势:以 {} 代替每一个结果,逐个处理,遇 \; 结束# find /boot/ -size +300k    -exec cp -r {} /opt   \; # ls /opt/ # find  /   -user student  -type f    # mkdir /root/findfile # find  /   -user student  -type f  -exec cp {}  /root/findfile \; # ls /root/findfile

################################################################

案例练习,准备:             rht-vmctl  reset  classroom             rht-vmctl  reset  server    rht-vmctl  reset  desktop ######################################################################## 案例1:为虚拟机 server0 配置以下静态地址参数 – 主机名:server0.example.com [root@server0 ~]# vim /etc/hostname – IP地址:172.25.0.11 – 子网掩码:255.255.255.0 – 默认网关:172.25.0.254 [root@server0 ~]# nmcli connection modify 'System eth0'  ipv4.method manual ipv4.addresses '172.25.0.11/24 172.25.0.254' connection.autoconnect yes [root@server0 ~]# nmcli connection up 'System eth0' – DNS服务器:172.25.254.254 [root@server0 ~]# vim /etc/resolv.conf  nameserver 172.25.254.254 案例2:为虚拟机 desktop0 配置以下静态地址参数 – 主机名:desktop0.example.com [root@desktop0 ~]# vim /etc/hostname – IP地址:172.25.0.10 – 子网掩码:255.255.255.0 – 默认网关:172.25.0.254

[root@desktop0 ~]# nmcli connection modify 'System eth0'   ipv4.method manual ipv4.addresses '172.25.0.10/24 172.25.0.254' connection.autoconnect yes

        [root@server0 ~]# nmcli connection up 'System eth0'    

– DNS服务器:172.25.254.254 [root@desktop0 ~]# vim /etc/resolv.conf  案例3:指定yum软件源 为 server0 指定可用的 yum 软件源 – YUM软件库的地址为 http://classroom.example.com/content/rhel7.0/x86_64/dvd [root@server0 ~]# vim /etc/yum.repos.d/dvd.repo – 将此配置为虚拟机 server0 的默认软件仓库 – 确认可用的仓库列表 [root@server0 ~]# yum repolist  – 利用yum仓库安装httpd与vsftpd [root@server0 ~]# yum install httpd [root@server0 ~]# yum -y install vsftpd 案例4:指定yum软件源 为 desktop0 指定可用的 yum 软件源 – YUM软件库的地址为 http://classroom.example.com/content/rhel7.0/x86_64/dvd [root@desktop0 ~]# vim /etc/yum.repos.d/dvd.repo – 将此配置为虚拟机 server0 的默认软件仓库 [rhel] name=rhel7.0 baseurl=http://classroom.example.com/content/rhel7.0/x86_64/dvd enabled=1 gpgcheck=0 – 确认可用的仓库列表 [root@desktop0 ~]# yum repolist  – 利用yum仓库安装httpd与vsftpd [root@desktop0 ~]# yum -y install httpd [root@desktop0 ~]# yum -y install vsftpd 案例5:虚拟机 server0上操作,复制、粘贴、移动

 以root用户新建/nsddir/目录,在此目录下新建readme.txt文件,并进一步完成下列操作

 1)将“I love Linux”写入到文件readme.txt  [root@desktop0 nsddir]# vim readme.txt  2)将readme.txt重命名为mylove.txt [root@desktop0 nsddir]# mv readme.txt mylove.txt  3)将/etc/passwd、/boot、/etc/group同时拷贝到/nsddir目录下 [root@desktop0 nsddir]# cp -r /etc/passwd /boot/ /etc/group /nsddir/  4)将ifconfig命令的前两行内容,追加写入mylove.txt [root@desktop0 nsddir]# ifconfig |head -2 >> mylove.txt   5)将主机名永久配置文件,拷贝到/nsddir目录下 [root@desktop0 nsddir]# cp /etc/hostname /nsddir/  6)将DNS永久配置文件,拷贝到/nsddir目录下 [root@desktop0 nsddir]# cp /etc/resolv.conf /nsddir/ 案例6:虚拟机Server上操作:复制、删除、移动及vim文本编辑器   1. 新建目录结构/nsd/test [root@desktop0 nsddir]# mkdir -p /nsd/test   2. 在目录/nsd/test创建文件nsd.txt并写入内容 NSD  Student [root@desktop0 test]# vim nsd.txt   3. 将/nsd/test/nsd.txt文件复制到/root目录下,同时 改名为 tedu.txt [root@desktop0 test]# cp /nsd/test/nsd.txt /root/tedu.txt   4. 利用vim 修改文件/etc/hostname将其原有内容全部删除,写入新的内容为server0.example.com [root@desktop0 ~]# vim /etc/hostname    5. 将/etc/passwd 、/etc/resolv.conf、/etc/hostname 同时拷贝到/nsd/test/目录下 [root@desktop0 ~]# cp -r /etc/passwd /etc/resolv.conf /etc/hostname /nsd/test/   6. 将文件 /nsd/test/hostname 重改名为 hn.txt  [root@desktop0 test]# mv /nsd/test/hostname /nsd/test/hn.txt   7. 创建目录结构/nsd/test/kernel [root@desktop0 test]# mkdir kernel   8. 将目录 /boot内容中以 vm 开头的 复制到/nsd/test/kernel目录下  [root@desktop0 test]# find /boot/ -name 'vm*' -exec cp {} /nsd/test/kernel \; 案例7:虚拟机 server0上操作,查找并处理文件  – 利用find查找所属用户 student 拥有的必须是文件,把它们拷贝到 /root/findfiles/ 文件夹中[root@desktop0 findfiles]# find / -user student -type f -exec cp {} /root/findfiles/ \;  – 利用find查找/boot目录下大于10M并且必须是文件,拷贝到/opt [root@desktop0 findfiles]# find /boot/ -size +10M  – 利用find查找/boot/ 目录下以 vm 开头且必须是文件,拷贝到/opt [root@desktop0 findfiles]# find /boot/ -name 'vm*' -type f -exec cp {} /opt/ \;  – 利用find查找/boot/ 目录下为快捷方式 [root@desktop0 opt]# find /boot/ -type l  – 利用find查找/etc 目录下,以 tab 作为结尾的 必须是文件 [root@desktop0 grub]# find /etc/ -name '*tab' -type f 案例8:虚拟机 server0上操作,查找并提取文件内容 1.在文件 /usr/share/dict/words 中查找到所有包含字符串 seismic 的行,将输出信息,写入到/opt/nsd18.txt [root@desktop0 grub]# grep 'seismic' /usr/share/dict/words > /opt/nsd18.txt 2.查看内核版本,将显示结果重定向到/root/version.txt [root@desktop0 opt]# uname -r > /root/version.txt 3.查看红帽系统版本,将显示结果追加到/root/version.txt [root@desktop0 ~]# cat /etc/redhat-release  >> /root/version.txt  4.查看主机名将显示结果追加到/root/version.txt [root@desktop0 ~]# hostname >> /root/version.txt  5.将/etc/fstab文件中以UUID开头的信息,写入到/root/fstab.txt [root@desktop0 ~]# grep '^UUID' /etc/fstab >> /root/fstab.txt 6.提取/etc/passwd以bash结尾的行,将其信息写入/opt/pass.txt [root@desktop0 ~]# grep 'bash$' /etc/passwd >> /opt/pass.txt 7. 复制/etc/login.defs文件到当前目录下,改名为init.txt [root@desktop0 ~]# cp /etc/login.defs /root/init.txt 8.提取init.txt文件里的有效配置(去除以#号开头,去除空行),保存为init2.txt[root@desktop0 ~]# grep -v '^#' init.txt | grep -v '^$' > init2.txt 案例9:虚拟机 server0上操作,tar制作/释放归档压缩包(zcf、ztf、zxf、jcf、jtf、jxf、cf、tf)     1)备份/boot、/home这两个文件夹,保存为boothome.tar.gz文件

[root@desktop0 ~]# tar -zcf /opt/boothome.tar.gz /boot/ /home/

    2)查看boothome.tar.gz文件内包含哪些内容 

[root@desktop0 opt]# tar -tf boothome.tar.gz 

    3)将boothome.tar.gz释放到文件夹/root/boothome/下

        [root@desktop0 opt]# tar -xf /opt/boothome.tar.gz -C /root/boothome/

    4)创建一个名为 /root/backup.tar.bz2 的归档文件,其中包含 /usr/local 目录中的内容 [root@desktop0 boothome]# tar -jcf /root/backup.tar.bz2 /usr/local/ 案例10:虚拟机 server0上操作 • 新建用户 alex,其用户ID为3456,密码是flectrag  [root@desktop0 ~]# useradd -u 3456 alex [root@desktop0 home]# passwd alex  • 创建下列用户、组以及组的成员关系:  – 一个名为 adminuser 的组  [root@desktop0 ~]# groupadd adminuser – 一个名为 natasha 的用户,其属于 adminuser 组, 这个组是该用户的从属组  [root@desktop0 ~]# useradd -G adminuser natasha – 一个名为 harry 的用户,其属于 adminuser 组,这个 组是该用户的从属组  [root@desktop0 ~]# useradd -G adminuser harry – 一个名为 sarah 的用户,其在系统中没有可交互的 Shell(/sbin/nologin),并且不是 adminuser 组的成员  [root@desktop0 home]# useradd -s /sbin/nologin sarah – natasha 、harry、sarah 的密码都要设置为 flectra [root@desktop0 home]# echo flectra | passwd --stdin natasha          更改用户 natasha 的密码 。         passwd:所有的身份验证令牌已经成功更新。         [root@desktop0 home]# echo flectra | passwd --stdin harry          更改用户 harry 的密码 。         passwd:所有的身份验证令牌已经成功更新。         [root@desktop0 home]# echo flectra | passwd --stdin sarah         更改用户 sarah 的密码 。         passwd:所有的身份验证令牌已经成功更新。 案例11:组账号基本管理     1)新建组账号stugrp [root@desktop0 home]# groupadd stugrp     2)创建用户lily、zhangsan [root@desktop0 home]# useradd lily [root@desktop0 home]# useradd zhangsan     3)为stugrp组添加三个成员用户(lily、root、zhangsan) [root@desktop0 home]# gpasswd -a lily stugrp [root@desktop0 home]# gpasswd -a root stugrp [root@desktop0 home]# gpasswd -a zhangsan stugrp     4)从stugrp组删除一个成员(lily) [root@desktop0 home]# gpasswd -d lily stugrp 案例12:配置NTP网络时间客户端

[root@desktop0 home]# yum -y install chrony   1.安装软件

配置虚拟机 server0,自动校对系统时间

        NTP服务器位于 classroom.example.com 

[root@desktop0 home]# vim /etc/chrony.conf    2.修改配置文件 server classroom.example.com iburst 此客户机的时间与NTP服务器的时间保持同步 [root@desktop0 home]# systemctl restart chronyd.service  3.重启服务   [root@desktop0 home]# systemctl enable chronyd             4.将服务设置为开机自启动    案例13:虚拟机 server0上操作     为用户 natasha 配置一个定时任务  – 每天在本地时间 14:23 执行 – 需要完成的任务操作为 /bin/echo  hiya [natasha@server0 home]$ crontab -e 23 14 * * * /bin/echo hiya

转载请注明原文地址: https://www.6miu.com/read-2612682.html

最新回复(0)