Linux Blog

APC Access Temperature Query and Conversion. (2 of 2)

Filed under: Shell Script Sundays — TheLinuxBlog.com at 6:55 pm on Sunday, March 29, 2009

This second part of the script APC Access Temperature Query Script and its been a long time coming. Basically this script is the part that runs as a cron and will e-mail me if the temperature goes over a certain threshold. Once it returns to normal it e-mails me again. It has the option to send a text message to me via my SMS gateway, but it is commented out.

#!/bin/bash
 
temp=$(/home/linux/bin/temp f)
threshold=76
 
if [ "`echo \"$temp > $threshold\" | bc`" == 1 ]; then
echo $(date +%s) $temp >> /home/linux/thermal-over.log
echo "High Temp";
 
if [ "$(cat temp.txt)" == "norm" ]; then
echo "Sending E-Mail, High Temp";
echo "Current Temperature Is: $(/home/linux/bin/temp f)" | mail -s "Thermal Overload" owen@linuxblog                #echo "Current Temp Is: $(/home/linux/bin/temp f)" | mail -s "Thermal Overload" mynumber@cingularme.com
echo "high" > temp.txt
fi
elif [ "`echo \"$temp < $threshold\" | bc`" == 1 ]; then
echo "Low Temp";
 
if [ "$(cat temp.txt)" == "high" ]; then
echo "Temp Resumed, Sending E-Mail";
echo $(date +%s) Resumed at: $temp | mail -s "Thermal Normal" owen@linuxblog
echo "norm" > temp.txt
fi
 
fi
 
echo $(date +%s) $temp >> /home/linux/thermal.log

When I first wrote the script, I did not do any temperature checking. I found out that I needed to when I came back one morning with a bunch of emails that I needed to delete. Its pretty simple to figure out, temp.txt holds a value that is either norm or high. It gets switched when the temperature changes, this will in turn stop it from e-mailing me repeatedly. Once the temperature drops it flips it back. It will still e-mail if your temperature fluctuates between 75 and 77 which can be annoying, but you can adjust the threshold with the variable and set it to what you need. Thankfully our chiller has been fixed and I no longer have to worry about the temperature, but it still runs on a cron just in case.

Timing your reboots with Twitter support!

Filed under: Shell Script Sundays — TheLinuxBlog.com at 12:01 am on Sunday, July 20, 2008

Firstly, I’d like to start off by saying that all of the concepts in this post should have been covered in other posts, so I will not go into great detail on the specifics of this script. If you need to know more information about any of the commands, check the man page section at the bottom of this page, from the man pages will be examples of other posts covering similar topics.

The purpose of this script for me was to time my reboot times. It could be modified to log the time it takes to replace hardware or add memory, but thats another post. Since we are logging reboot times, we are (hopefully) dealing with small numbers and therefore don’t have to deal with formatting time (at least not for now.)

The script should work on multiple systems that have bash. There is nothing too special about it. It uses the reboot command so the user this is launched as will have to have access to that command. You put the script in the users bin directory and chmod it. The user must also have write access to this. Also, they must have write access to their home directory, but this should not be a problem for most. Line 8 of the script needs to be changed to the user you plan on running this as.

After that test that the timereboot command works by typing timereboot:

[owen@linuxblog ~]$ timereboot
Usage: /home/linuxblog/bin/timereboot {time|ttime|back}

Once that is done, thats a pretty good indication that the script is working. Next, I suggest commenting out the reboot command on line #25 if this is a critical mission and you don’t want to reboot multiple times to get it working. If not go ahead and try the time command. Once your system is back up and your logged in you type the “timereboot back” command, it will then tell you the time taken since your system was done.

Once you have verified that the time works, you can go ahead and add it to your bashrc to automatically perform the action once your logged in. All you need to do is add a line like this:

home/linuxblog/bin/timereboot back

Now, if you want you can try again and see the results automatically.

“Thats great, but how do I post it to twitter?”

Well, there is one last thing that you have to do to get your reboot time posted to twitter. Edit line 55 and change to your twitter username and password. Do the same thing as before to reboot, but use the ttime parameter to log to twitter.

This script, does not post to twitter that you are rebooting (although it could) nor does it format the time, but it works and should give you a starting point if you are interested in doing this. It doesn’t really serve a real purpose other than to inform people how quickly or how slow you reboot. Also, please note that this is not a start up time. This times from when you issue the command until you issue the back command, or log in using the .bashrc method.

If you have any questions about this script or any other idea’s let me know and I’ll be happy to help or implement them for fun.

And here is the Twitter reboot script

Introduction to CHMOD – Octal Format

Filed under: General Linux,Quick Linux Tutorials — TheLinuxBlog.com at 1:25 pm on Tuesday, September 25, 2007

CHMOD is used to change permissions on a file. There are three types of permissions read, write, execute and there are three types that permissions can be set for owner, group and other.
It can be used with a symbolic representation or with an octal number that represents the bits. This blog post features on just the octal format. CHMOD works on most Linux file systems. It is also used on other operating systems such as BSD. Web designers and developers may be familiar with CHMOD as they have to set permissions when uploading files via FTP.

The octal notation can seem quite confusing but is actually very simple.
To figure out the octal format take the following table:

  Owner Group Other
Read 4 4 4
Write 2 2 2
Execute 1 1 1

To figure out the octal method just add up the sum of what you want to set the permissions to.
If you would like to set the permissions for the owner to read, write execute and the group/other to read and execute you would do the following:

  Owner Group Other
Read 4 4 4
Write 2 2 2
Execute 1 1 1
Add: 7 5 5

Its that simple. The way I remember the numbers to the corresponding permission is to remember that the number starts with 4 and is divided by two and then I repeat the following:
“For Read, Two Write, Execute”
meaning that 4 is read, 2 is write and the last (1) is execute.

There are graphical utilities that set permissions such as Thunar in XFCE and Konqeuror for KDE, but they normally do not allow you to change the permissions on multiple files at once.

If you have a whole directory full of files that you would like to change permissions on, you can simply do:

chmod 755 *

* is a wild card or regex and tells chmod to change permissions on all files.

If you would like chmod to go into directories and change permissions on files, the -R option is used.

chmod 755 -R *

will change permissions on all files and dive into the folders also.

chmod is an absolute must for system administrators and is good to know for home Linux users. If your experimenting with chmod be careful and do not use the -R option unless your absolutly sure you need to. I have accidentally used chmod to recursivly change permissions on a whole drive before. Lets leave it at this was not what I call a fun time since I had changed them to a very open 777.

Take that as your warning.