title: Running script using CRON gives Syntax error
body: <p>I have written a simple script to find the latency acorss the network (script at end of post) </p>
  
  <p>When I run this script from the CLI (as root) using </p>
  
  <p>./latencytest it runs fine and I get an output like </p>
  
  <pre><code>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||
  </code></pre>
  
  <p>however I added the following line to cron (as root and tried a few varations) </p>
  
  <pre><code>* * * * * cd /opt/mutiny/bin;./latencytest &gt; /dev/null
  </code></pre>
  
  <p>I get the follwoing out put apearing </p>
  
  <pre><code>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||
  </code></pre>
  
  <p>Note the lack of values for the various metrix in the output. </p>
  
  <p>This is running on Centos and i get mail in the var/spool/mail/root that says </p>
  
  <pre><code>From root@mutiny-remote.localdomain  Wed Jun 27 11:17:01 2018
  Return-Path: &lt;root@mutiny-remote.localdomain&gt;
  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)" &lt;root@mutiny-remote.localdomain&gt;
  To: root@mutiny-remote.localdomain
  Subject: Cron &lt;root@mutiny-remote&gt; cd /opt/mutiny/bin;./latencytest &gt; /dev/null
  Content-Type: text/plain; charset=UTF-8
  Auto-Submitted: auto-generated
  Precedence: bulk
  X-Cron-Env: &lt;XDG_SESSION_ID=818&gt;
  X-Cron-Env: &lt;XDG_RUNTIME_DIR=/run/user/0&gt;
  X-Cron-Env: &lt;LANG=en_GB.UTF-8&gt;
  X-Cron-Env: &lt;SHELL=/bin/sh&gt;
  X-Cron-Env: &lt;HOME=/root&gt;
  X-Cron-Env: &lt;PATH=/usr/bin:/bin&gt;
  X-Cron-Env: &lt;LOGNAME=root&gt;
  X-Cron-Env: &lt;USER=root&gt;
  Message-Id: &lt;20180627101701.22CB9872769@mutiny-remote.localdomain&gt;
  Date: Wed, 27 Jun 2018 11:17:01 +0100 (BST)
  
  (standard_in) 2: syntax error
  </code></pre>
  
  <p>it seems to be flagging a syntax error that it does not flag when running it manual. </p>
  
  <p>So finaly the script, (I have removed some of the the logic and commented out the locking for testing purposes)</p>
  
  <p>I am asuming that it might be the fping / AWK command as the but I am not sure how to work out what is causing the syntax error. </p>
  
  <pre><code>#!/bin/bash
  
  ## create lock directory so only one instance of script can run
  #if ! mkdir /tmp/myscript.lock 2&gt;/dev/null; then
  #    echo "Lockfile found exiting" &gt;&amp;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&gt;&amp;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||" &gt; $folder/latencyout.out
  echo "Latency To $hostname|D|maxlatency|$hostname|$max|Max Latency|ms|" &gt;&gt; $folder/latencyout.out
  echo "Latency To $hostname|D|minlatency|$hostname|$min|Min Latency|ms|" &gt;&gt; $folder/latencyout.out
  echo "Latency To $hostname|D|avglatency|$hostname|$avg|Average Latency|ms|" &gt;&gt; $folder/latencyout.out
  echo "Latency To $hostname|D|packetloss|$hostname|$loss|Packetloss||" &gt;&gt; $folder/latencyout.out
  
  
  
  rm -rf "/tmp/myscript.lock"
  </code></pre>