1

I have written a shell script which checks the NTP service on all our servers and mails the output to specific email. When I execute it manually, it runs fine and I'm receiving email with valid contents.

However, when I place it in cronjob for the same user, it is sending empty mails. Found the file /tmp/ntp_status.txt does not store any information when cronjob run. But it updates the details when run manually. I'm unable to find out what's missing. Same issue persist even if I scheduled the cronjob for root user. Below is my script and crontab entry.

0 12 * * * /root/test/servers_list/ntp.test.sh

#!/bin/bash
USR="root"

# Email
SUBJECT="NTP Service Status Report"
EMAIL="skrishna1@fmi.com"
EMAILMESSAGE="/tmp/ntp_status.txt"

# create new file
>$EMAILMESSAGE

# connect each host and pull up user listing
for host in `cat SAP_Prod_servers_51`
do
echo "--------------------------------" >>$EMAILMESSAGE
echo "* HOST: $host " >>$EMAILMESSAGE
echo "--------------------------------" >>$EMAILMESSAGE
ssh $USR@$host "UP=$(ps -ef | grep -v grep | grep ntpd | wc -l);
if [ "$UP" -ne 1 ];
then
        echo "NTP is down.";
        sudo service ntpd start

else
        echo "NTP Service is running.";
fi" >> $EMAILMESSAGE
done

# send an email using /bin/mail
/bin/mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE
  • 3
    What directory is SAP_Prod_servers_51 in? The user's home directory? – Mark Plotnick Dec 20 '19 at 4:30
  • that is the file name which have servers list. – sri Dec 20 '19 at 4:31
  • 2
    The point Mark Plotnick is getting at is that probably that file is not in the current working directory (which will be the home directory of the crontab's owner), and hence it can't be found as you do not use an absolute path, only the name itself. – wurtel Dec 20 '19 at 7:33
  • sudo service ntpd start requires a ; at the end? ssh (FAIK) treats the whole quoted command as a single line. – Paul_Pedant Dec 20 '19 at 15:47
  • You can also grep '[n]tpd', which will never match itself and so avoids the need for grep -v grep. – Paul_Pedant Dec 20 '19 at 15:51
0

I'm not sure if this could ever work. Because ending pair for first doublequotes of ssh argument (the command) ends without any escaping inside if []. The same applies for echos there.

I simplified the script a little. But didn't tested it.

#!/bin/bash
USR="root"
FILE=/full/path/to/file/change/this/SAP_Prod_servers_51

# Email
SUBJECT="NTP Service Status Report"
EMAIL="skrishna1@fmi.com"

for host in $(cat "$FILE")
do
  echo "--------------------------------"
  echo "* HOST: $host "
  echo "--------------------------------"
  ssh $USR@$host "
    if pgrep ntpd >/dev/null
    then
      echo 'NTP Service is running.'
    else
        echo 'NTP is down.'
        sudo service ntpd start
    fi
  " 2>&1
done | /bin/mail -s "$SUBJECT" "$EMAIL"

Notes: Keep in mind that running ntpd doesn't mean your time will be synced correctly.

If those servers have systemd, then systemd has ability to start failed services.

also consider:

  • to implement monitoring and measure ntp offset. Graph it and and trigger notifications based on some thresholds,

  • to use configuration management software to ensure service states.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.