linux常用命令:定时计划,修改文件内容

1, 定时任务管理: crontab

crontab -e : 此命令会打开一个定时任务管理文件,格式为

#使用命名参数: crontab
# -u: 指定某用户
# -r:  删除该用户所有的定时任务
# -l: 列出该用户所有的定时任务
# -e: 编辑该用户的定时任务


#每天23:00  定时关机
#格式:分  时   天         月    周几           具体命令
#	  0   23   *          *     *             sudo  poweroff

[root@test-c6 ~]# man cron
crond 在/etc/rc.d/init.d/crond里面配置开机启动,
通过任务文件的modtime或inotify机制来获取文件的改变,因而改变任务文件不需要重启服务
日志文件是:/var/log/cron
扫描的任务文件是:
	   /var/spool/cron: 每个用户定义的任务,以用户名创建文件来分开存放
	   /etc/anacrontab: 系统定时任务,每天/周/月的
	   /etc/cron.d:系统定时任务,格式和普通的cron不同(调用/etc/cron.hourly)
内置的时间值:
       @reboot    :    Run once after reboot.
       @yearly    :    Run once a year, ie  "0 0 1 1 *"
       @annually  :    Run once a year, ie  "0 0 1 1 *"
       @monthly   :    Run once a month, ie "0 0 1 * *"
       @weekly    :    Run once a week, ie  "0 0 * * 0"
       @daily     :    Run once a day, ie   "0 0 * * *"
       @hourly    :    Run once an hour, ie "0 * * * *"	  
       
[root@test-c6 ~]# cat /var/spool/cron/root
#每分钟执行
* * * * * echo every min

#每5分钟执行
*/5 * * * * echo every 5 min

#每小时执行
@hourly   	echo hourly 

#每小时的10分, 30分执行
10,30 * * * * echo every hour with some min:  10 and 30 min

#每小时的10分-30分执行
10-30 * * * * echo every hour with range min: 10 to 30 min

#每小时10-30分范围: 每5分钟执行
10-30/5 * * * * echo every hour with range min by step: 10,15,20,25,30

anacron: 新版的centos系统定时管理

[root@test-c6 ~]# rpm -qf /etc/anacrontab
cronie-anacron-1.4.4-16.el6_8.2.x86_64

[root@test-c6 ~]# man 8  anacron
和cron不一样,它假设服务器不是24小时运行的,也能保证定时任务被执行:每天/周/月的任务
它先通过 /var/spool/anacron/{cron.daily,cron.monthly,cron.weekly} 存储的时间(精确到天),即上次任务执行的时间,来判断该任务是否应该被执行
Anacron 每个活动作业最多使用两个文件描述符。如果超过 125 个活动符,它可能用完描述符

[root@test-c6 ~]# cat /var/spool/anacron/cron.daily
20201118


#配置文件
[root@test-c6 ~]# man anacrontab
RANDOM_DELAY:  最大延后执行的时间间隔(如果上一个任务未执行完,等待的最大时间值; 此时后面的任务都排成队列,等待调度)
       For example a RANDOM_DELAY set to 12 would therefore add,
       randomly, between 0 and 12 minutes to the user defined delay
       Jobs will be running in queue. After  one  finish, then next will start.
START_HOURS_RANGE:在什么时间范围内,执行任务   


[root@test-c6 ~]# cat /etc/anacrontab
# /etc/anacrontab: configuration file for anacron
# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22

#说明:比如delay设为5,则表示总的延迟时间是 5 minutes + RANDOM_DELAY 
#period in days   delay in minutes   job-identifier		 command
1					5				 cron.daily              nice run-parts /etc/cron.daily
7					25				 cron.weekly             nice run-parts /etc/cron.weekly
@monthly			45				 cron.monthly            nice run-parts /etc/cron.monthly


[root@test-c6 ~]# cat /etc/cron.daily/logrotate
#!/bin/sh
/usr/sbin/logrotate /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0

2,修改文件内容

a, cat 命令实现

[root@s101 txt]# cat a.txt 
a
[root@s101 txt]# cat >a.txt <<eof  #覆盖
> 123
> eof
[root@s101 txt]# cat a.txt 
123

[root@s101 txt]# cat >>a.txt <<eof #追加
> uuu
> eof
[root@s101 txt]# cat a.txt 
123
uuu

b, tee命令实现

wang@wang-pc:~/txt$ cat a.txt 
a
wang@wang-pc:~/txt$ tee a.txt <<eof  #覆盖
123
456
eof

wang@wang-pc:~/txt$ cat a.txt 
123
456

c, echo命令实现

[root@s101 txt]# cat a.txt 
a
[root@s101 txt]# echo -e "a\nb">a.txt  #覆盖
[root@s101 txt]# cat a.txt 
a
b

[root@s101 txt]# echo -e "c\nd">> a.txt  #追加
[root@s101 txt]# cat a.txt 
a
b
c
d

d, sed命令

#全部替换
sed -i 's/源字符/目标字符/g'   文件名
#指定行替换
sed -i  '1,6 s/源字符/目标字符/g'   文件名

#指定内容替换
wang@wang-pc:~$ cat err.txt 
SELINUX=yes
SELINUX=yes2
wang@wang-pc:~$ sed '/^SELINUX=/ cSELINUX=no' err.txt 
SELINUX=no
SELINUX=no
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

根哥的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值