linux下设置定时执行脚本

﹏ヽ暗。殇╰゛Y 2022-03-17 09:12 543阅读 0赞

linux下设置定时执行脚本

1.首先安装所需程序并启动
crontabs是设置周期性被执行的指令

  1. yum install vixie-cron
  2. yum install crontabs
  3. service crond start
  4. service crond stop
  5. service crond status

出现以下信息则表示crond启动成功

  1. crond.service - Command Scheduler
  2. Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
  3. Active: active (running) since Fri 2019-02-15 02:13:57 EST; 1 weeks 0 days ago
  4. Main PID: 94183 (crond)
  5. Tasks: 1
  6. Memory: 344.0K
  7. CGroup: /system.slice/crond.service
  8. └─94183 /usr/sbin/crond -n

2.将crond加入开机启动
就是把service crond start加入开机启动脚本
开机启动脚本可以参考我的另一篇博客:https://blog.csdn.net/qq_41969358/article/details/87798908

3.添加定时任务
vi /etc/crontab,例如设置每天晚上12点重启服务器
tip:定时任务失败很多时候都是crontab时间格式错误

  1. SHELL=/bin/bash
  2. PATH=/sbin:/bin:/usr/sbin:/usr/bin
  3. MAILTO=root
  4. # For details see man 4 crontabs
  5. # Example of job definition:
  6. # .---------------- minute (0 - 59)
  7. # | .------------- hour (0 - 23)
  8. # | | .---------- day of month (1 - 31)
  9. # | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
  10. # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
  11. # | | | | |
  12. # * * * * * user-name command to be executed
  13. 0 0 * * * root reboot

4.添加完定时任务后,重启crond,使改动生效

  1. service crond restart

5.查看是否生效
实时查看crontab执行内容

  1. tail -f /var/log/cron

通过系统日志来查看Cron任务是否有执行过

  1. grep "reboot" /var/log/cron

发表评论

表情:
评论列表 (有 0 条评论,543人围观)

还没有评论,来说两句吧...

相关阅读

    相关 linux定时执行shell脚本

    好久木有来更新了,下面来记录并分享下在linux下是如何实现定时执行shell脚本的: 一、写一个shell测试脚本 shell脚本命名为test.sh,具体实现如下所示