Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
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
add comment

1 Answer

up vote 6 down vote accepted

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.

share|improve this answer
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
add comment

Your Answer

 
discard

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

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