Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am able to run this script as a user or root with bash or sh. Doing so gives me the output that I expect (arrayFWS is written to file, then emailed). However, when I run it with crontab with user or root, the netcat output is not written to the array, so the file is empty but email is still sent.

I have determined it to be an issue with the execution of the script only as a cronjob. What is it about the cron service that is not allowing my findings to be stored in the array? Is the netcat results not being piped properly causing nothing to be written?

#!/bin/bash
arrayFWS=() # ARRAY TO STORE FOUND FW's

# -- DEBBUGING MESSAGE
echo $varSTARTMESSAGE

# -- LOOP THAT CYCLES THROUGH FW NUMBERS 
for (( i=136; i<=248; i++))
do  
    # -- DEBUG MESSAGE
    echo "Checking FW$i ..."
    # -- BUILD VARIABLE TO STORE FILTERED NETCAT SCAN LOOKING FOR A SMTP RESPONSE CONTAINING '200' VALUE -- INITAL SESSION SETUP
    varCHK=`nc -w 3 192.168.$i.17 25 | grep "^220\ "`   
    # -- IF CONDITION THAT WILL CALL THE FILTERED NETCAT SCAN TO RUN
    if [ -n "$varCHK" ]; then
        echo "Found something at 192.168.$i.17"
        echo "arrayFWS now contains: ${arrayFWS[@]}"
        # -- WRITES RESPONSIVE FW TO ARRAY FOR LATER USE
        arrayFWS=(${arrayFWS[@]} "$i")
    fi  
    # -- DEBUG MESSAGE
    echo "Done with FW$i ..."
done
echo " I found the following: ${arrayFWS[@]}"

# -------------------------------------------
varFILENAME="/root/report.txt"
echo "Starting to generate report"

echo > $varFILENAME "There are ${#arrayFWS[@]} FW's online"
echo >> $varFILENAME "----------------------------------------------------"
echo >> $varFILENAME " "

for (( d = 0 ; d < ${#arrayFWS[@]} ; d++ )) 
do 
echo >> $varFILENAME "FW${arrayFWS[$d]}"
done

# ---------------------------------------------
varSENDER="[email protected]"
(cat /root/report.txt) | mailx -r ${varSENDER} -s "Monitor: Found ${#arrayFWS[@]} FW's" [email protected]

Sample Email when run as user or root

There are 3 FW's online as of 201604155824 
----------------------------------------------------

FW123
FW126
FW201

Sample Email when run under crontab (as root or user)

There are 0 FW's  online as of 201604153501 
----------------------------------------------------
share|improve this question
    
Maybe you can find some logging information in the log of cron (if you use systemd you should see cron logs with journalctl). You can also try to use logger in your shell script to produce additional logging output for debugging. – Lucas Apr 15 at 23:11
    
the usual suspect is the PATH environment variable not being set in cron. so you should explicitly use the path to nc /bin/nc or whatever is appropriate in your system. – LHWizard Apr 16 at 2:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.