Ubuntu crontab 定时执行.sh脚本并输出内容写到日志

本文介绍了如何在Ubuntu系统中使用crontab设置定时任务,包括启动和停止cron服务,查看和编辑crontab配置,以及编写定时执行.sh脚本并将其输出内容写入日志的详细步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

cron是一个Linux定时执行工具,要把cron设为在开机的时候自动启动,在 /etc/rc.d/rc.local 脚本中加入 /sbin/service crond start

cron 服务的启动与停止,命令如下:

  1.  service cron start  /*启动服务*/
  2.  service cron stop /*关闭服务*/
  3. service cron restart /*重启服务*/
  4. service cron reload /*重新载入配置*/
  5. service cron status /*查看crond状态*/ 

crontab命令

  1. 查看当前用户的crontab,输入 crontab -l;
  2. 编辑crontab,输入 crontab -e;
  3. 删除crontab,输入 crontab -r

crontab文件示例

  1. 30 21 * * * /usr/local/etc/rc.d/lighttpd restart 
  2.  上面的例子表示每晚的21:30重启apache
  3.  45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart 
  4.  上面的例子表示每月1、10、22日的4 : 45重启apache
  5.  10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart 
  6.  上面的例子表示每周六、周日的1 : 10重启apache
  7.  0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart 
  8.  上面的例子表示在每天18 : 00至23 : 00之间每隔30分钟重启apache
  9.  0 23 * * 6 /usr/local/etc/rc.d/lig
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值