Python 没有最近(<;24小时)登录,CRON作业无法运行--超出基本故障排除范围 背景:

Python 没有最近(<;24小时)登录,CRON作业无法运行--超出基本故障排除范围 背景:,python,bash,cron,Python,Bash,Cron,我编写了一个bash脚本,查询存储块列表,调用Python3分析查询结果,然后装载一个驱动器。我想在每天早上运行rsnapshot之前运行脚本,所以我将脚本安排在/etc/cron.d/中的crontab中 症状: 我用“sudobash checkmount.bash”测试了bash脚本,它运行时没有错误 我已计划在登录时使用/etc/cron.d/中的cron选项卡运行bash脚本,并且它已完成 我已经安排bash脚本在我注销时使用/etc/cron.d/中的同一个cron选项卡运行,它完

我编写了一个bash脚本,查询存储块列表,调用Python3分析查询结果,然后装载一个驱动器。我想在每天早上运行rsnapshot之前运行脚本,所以我将脚本安排在/etc/cron.d/中的crontab中

症状:
  • 我用“sudobash checkmount.bash”测试了bash脚本,它运行时没有错误
  • 我已计划在登录时使用/etc/cron.d/中的cron选项卡运行bash脚本,并且它已完成
  • 我已经安排bash脚本在我注销时使用/etc/cron.d/中的同一个cron选项卡运行,它完成了
  • 我已计划在注销时使用/etc/cron.d/中的相同cron选项卡运行bash脚本,但在2-3天后检查时,它根本没有运行。
故障排除:
  • 我确保bash脚本和python3脚本位于/usr/local/bin/,root(运行脚本的用户)可以访问该脚本。这些脚本由root拥有,并且具有rwx权限
  • 我选中了“grepcron/var/log/syslog”和“grepcron/var/log/syslog/”。虽然我注销了2-3天,但cron甚至没有出现在syslog中。如果cron在我登录时运行,则cron将显示在日志中,并且不会报告任何错误
  • 我确保bash脚本中引用的文件包含完整路径+文件名。运行脚本的root用户可以访问路径和文件名
  • 我将stdout和stderr指向一个文件,我可以稍后查看。如果我在CRON运行时登录,它只有我写入脚本的输出,没有错误。如果我在CRON应该运行时注销,则该文件没有输出
文件夹:
  • bash脚本位于/usr/local/bin/中。文件的权限为:-rwxr-xr-x 1 root Bash脚本:
  • cron选项卡位于/etc/cron.d/中。我没有更改它的权限。 cron选项卡:
帮忙? 当我注销时,如何获得有关cron的更多信息?当我长时间注销时,它甚至没有显示在/var/log/syslog中。我应该发布整个/var/log/syslog吗

/var/log/cron.log 5月14日、5月13日、5月12日(5月14日,所有cron条目在我登录时运行,将时间调整为未来5-10分钟并注销;5月13日,我从未登录;5月12日,所有cron条目在我登录时运行,将时间调整为未来5-10分钟并注销)。
哪个bash
说了什么?在/usr/local/bin/errorCRON中没有输出吗?在调试时,您可以将运行频率设置为3分钟或每10分钟一次,这对您调试来说是合适的。另外,我不会将脚本的输出存储在
/usr/local/bin
中。它通常是为可执行文件保留的。Temp.files可以在/tmp中创建,当然root可以在root的主目录
/root
中存储文件。另外,您不是在
/mnt/externaldrive
中一个接一个地安装文件系统吗?
#!/usr/bin/bash
#list the devices in /dev in .mount1.txt
#Process the list, save devices that do not hold the LVM and are large enough to backup the LVM

lsblk --output=NAME,SIZE,TYPE --bytes > /usr/local/bin/.mount1.txt
python3 /usr/local/bin/validDisks.py /usr/local/bin/.mount1.txt /usr/local/bin/.mount2.txt

#Empty .mount3.txt
> /usr/local/bin/.mount3.txt

#Open .mount2.txt and for each line
# run lsblk and append output to .mount3.txt
input1=/usr/local/bin/.mount2.txt
while read line1
do
    echo $(lsblk $line1 --output=MOUNTPOINT -n) >> /usr/local/bin/.mount3.txt
    operand=$(< /usr/local/bin/.mount3.txt)
    if [[ -n $operand ]]
    then
        echo $line1 "is mounted at " $operand "."
    else
        echo $line1 "is not mounted."
        mount $line1 /mnt/externaldrive
        if [[ $? ]]
        then
            echo "mounted " $line1
        else
            echo "failed to mount"
        fi
    fi
done < $input1
lsblk
from pathlib import Path
import sys
import io
import re

###Method Definitions###

def makeRawFrame(filename):
    rawtargetframe = []
    print(".")
#Open the text data
    openfile = open(filename)
    print("..")
#Convert each row in the text data into a list of items delimited by whitespace
    for i in openfile:
        framerow=re.split("\s", i)
        print("...")
        rawtargetframe.append(framerow)
        print("....")
    openfile.close()
    print(".....")
    return rawtargetframe

def makeTrimFrame(frame):
#make the empty frame
    trimmedFrame = []
    print('.')
#iterate each row in the raw frame
    for i in frame: 
#make an empty row to add to the new frame
        trimmedRow = []
        print("..")
#iterate each item in the raw row
        for j in i: 
            print("...")
#if the item is non-empty, append to new row
            if j != '':
                trimmedRow.append(trimNonAN(j))
#when row is done reading, append to new frame
        trimmedFrame.append(trimmedRow)
    print("....")
    return trimmedFrame

def trimNonAN(str):
    alphanum = ''
    r = re.findall("[a-zA-Z0-9]",str)
    for i in r:
        alphanum=alphanum+i
    return alphanum

def trimNonA(str):
    alphabet = ''
    r = re.findall("[a-zA-Z]",str)
    for i in r:
        alphabet=alphabet+i
    return alphabet

def findLVM(frame):
    for i in frame:
        if i[2] == 'lvm': 
            LVMindex=frame.index(i)
            partindex=LVMindex-1
            LVMpart=frame[partindex][0]
    return LVMpart

def findLVMsize(frame):
    for i in frame:
        if i[2] == 'lvm': 
            LVMindex=frame.index(i)
            partindex=LVMindex-1
            LVMsize=int(frame[partindex][1])
    return LVMsize

def findAllParts(frame):
    parts=[]
    for i in frame:
        if i[2] == 'part':
            parts.append(trimNonAN(i[0]))
    return parts
    
def removeLVMpart(list, LVMpart):
    noLVMlist=[]
    print("Culling LVM from list.")
    for i in list: 
        print("... " + i + "... ")
        if trimNonA(i) != trimNonA(LVMpart):
            noLVMlist.append(i)
    for j in noLVMlist:
        print("Valid non-LVM partitions: " + j)
    return noLVMlist

def sizeCheck(frame, list, LVMsize):
    validParts=[]
    print("Checking valid partition for size to accomodate the LVM.")
    for i in frame:
        for j in list:
            if i[0] == j:
                print("Valid partition found. Checking size against LVM.")
                if int(i[1]) > LVMsize:
                    validParts.append(i[0])
                    print("Size adequate. " + i[0] + " appended.")
                else:
                    print("Size inadequate.")
#            else:
#                print("Not a valid partition.")
    return validParts

###MAIN SCRIPT###

#Load the file
raw=makeRawFrame(sys.argv[1])
#returns 2D LIST: Make a tidy frame with the drive names, sizes, and types
trim = makeTrimFrame(raw)
print("===============================================================================")
print("trim frame: ")
for i in trim:
    print(i)
print("===============================================================================")
#returns STRING: find the name of the drive/part holding the LVM
rawlvmpart=findLVM(trim)
lvmpart=trimNonAN(rawlvmpart)
print("LVM partition is " + lvmpart)
print("===============================================================================")
#returns INTEGER: find the size of the LVM
size=findLVMsize(trim)
#returns LIST: find the names of all disk partitions
allParts=findAllParts(trim)
#Look for the LVM disk/part in the disk list
#returns LIST: Remove the LVM disk/part from the disk list
onlyParts=removeLVMpart(allParts, lvmpart)
#Look for non-LVM disks in tidy frame and check size against LVM size
#returns LIST
validParts=sizeCheck(trim, onlyParts, size)
if len(validParts) > 0: 
    print("===============================================================================")
    print("Depositing list of valid backup targets.")
#Create a file with the name of the valid disk drive for backup
    outputfile = open(sys.argv[2], "w")
    for i in validParts:
        print("Partition Name: " + i)
        outputfile.write("/dev/" + i +"\n")
    outputfile.close()
    print("Operation complete. .mount2.txt is now updated.")
# To activate this cron file you have to uncomment the lines below.
# Feel free to adapt it to your needs.
00 11   * * *   root    /usr/bin/bash /usr/local/bin/checkmount.bash >> /usr/local/bin/errorCRON.log 2>&1
54 11   * * *   root    /usr/bin/rsnapshot alpha
# 30 3      * * *       root    /usr/bin/rsnapshot beta
# 0  3      * * 1       root    /usr/bin/rsnapshot gamma
# 30 2      1 * *       root    /usr/bin/rsnapshot delta
May 12 16:31:01 clientavnetwork CRON[17459]: (root) CMD (/usr/bin/rsnapshot alpha)
May 12 16:33:01 clientavnetwork cron[731]: (*system*mountandsync) RELOAD (/etc/cron.d/mountandsync)
May 12 16:39:01 clientavnetwork CRON[17503]: (root) CMD (  [ -x /usr/lib/php/sessionclean ] && if [ ! -d /run/systemd/system ]; then /usr/lib/php/sessionclean; fi)
May 12 16:41:01 clientavnetwork cron[731]: (*system*mountandsync) RELOAD (/etc/cron.d/mountandsync)
May 12 16:42:01 clientavnetwork CRON[17599]: (root) CMD (/usr/bin/bash /usr/local/bin/checkmount.bash >> /usr/local/bin/errorCRON.log 2>&1)
May 12 16:46:01 clientavnetwork cron[731]: (*system*mountandsync) RELOAD (/etc/cron.d/mountandsync)
May 12 16:46:01 clientavnetwork CRON[17833]: (root) CMD (/usr/bin/bash /usr/local/bin/checkmount.bash >> /usr/local/bin/errorCRON.log 2>&1)
May 12 16:47:01 clientavnetwork CRON[17862]: (root) CMD (/usr/bin/rsnapshot alpha)
May 12 16:50:01 clientavnetwork cron[731]: (*system*mountandsync) RELOAD (/etc/cron.d/mountandsync)
May 12 16:53:01 clientavnetwork CRON[18033]: (root) CMD (/usr/bin/bash /usr/local/bin/checkmount.bash >> /usr/local/bin/errorCRON.log 2>&1)
May 12 16:54:01 clientavnetwork CRON[18041]: (root) CMD (/usr/bin/rsnapshot alpha)
May 12 21:26:04 clientavnetwork systemd-timesyncd[674]: No network connectivity, watching for changes.
May 12 21:26:04 clientavnetwork systemd-timesyncd[674]: Network configuration changed, trying to establish connection.
May 12 21:26:13 clientavnetwork systemd-timesyncd[674]: Network configuration changed, trying to establish connection.
May 12 21:26:13 clientavnetwork systemd-timesyncd[674]: Network configuration changed, trying to establish connection.
May 12 21:26:15 clientavnetwork systemd-timesyncd[674]: Initial synchronization to time server 91.189.94.4:123 (ntp.ubuntu.com).
May 12 21:39:01 clientavnetwork CRON[18332]: (root) CMD (  [ -x /usr/lib/php/sessionclean ] && if [ ! -d /run/systemd/system ]; then /usr/lib/php/sessionclean; fi)
May 13 06:17:25 clientavnetwork systemd-timesyncd[674]: No network connectivity, watching for changes.
May 13 06:17:25 clientavnetwork systemd-timesyncd[674]: Network configuration changed, trying to establish connection.
May 13 06:17:34 clientavnetwork systemd-timesyncd[674]: Network configuration changed, trying to establish connection.
May 13 06:17:34 clientavnetwork systemd-timesyncd[674]: Network configuration changed, trying to establish connection.
May 13 06:17:36 clientavnetwork systemd-timesyncd[674]: Initial synchronization to time server 91.189.91.157:123 (ntp.ubuntu.com).
May 13 06:25:01 clientavnetwork CRON[19759]: (root) CMD (test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily ))
May 13 06:39:01 clientavnetwork CRON[19828]: (root) CMD (  [ -x /usr/lib/php/sessionclean ] && if [ ! -d /run/systemd/system ]; then /usr/lib/php/sessionclean; fi)
May 13 07:09:01 clientavnetwork CRON[19890]: (root) CMD (  [ -x /usr/lib/php/sessionclean ] && if [ ! -d /run/systemd/system ]; then /usr/lib/php/sessionclean; fi)
May 13 07:17:01 clientavnetwork CRON[19954]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
May 13 07:39:01 clientavnetwork CRON[19959]: (root) CMD (  [ -x /usr/lib/php/sessionclean ] && if [ ! -d /run/systemd/system ]; then /usr/lib/php/sessionclean; fi)
May 13 08:09:01 clientavnetwork CRON[20475]: (root) CMD (  [ -x /usr/lib/php/sessionclean ] && if [ ! -d /run/systemd/system ]; then /usr/lib/php/sessionclean; fi)
May 13 08:17:01 clientavnetwork CRON[20537]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
May 13 17:29:11 clientavnetwork systemd-timesyncd[674]: No network connectivity, watching for changes.
May 13 17:29:12 clientavnetwork systemd-timesyncd[674]: Network configuration changed, trying to establish connection.
May 13 17:29:22 clientavnetwork systemd-timesyncd[674]: Network configuration changed, trying to establish connection.
May 13 17:29:22 clientavnetwork systemd-timesyncd[674]: Network configuration changed, trying to establish connection.
May 13 17:29:24 clientavnetwork systemd-timesyncd[674]: Initial synchronization to time server 91.189.89.199:123 (ntp.ubuntu.com).
May 13 17:39:01 clientavnetwork CRON[20647]: (root) CMD (  [ -x /usr/lib/php/sessionclean ] && if [ ! -d /run/systemd/system ]; then /usr/lib/php/sessionclean; fi)
May 13 17:55:27 clientavnetwork systemd-timesyncd[674]: No network connectivity, watching for changes.
May 13 17:55:29 clientavnetwork systemd-timesyncd[674]: Network configuration changed, trying to establish connection.
May 13 17:55:29 clientavnetwork systemd-timesyncd[674]: Network configuration changed, trying to establish connection.
May 13 17:55:30 clientavnetwork systemd-timesyncd[674]: Initial synchronization to time server 91.189.91.157:123 (ntp.ubuntu.com).
May 13 17:55:34 clientavnetwork systemd-timesyncd[674]: No network connectivity, watching for changes.
May 13 17:55:36 clientavnetwork systemd-timesyncd[674]: Network configuration changed, trying to establish connection.
May 13 17:55:36 clientavnetwork systemd-timesyncd[674]: Network configuration changed, trying to establish connection.
May 13 17:55:36 clientavnetwork systemd-timesyncd[674]: Initial synchronization to time server 91.189.89.198:123 (ntp.ubuntu.com).
May 13 17:57:10 clientavnetwork systemd-timesyncd[674]: Initial synchronization to time server 91.189.89.198:123 (ntp.ubuntu.com).
May 13 18:09:02 clientavnetwork CRON[20718]: (root) CMD (  [ -x /usr/lib/php/sessionclean ] && if [ ! -d /run/systemd/system ]; then /usr/lib/php/sessionclean; fi)
May 13 18:17:02 clientavnetwork CRON[20780]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
May 13 18:39:02 clientavnetwork CRON[20784]: (root) CMD (  [ -x /usr/lib/php/sessionclean ] && if [ ! -d /run/systemd/system ]; then /usr/lib/php/sessionclean; fi)
May 13 20:41:16 clientavnetwork systemd-timesyncd[674]: No network connectivity, watching for changes.
May 13 20:41:16 clientavnetwork systemd-timesyncd[674]: Network configuration changed, trying to establish connection.
May 13 20:41:25 clientavnetwork systemd-timesyncd[674]: Network configuration changed, trying to establish connection.
May 13 20:41:25 clientavnetwork systemd-timesyncd[674]: Network configuration changed, trying to establish connection.
May 13 20:41:26 clientavnetwork systemd-timesyncd[674]: Initial synchronization to time server 91.189.89.198:123 (ntp.ubuntu.com).
May 13 21:09:01 clientavnetwork CRON[21354]: (root) CMD (  [ -x /usr/lib/php/sessionclean ] && if [ ! -d /run/systemd/system ]; then /usr/lib/php/sessionclean; fi)
May 13 21:17:01 clientavnetwork CRON[21417]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
May 13 21:39:01 clientavnetwork CRON[21421]: (root) CMD (  [ -x /usr/lib/php/sessionclean ] && if [ ! -d /run/systemd/system ]; then /usr/lib/php/sessionclean; fi)
May 14 07:33:48 clientavnetwork systemd-timesyncd[674]: No network connectivity, watching for changes.
May 14 07:33:48 clientavnetwork systemd-timesyncd[674]: Network configuration changed, trying to establish connection.
May 14 07:33:56 clientavnetwork systemd-timesyncd[674]: Network configuration changed, trying to establish connection.
May 14 07:33:56 clientavnetwork systemd-timesyncd[674]: Network configuration changed, trying to establish connection.
May 14 07:33:57 clientavnetwork systemd-timesyncd[674]: Initial synchronization to time server 91.189.89.199:123 (ntp.ubuntu.com).
May 14 07:39:01 clientavnetwork CRON[22028]: (root) CMD (  [ -x /usr/lib/php/sessionclean ] && if [ ! -d /run/systemd/system ]; then /usr/lib/php/sessionclean; fi)
May 14 08:09:01 clientavnetwork CRON[23398]: (root) CMD (  [ -x /usr/lib/php/sessionclean ] && if [ ! -d /run/systemd/system ]; then /usr/lib/php/sessionclean; fi)
May 14 08:17:02 clientavnetwork CRON[23460]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
May 14 08:39:02 clientavnetwork CRON[23466]: (root) CMD (  [ -x /usr/lib/php/sessionclean ] && if [ ! -d /run/systemd/system ]; then /usr/lib/php/sessionclean; fi)
May 14 09:09:02 clientavnetwork CRON[23531]: (root) CMD (  [ -x /usr/lib/php/sessionclean ] && if [ ! -d /run/systemd/system ]; then /usr/lib/php/sessionclean; fi)
May 14 09:17:02 clientavnetwork CRON[23594]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
May 14 09:39:01 clientavnetwork CRON[23623]: (root) CMD (  [ -x /usr/lib/php/sessionclean ] && if [ ! -d /run/systemd/system ]; then /usr/lib/php/sessionclean; fi)
May 14 10:09:02 clientavnetwork CRON[23690]: (root) CMD (  [ -x /usr/lib/php/sessionclean ] && if [ ! -d /run/systemd/system ]; then /usr/lib/php/sessionclean; fi)
May 14 10:17:02 clientavnetwork CRON[23753]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
May 14 10:39:02 clientavnetwork CRON[23963]: (root) CMD (  [ -x /usr/lib/php/sessionclean ] && if [ ! -d /run/systemd/system ]; then /usr/lib/php/sessionclean; fi)
May 14 10:47:01 clientavnetwork cron[731]: (*system*mountandsync) RELOAD (/etc/cron.d/mountandsync)
May 14 10:53:01 clientavnetwork CRON[24126]: (root) CMD (/usr/bin/bash /usr/local/bin/checkmount.bash >> /usr/local/bin/errorCRON.log 2>&1)
May 14 10:59:01 clientavnetwork cron[731]: (*system*mountandsync) RELOAD (/etc/cron.d/mountandsync)
May 14 11:00:01 clientavnetwork CRON[24179]: (root) CMD (/usr/bin/bash /usr/local/bin/checkmount.bash >> /usr/local/bin/errorCRON.log 2>&1)
May 14 11:05:01 clientavnetwork cron[731]: (*system*mountandsync) RELOAD (/etc/cron.d/mountandsync)
May 14 11:09:01 clientavnetwork CRON[24324]: (root) CMD (  [ -x /usr/lib/php/sessionclean ] && if [ ! -d /run/systemd/system ]; then /usr/lib/php/sessionclean; fi)
May 14 11:15:02 clientavnetwork CRON[24392]: (root) CMD (/usr/bin/bash /usr/local/bin/checkmount.bash >> /usr/local/bin/errorCRON.log 2>&1)
May 14 11:17:01 clientavnetwork CRON[24401]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
May 14 11:38:01 clientavnetwork cron[731]: (*system*mountandsync) RELOAD (/etc/cron.d/mountandsync)
May 14 11:39:01 clientavnetwork CRON[25095]: (root) CMD (  [ -x /usr/lib/php/sessionclean ] && if [ ! -d /run/systemd/system ]; then /usr/lib/php/sessionclean; fi)
May 14 11:42:01 clientavnetwork CRON[25158]: (root) CMD (/usr/bin/bash /usr/local/bin/checkmount.bash >> /usr/local/bin/errorCRON.log 2>&1)