crontab timing task does not execute in Linux

Recently, I encountered a case of crontab timing task not executing in linux. I'd like to share it with you to avoid stepping into the pit I encountered.

Post the script first

In order to facilitate the display, the script has been written into the parameter

#!/usr/bin/env bash
# 1 output file to log for example: bg 
# 2 the destination file directory does not have the last / for example, / opt / flume / home / HDP ﹐ LBG ﹐ ectech / rawdata / xxx / Web ﹐ VIP ﹐ BG
# 3. When executing, select the logs of the previous days, for example: 1

LAST_DAY=$(date +"%Y-%m-%d" -d "1 days ago")
LAST_HOUR_DAY=$(date +"%Y%m%d" -d "1 days ago")
#Execute jar to get bg log
 java -jar /opt/script/xxx/vipbg/vip-bg-job-1.0-SNAPSHOT-shaded.jar  /opt/script/xxx/vipbg/bg.log

#Specific to file name
SRC_FILE=/opt/script/xxx/vipbg/bg.log

#Specify target directory, create directory by date
DEST_DIR_NAME=/opt/flume/home/hdp_lbg_ectech/rawdata/xxx/web_vip_bg/$LAST_HOUR_DAY
DEST_FILE_NAME=$(basename $SRC_FILE)
#Flush ignores hidden files, so you can copy them directly
TMP_FILE=$DEST_DIR_NAME/.$DEST_FILE_NAME
DEST_FILE=$DEST_DIR_NAME/$DEST_FILE_NAME.$SRC_IP
#Determine whether the source file is not empty. The empty file Flume will be deleted directly, and the target file does not exist, so as to avoid duplicate copying of data
if [ -s "$SRC_FILE" ] && [ ! -f "$DEST_FILE" ];then
    #Make flume have the permission to delete files in this directory, otherwise flume cannot be collected
    mkdir -p $DEST_DIR_NAME && chmod 777 $DEST_DIR_NAME
    #You must cp to a temporary file and then mv. Direct cp will cause data loss
    cp $SRC_FILE $TMP_FILE && mv $TMP_FILE $DEST_FILE
fi
#Delete tmp transit file
rm -rf  /opt/script/xxx/vipbg/bg.log
echo "Delete source file succeeded"

Some directories are represented by xxx
This script can be executed after being tested separately. When it is put into crontab, it will not be executed. After Baidu, most of them say that absolute path needs to be used. After reading our script, there is a problem in the following sentence:

 java -jar /opt/script/xxx/vipbg/vip-bg-job-1.0-SNAPSHOT-shaded.jar  /opt/script/xxx/vipbg/bg.log

The preliminary judgment is that java needs to use the directory path of jdk, and two experiments are done.

Add a java -jar xxxx.jar timing task to crontab

The code is as follows:

10 16 * * *  java -jar /opt/script/xxx/vipbg/vip-bg-job-1.0-SNAPSHOT-shaded.jar  /opt/script/xxx/vipbg/bg.log

As expected, this was not implemented

Add a timing task of jdk directory / bin/java -jar xxxx.jar in crontab

The code is as follows:

20 16 * * *  /opt/soft/jdk/jdk1.6.0_45/bin/java -jar /opt/script/xxx/vipbg/vip-bg-job-1.0-SNAPSHOT-shaded.jar  /opt/script/xxx/vipbg/bg.log

This time, the reason for no execution is that when executing jar, the path of jdk needs to be added

Do you have similar problems? Welcome to exchange, point out some mistakes in the article, let me deepen my understanding, share the holes I have encountered, and let us have no bug s together. Thank you!

Tags: Java crontab snapshot JDK

Posted on Thu, 02 Apr 2020 15:24:01 -0700 by playa4real