ubuntu编写shell并做成定时任务脚本

  1. 创建shell脚本
#创建文件夹
mkdir /usr/local/shell
#创建并编辑脚本
vim test.sh

脚本内容
此脚本命令为清除linux系统buff/cache缓冲区命令

#! /bin/bash
  
echo "1" > /proc/sys/vm/drop_caches
echo "2" > /proc/sys/vm/drop_caches
echo "3" > /proc/sys/vm/drop_caches

测试脚本是否能执行

#使脚本具有执行权限
chmod +x ./test.sh  
#执行脚本
./test.sh  
  1. 安装cron
apt-get install cron

相关命令

#查看运行状态
systemctl status cron
#重启
systemctl restart cron
#启动
systemctl start cron
#停止
systemctl stop cron
  1. 设置定时任务
    编写定时任务
#设定定时任务
#crond定时方式的配置
vim /etc/crontab 
#在最后一行加上定时任务的配置 每天0点执行一次
0  0    * * *   root    /usr/local/shell/test.sh

设置好后需要重启cron

#修改crontab的配置后需要重启crontab,使配置生效
systemctl restart cron
#检测cron定时服务是否自启用
systemctl is-enabled cron
#enable表示已启用自启动
#disable标识未启用自启动
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值