Adding a cron job to VMWare / ESXi

ESXi VMWare

I needed to add a couple of cron jobs to my ESXi box.

This is quite a bit more complicated than it needs to be in my opinion, but here’s the procedure.

Firstly, I create my script and then store it on my datastore (in my case /vmfs/volumes/datastore2/hp/)

Then, I added the following to /etc/rc.local.d/local.sh

/bin/kill $(cat /var/run/crond.pid)
/bin/echo '30 1 * * * /vmfs/volumes/datastore2/hp/check_disk.sh > /dev/null 2>&1' >> /var/spool/cron/crontabs/root
/bin/echo '*/5 * * * * /vmfs/volumes/datastore2/hp/disk_temps.sh > /dev/null 2>&1' >> /var/spool/cron/crontabs/root
/usr/lib/vmware/busybox/bin/busybox crond

The first line kills any running cron daemon.

The second and third lines echo the tasks I want to add to root’s cron file which is stored in /var/spool/crontabs/root

The fourth line restarts the cron daemon.

For those who my be interested, these are my scripts:

/vmfs/volumes/datastore2/hp/check_disk.sh

#!/bin/sh
STATUS=""
HOSTNAME=$(hostname -f)
MAILHOST=
PORT=25
TO=admin@your.domain
FROM=esxi@your.domain
DATE=$(date +'%d/%m/%Y')
MESSID=$(date | md5sum)
MAILDATE=$(date --rfc-2822)
MIMEVER="MIME-Version: 1.0"
SUBJECT="Raid status $DATE"

err_exit() { echo -e 1>&2; exit 1; }

STATUS=$(/vmfs/volumes/datastore2/hp/hpacucli ctrl slot=16 ld all show status)

mail_input()
{
  echo "ehlo $HOSTNAME"
  echo "MAIL FROM: <$FROM>"
  echo "RCPT TO: <$TO>"
  echo "DATA"
  echo "From: <$FROM>"
  echo "To: <$TO>"
  echo "Subject: $SUBJECT"
  echo "Date: $MAILDATE"
  echo "Message-ID: <$MESSID@$HOSTNAME>"
  echo "$MIMEVER"
  echo "Content-Type: text/plain; charset='utf-8'"
  echo "Content-Transfer-Encoding: quoted-printable"
  echo "Importance: normal"
  echo "Priority: normal"
  echo "$STATUS"
  echo "."
  echo "quit"
}

mail_input | nc $MAILHOST $PORT || err_exit

Some of the email header info above may appear to be superfluous – but it did enable my emails to bypass my spam filtering.

Also – if using Postfix as your SMTP server, you need to allow SMTP pipe lining due to the method in which netcat sends email.

Finally – this is specific to the HP P410i array controller, but as this controller is quite popular for home labs, I thought it might be useful to include this info here.

This is the output of the above script:

Subject: Raid status 08/09/2015

logicaldrive 1 (596.1 GB, RAID 5): OK
logicaldrive 2 (596.1 GB, RAID 5): OK

I do realise that I could simply elect to only send email when there is an error – however I like the warm feeling of seeing “OK” every day… 🙂

The other script I run gets the disk temperatures using the HP utility (you need the latest controller firmware for this).

/vmfs/volumes/datastore2/hp/disk_temps.sh

/vmfs/volumes/datastore2/hp/hpacucli ctrl slot=16 pd all show detail |grep "Current Temperature (C):" |awk -F ":" {'print $2'} |xargs |sed -e 's/ /,/g' > /vmfs/volumes/datastore2/hp/disk_temps.txt
cat /vmfs/volumes/datastore2/hp/disk_temps.txt | nc -w 3 1.2.3.4 1025

This post details more information about using this script, and the output it generates.