3

I want to write shell or perl code which would give me notification after x days. Basically on SUSE Linux server, there is reboot required after 208 days, else the system would freeze. This is known bug. So in order to avoid this, I want notification mail coming to me before 8 days something like that....just an alert..

The script has to check the current uptime and then calculate the remaining days required for total 208 days. Hope this makes sense.

  • 1
    The right thing to do, of course, would be to fix the bug that causes this in the first place. – grawity Apr 18 '12 at 8:04
7

You can easily determine the uptime in days with awk:

# Print days of uptime, or zero if less than 1 day.
uptime | awk '/days?/ {print $3; next}; {print 0}'

You can use this with command substitution to perform any action you like based on the results. For example:

#!/bin/bash
days () { uptime | awk '/days?/ {print $3; next}; {print 0}'; }
UPTIME_THRESHOLD=200
if [ $(days) -ge $UPTIME_THRESHOLD ]; then
    : # Take some action.
fi

Obviously, the action you take is up to you. You can mail yourself messages, schedule a reboot with the at command, or anything else that you feel necessary to do.

You may also wish to set this script up as a daily cron job, so that it will trigger your defined action when the uptime threshold has been exceeded. If you have root access, you could simply drop the script into /etc/cron.daily/, or you might set up your personal crontab to call this script once a day.

  • 1
    On Linux, it is faster to read from /proc/uptime (the first number is uptime in seconds). – grawity Apr 18 '12 at 7:59
1

Inspired by CodeGnome answer, made a small script for mac, where a notification shows up after the computer is on for 8:30. Also had to configure cron to run this every minute.

#!/bin/bash
timeup () { uptime | awk  '{print $3}'; }
UPTIME="8:30,"
if [ $(timeup) == $UPTIME ]; then
    osascript -e 'tell app "Finder" to display dialog "8:30 uptime! Go Home!"' &
    osascript -e 'display notification "8:30 uptime! Go Home!" with title "Go Home!!"' &
fi

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.