delete

Shell Script – Server Maintenance

So here’s my first shell script, written for a server at work who has maintenance tasks that need to be completed, and the commands get tedious to input and remember, now all you have to remember is ./maintenance

The script automates 4 tasks that our server has. Restarting out PostgreSQL database, restarting apache, vacuuming the database, and running our custom import scripts, which has to be done as a specific user each day, and NOT as root… which has caused issues in the past, but now if you run them with the script I wrote, there’s no issue at all.

When launched, the script displays a menu for you, on which you make your choices.

scriptmenu

You can then make your choice, via number, and it will complete the task and prompt you to continue.
When a wrong choice is made it tells you that you need to select a number from the list. The help command gives you an idea of what each other command does, and exit obviously exits the script.

So here’s a video to demonstrate how the menu system works.

Its a simple shell script, but it works, and it works well for what it does.
And of course I’d want to share the actual script with you, and document it in my blog,
so here it is, Frank’s maintenance script!

#######################################################
#  _______        _            _                      #
# |__   __|      | |          | |                     #
#    | | ___  ___| |__        | | __ ___      ____ _  #
#    | |/ _ \/ __|  _ \   _   | |/ _` \ \ /\ / / _` | #
#    | |  __/ (__| | | | | |__| | (_| |\ V  V / (_| | #
#    |_|\___|\___|_| |_|  \____/ \__,_| \_/\_/ \__,_| #
#                                                     #
#           www.techjawa.com/category/code            #
#             www.corey.degrandchamp.com              #
#                                                     #
#######################################################
#                                                     #
# Written  By: Corey DeGrandchamp                     #
# Written  On: June 18, 2009                          #
# Written For: Michigan Drill Corporation             #
# Description: This shell script will automate all of #
#              the maintenance tasks on Frank.        #
#                                                     #
#######################################################
#                                                     #
# Edit   Date: June 24, 2009                          #
# Edit     By: Corey DeGrandchamp                     #
# About  Edit: Fixed script so the heading/title will #
#              show every time now, also made it      #
#              pause and wait for the ENTER key to    #
#              be pressed before it continues.        #
#                                                     #
#######################################################

#!/bin/bash

# Display the title.
clear
echo '
     __  __    __    ____  _  _  ____  ____  _  _    __    _  _  ___  ____
    (  \/  )  /__\  (_  _)( \( )(_  _)( ___)( \( )  /__\  ( \( )/ __)( ___)
     )    (  /(__)\  _)(_  )  (   )(   )__)  )  (  /(__)\  )  (( (__  )__)
    (_/\/\_)(__)(__)(____)(_)\_) (__) (____)(_)\_)(__)(__)(_)\_)\___)(____)

	'
# Display possible choices.
CHOICES="RESTART_POSTGRESQL RESTART_APACHE VACUUM_DATABASE RUN_IMPORTS HELP EXIT"
# Define commands for choices.
select choice in $CHOICES; do
	if [ "$choice" = "RESTART_POSTGRESQL" ]; then
		/etc/rc.d/init.d/postgresql restart
		echo ""
		echo ""
		echo ""
		echo ""
		echo "Finished restarting PostgreSQL."
		echo ""
		echo "Press ENTER to continue."
		read
		clear
	elif [ "$choice" = "RESTART_APACHE" ]; then
		service httpsd restart
		echo ""
		echo ""
		echo ""
		echo ""
		echo "Finished restarting Apache."
		echo ""
		echo "Press ENTER to continue."
		read
		clear
	elif [ "$choice" = "VACUUM_DATABASE" ]; then
		su ********-c "/usr/bin/vacuumdb -* *******"
		echo ""
		echo ""
		echo ""
		echo ""
		echo "Finished vacuuming the database."
		echo ""
		echo "Press ENTER to continue."
		read
		clear
	elif [ "$choice" = "RUN_IMPORTS" ]; then
		su ********-c "/home/********/********/********/IMPORT"
		echo ""
		echo ""
		echo ""
		echo ""
		echo "Finished running the imports."
		echo ""
		echo "Press ENTER to continue."
		read
		clear
	elif [ "$choice" = "HELP" ]; then
		clear
		echo "RESTART_POSTGRES"
		echo "     - Useful for when SOEFIE giving errors, or functioning slowly."
		echo ""
		echo "RESTART_APACHE"
		echo "     - When changes are made to websites, or websites are not working."
		echo ""
		echo "VACUUM_DATABASE"
		echo "     - This will vacuum the veritime database, clearing our all of the junk."
		echo ""
		echo "RUN_IMPORTS"
		echo "     - Runs the imports from the AS/400 to SOEFIEs database."
		echo ""
		echo "HELP"
		echo "     - Displays this menu"
		echo ""
		echo "EXIT"
		echo "     - Exits the program."
		echo ""
		echo ""
		echo ""
		echo ""
		echo "Press ENTER to continue."
		read
		clear
	elif [ "$choice" = "EXIT" ]; then
		clear
		exit
	else
		echo ""
		echo ""
		echo ""
		echo ""
		echo "Please choose a number from the list."
		echo ""
		echo "Press ENTER to continue."
		read
		clear
	fi
# Display the title after each choice again.
	echo '
     __  __    __    ____  _  _  ____  ____  _  _    __    _  _  ___  ____
    (  \/  )  /__\  (_  _)( \( )(_  _)( ___)( \( )  /__\  ( \( )/ __)( ___)
     )    (  /(__)\  _)(_  )  (   )(   )__)  )  (  /(__)\  )  (( (__  )__)
    (_/\/\_)(__)(__)(____)(_)\_) (__) (____)(_)\_)(__)(__)(_)\_)\___)(____)

	'
done

Some parts taken out to protect our security….

I know there are other ways to do what I achived here, but as I said before, this is my VERY FIRST shell script.


delete

Excel VBScript – Delete Row if Column “X” = “Value”

This is the first entry in my new “Code” category, as I do a LOT of coding for work lately.

So the following script is one of my firsts that I wrote for work.

Sub Delete_Unchanged_Prices()

'''''''''''''''''''''''''''''''''''''''''''''''''''''''
'  _______        _            _                      '
' |__   __|      | |          | |                     '
'    | | ___  ___| |__        | | __ ___      ____ _  '
'    | |/ _ \/ __|  _ \   _   | |/ _` \ \ /\ / / _` | '
'    | |  __/ (__| | | | | |__| | (_| |\ V  V / (_| | '
'    |_|\___|\___|_| |_|  \____/ \__,_| \_/\_/ \__,_| '
'                                                     '
'           www.techjawa.com/category/code            '
'             www.corey.degrandchamp.com              '
'                                                     '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
'                                                     '
' Written  By: Corey DeGrandchamp                     '
' Written  On: December 10, 2008                      '
' Written For: Michigan Drill Corporation             '
' Description: This Excel VBScript will search column '
'              B, and if a cell in B has a null value '
'              then the script deltes the entire row. '
'              This is mainly used for our end of the '
'              year cost changes, but can be modified '
'              to suit your own needs.                '
'                                                     '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
'                                                     '
' Edit   Date: June 18, 2009                          '
' About  Edit: Added information.                     '
'                                                     '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''

  Application.ScreenUpdating = False
  Application.Calculation = xlCalculationManual
  Dim Rng As Range, ix As Long
  Set Rng = Intersect(Range("B:B"), ActiveSheet.UsedRange)
  For ix = Rng.Count To 1 Step -1
      If Trim(Replace(Rng.Item(ix).Text, Chr(160), Chr(32))) = "" Then
        Rng.Item(ix).EntireRow.Delete
      End If
  Next
done:
  Application.Calculation = xlCalculationAutomatic
  Application.ScreenUpdating = True
End Sub

It basically goes through and checks every row in column B, and if there is a cell with a value of “” (AKA null) then it removes the entire row.

This can be changed to any column/value combination, but this is the specific combination we need at work.






Powered by WP VideoTube