使用CRON运行脚本会产生语法错误

时间:2018-06-27 10:49:34

标签: bash shell cron

我写了一个简单的脚本来找到与网络相对应的延迟(脚本在发布结尾)

当我使用

从CLI(作为根用户)运行此脚本时

./ latencytest运行正常,我得到类似

的输出
Latency To LondonDB4|1|20180627112833|Latency with in Limits||
Latency To LondonDB4|D|maxlatency|LondonDB4|16.8|Max Latency|ms|
Latency To LondonDB4|D|minlatency|LondonDB4|4.59|Min Latency|ms|
Latency To LondonDB4|D|avglatency|LondonDB4|6.02|Average Latency|ms|
Latency To LondonDB4|D|packetloss|LondonDB4|0|Packetloss||

但是我将以下行添加到cron中(作为root并尝试了一些修改)

* * * * * cd /opt/mutiny/bin;./latencytest > /dev/null

我明白了

Latency To LondonDB4|1|20180627112801|Latency with in Limits||
Latency To LondonDB4|D|maxlatency|LondonDB4||Max Latency|ms|
Latency To LondonDB4|D|minlatency|LondonDB4||Min Latency|ms|
Latency To LondonDB4|D|avglatency|LondonDB4||Average Latency|ms|
Latency To LondonDB4|D|packetloss|LondonDB4||Packetloss||

请注意输出中缺少各种metrix的值。

这是在Centos上运行的,我在var / spool / mail / root中收到显示

的邮件
From root@mutiny-remote.localdomain  Wed Jun 27 11:17:01 2018
Return-Path: <root@mutiny-remote.localdomain>
X-Original-To: root
Delivered-To: root@mutiny-remote.localdomain
Received: by mutiny-remote.localdomain (Postfix, from userid 0)
        id 22CB9872769; Wed, 27 Jun 2018 11:17:01 +0100 (BST)
From: "(Cron Daemon)" <root@mutiny-remote.localdomain>
To: root@mutiny-remote.localdomain
Subject: Cron <root@mutiny-remote> cd /opt/mutiny/bin;./latencytest > /dev/null
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
Precedence: bulk
X-Cron-Env: <XDG_SESSION_ID=818>
X-Cron-Env: <XDG_RUNTIME_DIR=/run/user/0>
X-Cron-Env: <LANG=en_GB.UTF-8>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>
Message-Id: <20180627101701.22CB9872769@mutiny-remote.localdomain>
Date: Wed, 27 Jun 2018 11:17:01 +0100 (BST)

(standard_in) 2: syntax error

它似乎正在标记一个语法错误,在手动运行它时它不会标记。

最后,脚本(我删除了一些逻辑并注释掉了用于测试的锁定)

我假设它可能是fping / AWK命令,但是我不确定如何找出导致语法错误的原因。

#!/bin/bash

## create lock directory so only one instance of script can run
#if ! mkdir /tmp/myscript.lock 2>/dev/null; then
#    echo "Lockfile found exiting" >&2
#    exit 1
#fi

#get current date and time
date=$(date +%Y%m%d%H%M%S)

### this area of the  script we set up the remote host to test , number of ping to send for each test and  size of packet.
size=500
number=50
IP=8.8.8.8
hostname=database 4
folder="/opt/mutiny/agentResults"


### here we can set up the warning and critical values for each of the 3 values. average and max  latency and packet lost per script cycle
### we are not testing the minimum value just reporting it.

### Due to how ping works the first packet can have a high spike in latency of possible 10+ ms above the average. so it is
### sugested that focus should be on average and packet loss.

maxW=5
maxC=15

avgW=5
avgC=15

lossW=1
lossC=5

avgS=OK
maxS=OK
lossS=OK


## we also set the status agent begin status state to 1 (this is OK 0, is critical and 2 is warning)
status=1
 statusline="Latency with in Limits"

### First step is to get the latency vaules we can report on, we use fping for this and then various cut and awk steps to extract the required data

variable=$(fping -c $number -p 50 -b $size $IP 2>&1 | awk  '/min/  {print $5,$8;}' OFS='/')



sent=$(echo $variable | cut -d '/' -f 1)
recived=$(echo $variable | cut -d '/' -f 2 )
min=$(echo $variable | cut -d '/' -f 4)
avg=$(echo $variable | cut -d '/' -f 5)
max=$(echo $variable | cut -d '/' -f 6)

loss=$(echo $sent-$recived | bc)

### Next we need to determ if any of the vaules are in a warning or critical state and update the return string to report this clearly.
### we test max, avg and then packet loss. in this way the agent will report the highest critical status over all.


### one we have updated the status to the required value we are ready to out put it all in to mutiny agent format
## This block of code creates the table that mutiny will display with in the node agent.

### line one sets up the headers



##last we close the table

echo "Latency To $hostname|$status|$date|$statusline||" > $folder/latencyout.out
echo "Latency To $hostname|D|maxlatency|$hostname|$max|Max Latency|ms|" >> $folder/latencyout.out
echo "Latency To $hostname|D|minlatency|$hostname|$min|Min Latency|ms|" >> $folder/latencyout.out
echo "Latency To $hostname|D|avglatency|$hostname|$avg|Average Latency|ms|" >> $folder/latencyout.out
echo "Latency To $hostname|D|packetloss|$hostname|$loss|Packetloss||" >> $folder/latencyout.out



rm -rf "/tmp/myscript.lock"

1 个答案:

答案 0 :(得分:0)

如果愿意,请删除,但在我发布此帖子时,有人向我展示了此问题。

问题是手动运行和从Cron运行时使用了不同的环境变量。主要是路径

PATH = / usr / local / bin:/ usr / bin:/ usr / local / sbin:/ usr / sbin:/ home //。local / bin:/ home // bin

vs

PATH = / usr / bin:/ bin

简单的解决方案是使用脚本中fping的完整路径,该路径可通过键入

找到
whereis fping

脚本现在可以正确运行。也可以更改Cron的路径env,但在这种情况下,我认为在脚本中使用完整路径是更好的方法。

如果该问题重复出现,请删除,但我认为这可能会对其他问题有所帮助。