Pythian Blog: Technical Track

Detect and Notify Linux Server Restart

When Oracle support performs maintenance for Exadata Cloud there is minimal communication.

Typically, Oracle support restarts the host during maintenance. Since the application is not RAC aware, the application team needs a way of knowing they have to restart the application after Oracle restarts the database server.

To help with this situation, cron offers an option to detect a server reboot with @reboot.

From the crontab below, when the server is rebooted, the shell script will be executed as the oracle user.

# crontab -l
@reboot su oracle -c '/home/oracle/scripts/host_restart_alert.sh' > /tmp/host_restart_alert.out 2>&1

The shell script host_restart_alert.sh will send a message to the recipient.

#!/bin/bash -x 
# Script ie being called from root crontab 
# uptime reports minutely and need to sleep for at least 1m after host restart 
sleep 63 
EMAILMESSAGE="$(hostname) was restarted `uptime -p | awk -F'up' '{print $2}'` ago at `uptime -s`" 
echo $EMAILMESSAGE | mailx -s "$HOSTNAME was restarted" email@gmail.com
exit 

Here is an example of what the EMAILMESSAGE will look like:

$ EMAILMESSAGE="$(hostname) was restarted `uptime -p | awk -F'up' '{print $2}'` ago at `uptime -s`"
$ echo $EMAILMESSAGE
oracle-12201-vagrant was restarted 2 hours, 47 minutes ago at 2021-02-16 14:50:02

I have tested these scripts on Oracle Linux Server release 7.8  and 7.9.

Hopefully, this is a useful tip to help detect and notify you of a server restart.

Please leave any questions or thoughts in the comments.

No Comments Yet

Let us know what you think

Subscribe by email