Ubuntu使用crontab设置定时任务

Posted Christine

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ubuntu使用crontab设置定时任务相关的知识,希望对你有一定的参考价值。

crontab常用命令

service cron start 开启服务

service cron stop 关闭服务

service cron restart 重启服务

service cron reload 重新载入配置

1 创建crontab任务

1.1 service cron status 查看定时任务是否开启

(base) root@node3:~# service cron status
● cron.service - Regular background program processing daemon
   Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: ena
   Active: active (running) since Wed 2019-10-30 14:05:22 CST; 2 months 14 days
     Docs: man:cron(8)
Main PID: 637 (cron)
   CGroup: /system.slice/cron.service
           ├─  637 /usr/sbin/cron -f
           ├─21320 /titan/agent/titanagent -d
           └─21322 titan_monitor -p 21320 -l 50 -c 150 -g 0

Jan 13 13:52:01 node3 CRON[30126]: (root) CMD (/bin/bash /etc/titanagent/agent_m
Jan 13 13:52:01 node3 CRON[30125]: pam_unix(cron:session): session opened for us
Jan 13 13:52:01 node3 CRON[30127]: (root) CMD (/usr/local/easyops/agent/bin/moni
Jan 13 13:52:01 node3 CRON[30128]: (root) CMD (/bin/bash /etc/titanagent/agent_u
Jan 13 13:52:01 node3 CRON[30124]: pam_unix(cron:session): session closed for us
Jan 13 13:52:01 node3 CRON[30123]: pam_unix(cron:session): session closed for us
Jan 13 13:52:05 node3 CRON[30125]: pam_unix(cron:session): session closed for us
Jan 13 13:53:01 node3 CRON[30350]: pam_unix(cron:session): session opened for us
Jan 13 13:53:01 node3 CRON[30351]: (root) CMD (/usr/local/easyops/agent/bin/moni
Jan 13 13:53:01 node3 CRON[30350]: pam_unix(cron:session): session closed for us
lines 1-20/20 (END)...skipping...
● cron.service - Regular background program processing daemon
   Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2019-10-30 14:05:22 CST; 2 months 14 days ago
     Docs: man:cron(8)
Main PID: 637 (cron)
   CGroup: /system.slice/cron.service
           ├─  637 /usr/sbin/cron -f
           ├─21320 /titan/agent/titanagent -d
           └─21322 titan_monitor -p 21320 -l 50 -c 150 -g 0

Jan 13 13:52:01 node3 CRON[30126]: (root) CMD (/bin/bash /etc/titanagent/agent_monitor.sh >> /var/log/titanagent/edog.o.log 2>> /var/log/titanagent/edog.e.log)
Jan 13 13:52:01 node3 CRON[30125]: pam_unix(cron:session): session opened for user root by (uid=0)
Jan 13 13:52:01 node3 CRON[30127]: (root) CMD (/usr/local/easyops/agent/bin/monitor.sh  >> /usr/local/easyops/agent/log/easyops.log 2>&1)
Jan 13 13:52:01 node3 CRON[30128]: (root) CMD (/bin/bash /etc/titanagent/agent_update_exception.sh >> /var/log/titanagent/check.o.log 2>> /var/log/titanagent/check.e.log)
Jan 13 13:52:01 node3 CRON[30124]: pam_unix(cron:session): session closed for user root
Jan 13 13:52:01 node3 CRON[30123]: pam_unix(cron:session): session closed for user root
Jan 13 13:52:05 node3 CRON[30125]: pam_unix(cron:session): session closed for user root
Jan 13 13:53:01 node3 CRON[30350]: pam_unix(cron:session): session opened for user root by (uid=0)
Jan 13 13:53:01 node3 CRON[30351]: (root) CMD (/usr/local/easyops/agent/bin/monitor.sh  >> /usr/local/easyops/agent/log/easyops.log 2>&1)
Jan 13 13:53:01 node3 CRON[30350]: pam_unix(cron:session): session closed for user root

1.2 crontab -e 编辑用户的crontab文件的内容

*/2 * * * * /BigData/workspace/monitorCPU.py &  #每2分钟执行一次python任务(ctrl+c ctrl+x Y enter)

1.3 crontab -l 显示用户的crontab文件的内容

(base) root@node3:~# crontab -l
20 */2 * * * /bin/bash /etc/titanagent/agent_update.sh >> /var/log/titanagent/check.o.log 2>> /var/log/titanagent/check.e.log
*/2 * * * * /bin/bash /etc/titanagent/agent_update_exception.sh >> /var/log/titanagent/check.o.log 2>> /var/log/titanagent/check.e.log
*/2 * * * * /bin/bash /etc/titanagent/agent_monitor.sh >> /var/log/titanagent/edog.o.log 2>> /var/log/titanagent/edog.e.log
* * * * * /usr/local/easyops/agent/bin/monitor.sh  >> /usr/local/easyops/agent/log/easyops.log 2>&1
*/2 * * * * /BigData/workspace/monitorCPU.py &

2 日志服务

2.1 修改rsyslog

sudo vim /etc/rsyslog.d/50-default.conf
cron.* /var/log/cron.log #将cron前面的注释符去掉

2.2 重启rsyslog

sudo service rsyslog restart

2.3 查看crontab日志

less /var/log/cron.log

注意: 这里必须重启日志服务,重启以后使用less查看可能还没有日志,需要等任务执行以后才能看到日志。

3 查看日志

(base) root@node3:~# cat /var/log/cron.log

Jan 13 14:51:01 node3 CRON[6080]: (root) CMD (/usr/local/easyops/agent/bin/monit
Jan 13 14:52:01 node3 CRON[6156]: (root) CMD (/usr/local/easyops/agent/bin/monit
Jan 13 14:52:01 node3 CRON[6157]: (root) CMD (/BigData/workspace/monitorCPU.py &
Jan 13 14:52:01 node3 CRON[6161]: (root) CMD (/bin/bash /etc/titanagent/agent_up
Jan 13 14:52:01 node3 CRON[6160]: (root) CMD (/bin/bash /etc/titanagent/agent_mo
Jan 13 14:53:01 node3 CRON[6354]: (root) CMD (/usr/local/easyops/agent/bin/monit
Jan 13 14:54:01 node3 CRON[6434]: (root) CMD (/BigData/workspace/monitorCPU.py &
Jan 13 14:54:01 node3 CRON[6438]: (root) CMD (/bin/bash /etc/titanagent/agent_mo
Jan 13 14:54:01 node3 CRON[6436]: (root) CMD (/bin/bash /etc/titanagent/agent_up
Jan 13 14:54:01 node3 CRON[6437]: (root) CMD (/usr/local/easyops/agent/bin/monit
Jan 13 14:55:01 node3 CRON[6689]: (root) CMD (command -v debian-sa1 > /dev/null
Jan 13 14:55:01 node3 CRON[6690]: (root) CMD (/usr/local/easyops/agent/bin/monit
Jan 13 14:56:01 node3 CRON[6766]: (root) CMD (/usr/local/easyops/agent/bin/monit
Jan 13 14:56:01 node3 CRON[6765]: (root) CMD (/BigData/workspace/monitorCPU.py &
Jan 13 14:56:01 node3 CRON[6767]: (root) CMD (/bin/bash /etc/titanagent/agent_mo
Jan 13 14:56:01 node3 CRON[6770]: (root) CMD (/bin/bash /etc/titanagent/agent_up

crontab -e 修改后:

sudo service rsyslog restart #重启rsyslog

service cron restart #重启服务

实例:crontab定时JAVA任务

在class所在文件目录下创建java.sh脚本

#!/bin/bash

#java环境目录
export JAVA_HOME=/opt/java/jdk1.8.0_181   
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
# kafka环境目录
export KAFKA_HOME=/opt/kafka/kafka_2.11-0.10.2.2
export PATH=$KAFKA_HOME/bin:$PATH
export CLASSPATH=.://opt/kafka/kafka_2.11-0.10.2.2/libs/*
# class文件所在目录
cd /opt/workspace/Demo/src/main/java
# 需要执行的命令
java KafkaTest

注意:
不添加class文件所在目录报错如下:

Error: Could not find or load main class KafkaTest

不添加kafka环境目录报错如下:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/kafka/clients/consumer/KafkaConsumer
    at KafkaConsumerTest6.main(KafkaConsumerTest6.java:45)
Caused by: java.lang.ClassNotFoundException: org.apache.kafka.clients.consumer.KafkaConsumer
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

编辑crontab -e

*/2 * * * * /opt/workspace/Demo/src/main/java/java.sh >> /opt/Data/out.log 2>&1 #每2分钟执行一次

重启

sudo service rsyslog restart  #重启rsyslog
service cron restart  #重启服务

kill掉进程日志如下:

/opt/workspace/Demo/src/main/java/java.sh: line 12: 20691 Killed                  java KafkaTest
/opt/workspace/Demo/src/main/java/java.sh: line 12: 21016 Killed                  java KafkaTest
/opt/workspace/Demo/src/main/java/java.sh: line 12: 20379 Killed                  java KafkaTest
/opt/workspace/Demo/src/main/java/java.sh: line 12: 20079 Killed                  java KafkaTest

以上是关于Ubuntu使用crontab设置定时任务的主要内容,如果未能解决你的问题,请参考以下文章

ubuntu crontab设置定时任务

ubuntu中执行定时任务crontab

linux设置定时任务crontab

Ubuntu 使用crontab定时任务

ubuntu 设置定时任务

docker(ubuntu)中安装cron运行crontab定时任务