Uptime

abenedict
New Contributor II

I need a little help creating a script that will look at the computers
uptime and if it has not been rebooted in more than a week it would either
display a message to the user reminding them to reboot or populate the
computer into a smart group so I can take an appropriate action. Any ideas
of how I would accomplish this?

Thanks
Alan

--
Alan Benedict
?
Macintosh Technician
The Integer Group
O: 515-247-2738
C: 515-770-8234
http://www.integer.com

19 REPLIES 19

donmontalvo
Esteemed Contributor III

Interesting, I wonder if Growl be able to toss up a nagging reminder? :)
Alan Benedict <abenedict at integer.com> wrote:

Don

--
https://donmontalvo.com

ernstcs
Contributor III

Someone is going to say dummy receipt I’m sure of it. Here is my really complicated way of doing this that I just cooked up in my head...but I like it because of the way it cleans itself up when the system actually reboots.

If something isn’t clear let me know.

Solution 1:

Create a new package called long-uptime.dmg (or whatever you like) that contains a startup item that will:

* Run a jamf binary policy command that calls your manually named policy to uninstall long-uptime.dmg

Let me know if you want help creating the StartupItem.

Create a smart group that looks for a package receipt for long-uptime.dmg that you’ve created. This is your current list of systems up a long time.

Create a policy that is manually triggered that:

Installs your long-uptime.dmg package Runs a recon of the system so it goes into the smart group you created when it sees a receipt for the package * Displays a message to the user to Remind them to reboot using the reboot tab of the policy. Or if you wanted to get more advanced probably an AppleScript (which could come down with and be somewhere inyour startup package) that has a choice if they choose yes then perform a reboot command right then and there. A bit more dangerous to do that, but put wording in the choice to make sure they save and quit all of their applications first so they don’t lose their work.

Create a policy that is manually triggered by the name you specified in your startup item that will:

Uninstall your long-uptime.dmg Runs a recon

Create a policy that runs on what ever interval you want to check for this uptime that runs a script that does the uptime check.

If the script determines the system has been up longer than you chose it will Run a jamf binary policy command that calls your manually named policy * If the script determines the system has been up less than you chose it will do nothing

-- Script Code --

#!/bin/sh

. /etc/rc.common

days=$(uptime | awk '{print $3}' | sed 's/,//g')

if [ "$days" > 7 ]; then /usr/sbin/jamf policy -trigger your_manual_install_package_trigger
Fi

-- End Script Code --

Code Parts Borrowed From (CUZ I SUCK AT SCRIPTS!):
http://brockangelo.com/2009/06/05/how-to-display-your-uptime-using-bash/

When they actually reboot or you force a reboot the startup item you made and deployed does the rest.

Question: Why do you want to see if a system reboot or not? What is driving you to want to make a system reboot if it doesn’t require one for a software update or install? I understand in my lab environment that I want it to reboot every night for maintenance. Is this for a lab?

Solution 2: Put in a feature request to JAMF that a recon reports uptime to the JSS and then allow Smart Group options for it. =)

Hope that helps!

Craig E

tlarkin
Honored Contributor

you can also do this:

#!/bin/bash

some code for my shell script

osascript -e 'tell app "System Events" to display dialog "Reboot or die!" '

Of course that is a joke.....

This will also retun OK in the shell, but you can have it continue to do whatever, by doing a & or && after the osascript. I got my new apple script book in the mail a few weeks go so I am toying with some AS stuff now.

abenedict
New Contributor II

My main problem with using the jamf binary or AppleScript to display a
message is that it will time out if not clicked. So if someone is away from
their desk when the notification pops up, most likely when they return it
will no longer be there.

--
Alan Benedict
?
Macintosh Technician
The Integer Group
O: 515-247-2738
C: 515-770-8234
http://www.integer.com

tlarkin
Honored Contributor

I am sure you can set a loop for it, I know for a fact in Apple Script
you can manually set the times it runs until it is clicked. I just
don't know the full syntax as I am still learning Apple Script

tlarkin
Honored Contributor

Actually,

Now that I think about it, if you really want to annoy your users, use the say command to spam "Please reboot your machine" over and over again until they reboot. example:

osascript -e ' tell app "System Events"to display dialog "Please reboot your machine" ' & say "Please reboot your machine!"

abenedict
New Contributor II

If I set it to loop and lets say it loops for 4 hours until the user finally
clicks OK, since it is still running that policy, will any other policies
run while the script is waiting for the OK button? If not, I think that is
reason enough for me to use Growl.

--
Alan Benedict
?
Macintosh Technician
The Integer Group
O: 515-247-2738
C: 515-770-8234
http://www.integer.com

dderusha
Contributor

Don't forget to loop the volume incase they decide to mute it

tlarkin
Honored Contributor

I am pretty sure casper can handle multiple policies at once, but that
is a question best answered by Jamf.

tlarkin
Honored Contributor

Alan-

I love the idea. We used to tell students to reboot 5 times daily,
thinking if we told them to reboot 5 times they would at least reboot
once. So, what you can do is a simple bracket comparison in a shell
script like so

#!/bin/bash

days=uptime | awk '{ print $3 }'

if [ $days -gt 7 ]

then jamf displayMessage -message "Please reboot your machine, it has
not been rebooted in over 1 week"

fi

done



Thomas Larkin
TIS Department
KCKPS USD500
tlarki at kckps.org
blackberry: 913-449-7589
office: 913-627-0351
chown -R us /.base

stevewood
Honored Contributor II
Honored Contributor II

Ah, but here's the rub: your script, and Craig's, returns only the number
regardless of if it is minutes, hours, or days.

For example, I just ran your script on my machine and it reported back 25:

Steve-Wood:~ swood$ uptime | awk '{ print $3 }'
25
Steve-Wood:~ swood$ uptime 8:36 up 25 mins, 2 users, load averages: 0.04 0.14 0.13

You can see from me running uptime manually that my machine has been up for
25 minutes, not 25 days. Your scripts would wrongly interpret that as being
up 25 days.

So to do this right, you would have to look at the fourth item in the uptime
return to see if it was days or mins or whatever, then look at the third
item to see what it is. Borrowing from Craig and Tom:

#!/bin/bash

days=uptime | awk '{ print $4 }'sed 's/,//g'
num=uptime | awk '{ print $3 }'

if [ $days = "days" ];

then
if [ $num -gt 1 ]; then jamf displayMessage -message "Restart Now" fi
fi

done
exit 0

That should work. I tested on a machine that has been up for 4 days and it
displayed the message.

You could also write out a dummy text file and scope a group to look for
that file. Then you have a smart group with the members.

You could also use Growl with the growlnotify scirpt instead of the JAMF
binary. Your call.

Steve Wood
Director of IT
swood at integer.com

The Integer Group | 1999 Bryan St. | Ste. 1700 | Dallas, TX 75201
T 214.758.6813 | F 214.758.6901 | C 940.312.2475
Sent from Dallas, TX, United States

tlarkin
Honored Contributor

I was thinking about doing a grep for days first in a mini array and
then awk'ing out the date but was not sure on how it displayed if it
were not days, but minutes.

Thanks for the tip

abenedict
New Contributor II

Thanks for the help! This will surely get me going in the right direction. One question about GrowlNotify, my manager is worried about putting Growl
on all out users machines, he wants to make sure that other apps that
support Growl don't start popping up notifications for things. Is there a
way that I can ensure that all application notifications are turned off and
stay turned off even if an application that supports Growl is installed at a
later date?
--
Alan Benedict
?
Macintosh Technician
The Integer Group
O: 515-247-2738
C: 515-770-8234
http://www.integer.com

tlarkin
Honored Contributor

Why install growl? Casper does all of this for you? Create a script,
check the box in Casper Admin that says, make available off line, which
will cache the script to the machine. Set the frequency to run once per
a day, and the casper binary will take of it and the end user will never
know it.

launchd can also handle this, but Casper gives you the easy button

stevewood
Honored Contributor II
Honored Contributor II

<trimmed for Don's viewing pleasure>
On Tue, Jan 19, 2010 at 11:33 AM, Thomas Larkin <tlarki at kckps.org> wrote:

I finally had a few more minutes to play around with this script a bit. I
realized I had a typo in what I sent out earlier, so here it is again,
slightly modified:

#!/bin/bash

days=uptime | awk '{ print $4 }' | sed 's/,//g'
num=uptime | awk '{ print $3 }'

if [ $days = "days" ];

then
if [ $num -gt 1 ]; then
/usr/local/bin/growlnotify -a "Finder" -t "Restart Required" -m "Your
computer has been up for $num days. Please restart as soon as possible." -s
fi
fi
exit 0

I missed the pipe in the "days" variable declaration.

And, to Tom's question about using "jamf displayMessage", what I've noticed
is that the jamf method seems to wait for the user to click OK and then
tells you what it returned. In this case, you really don't need that. All
you need to do is alert the user. GrowlNotify works perfect for this

And to Alan's question, the only way I know of to disable particular apps in
Growl is to do it through the Sys Pref pane. I haven't tried setting those
prefs via Composer capture or any other way, but I'm sure there should be a
way to do it.

Steve Wood
Director of IT
swood at integer.com

The Integer Group | 1999 Bryan St. | Ste. 1700 | Dallas, TX 75201
T 214.758.6813 | F 214.758.6901 | C 940.312.2475
Sent from Dallas, TX, United States

donmontalvo
Esteemed Contributor III
Steve Wood <swood at integar.com> wrote: <trimmed for Don's viewing pleasure>

(blush)

And, to Tom's question about using "jamf displayMessage", what I've noticed is that the jamf method seems to wait for the user to click OK and then tells you what it returned. In this case, you really don't need that. All you need to do is alert the user. GrowlNotify works perfect for this

I thought the same thing. Growl is a non invasive way to get these errors displayed, as opposed to what I assume is a modal dialog box tossed up by Casper? Correct me if I'm wrong.

I'm not familiar enough with Growlnotify to know how to disable alerts for the other applications, but I'm fairly certain the defaults command can handle this. I'd have to defer the "how"...

Don

--
https://donmontalvo.com

stevewood
Honored Contributor II
Honored Contributor II

Disabling other notifications is easier than I thought. Growl uses its
Application Support folder to store what apps it will provide notifications
for:

/Users/<username>/Library/Application Support/Growl/Tickets

If you clear out that folder then no other applications should display Growl
notifications. I have not tested whether notifications will definitely stop
for other apps, but I did test to make sure they do not show up in Growl
once removed from that folder.

Also, you would want to check this folder regularly, especially after
installing software since a lot of software now-a-days is starting to use
Growl for notifications.

Steve Wood
Director of IT
swood at integer.com

The Integer Group | 1999 Bryan St. | Ste. 1700 | Dallas, TX 75201
T 214.758.6813 | F 214.758.6901 | C 940.312.2475
Sent from Dallas, TX, United States

jchurch
Contributor II

this isn't working for me. when i run the script i get "command not found"

here the script I'm using.

#!/bin/bash

days=uptime | awk '{ print $4 }' | sed 's/,//g'
num=uptime | awk '{ print $3 }'

if [ $days = "days" ];

then
if [ $num -gt 5 ];
then
jamf displayMessage -message "Just a friendly reminder from TSS...
90% of computer problems can be avoided by simply rebooting your computer on a daily basis.
Your computer has been up for $num days. Please restart as soon as possible."
fi
fi

#done
exit 0

any ideas?

stevewood
Honored Contributor II
Honored Contributor II

@jchurch Are you running this on a machine that has the jamf binary installed? That's the only thing I can see that might give that error.

You could add some echo's in there to debug a bit and figure out where the error is coming.