script for using trim on a ssd and a wake on lan script

Here you can exchange scripts that you created or have permission to share with other users.
Post Reply
Message
Author
User avatar
GDixon
Posts: 51
Joined: Fri Nov 02, 2007 4:39 pm

script for using trim on a ssd and a wake on lan script

#1 Post by GDixon »

I'm working on learning how to make a pretty user interface and searching for something like kdialog. I will give zenity a try and repost when it's done in the future but for now heres something you can run in a terminal.

If you make any changes to make these scripts better which shouldn't be too hard please share them with everyone :)

you will have to make simple changes depending on where you put the script and the sda's that are ssd you might have.
You will notice i use sleep so you can read some fstrim output then clear the screen and return to the menu. Use q or Q to quit. The sleep time is seconds and you can change it to what you prefer.

Code: Select all

#!/bin/bash


# Script has to be run as root/superuser 

# This Script is to be used to trim a ssd drive

# trim command is /sbin/fstrim -v   Use of the -v is for verbose
# command for root                           /sbin/fstrim -v /
# command for home                           /sbin/fstrim -v /home
# command for additional drive/partition     /sbin/fstrim -v /media/Storage

# place script in /usr/local/bin and make executable or
# anywhere in your path where you place your scripts
# you will have to make changes in the script if other than /usr/local/bin

# This is to check if your root/superuser and if not exits and reminds you

if [ $(id -u) != "0" ]; then
/usr/bin/clear
    echo
    echo -e "You must be root/superuser to run this script\n" >&2
    exit 1
fi

# definition of sda1, sda2, sda3, ALL sda's

# sda1 /               added as 1
# sda2 /home           added as 2
# sda3 /media/Storage  added as 3
# All                  added as a or A

sda1=/
sda2=/home
sda3=/media/Storage

while [ "$input1" != quit ]; do
    echo
    echo -e "Which SSD would you like to Trim?\n"

# These are menu choice descriptions below

    echo -e "1) Trim sda1 /\n"
    echo -e "2) Trim sda2 /home\n"
    echo -e "3) Trim sda3 /media/Storage\n"
    echo -e "a) Trim sda1, sda2, sda3\n"
    echo -e "q) quit and take no further action\n"
    echo -e "Waiting on your choice\n"

# to trim one at a time

#sda1 /

read input1
if [ $input1 == 1 ]; then
/usr/bin/clear
    echo
    echo -e "Trim running on sda1\n"
/sbin/fstrim -v $sda1
sleep 3; /usr/bin/clear; /usr/local/bin/trim.sh
exit 1
fi

#sda2 /home

if [ $input1 == 2 ]; then
/usr/bin/clear
    echo
    echo -e "Trim running on sda2\n"
/sbin/fstrim -v $sda2
sleep 3; /usr/bin/clear; /usr/local/bin/trim.sh
exit 1
fi

#sda3 /media/Storage

if [ $input1 == 3 ]; then
/usr/bin/clear
    echo
    echo -e "Trim running on sda3\n"
/sbin/fstrim -v $sda3
sleep 3; /usr/bin/clear; /usr/local/bin/trim.sh
exit 1
fi

# to trim all at once

if [ $input1 == A ] || [ $input1 == a ]; then
/usr/bin/clear
    echo
    echo -e "Trim running on sda1\n"
/sbin/fstrim -v $sda1
    echo
    echo -e "Trim running on sda2\n"
/sbin/fstrim -v $sda2
    echo
    echo -e "Trim running on sda3\n"
/sbin/fstrim -v $sda3
sleep 4; /usr/bin/clear; /usr/local/bin/trim.sh
exit 1
fi

if [ $input1 == Q ] || [ $input1 == q ]; then
/usr/bin/clear
    echo
    echo -e "later gator!\n"
sleep 3; /usr/bin/clear    
exit 1
fi

done



Heres a very similiar one that can be used for wake on lan and i called it wol.sh
I used wakeonlan because etherwake didn't wake my systems but wakeonlan does.
Test which will work for you and use the one that works.
Where you need to make your changes to match your system should be obvious if not just ask.



Code: Select all

#!/bin/bash

# get wakeonlan from the repository and install first
# place script in /usr/local/bin and make executable so everyone can use

# definition of MAC addresses for network

# macs for Acer desktop 
# Realtek   90:FB:A6:E4:E4:0A
# Wireless  1C:65:9D:15:DA:1C

# menu definitions

# Acer1 desktop realtek  added as 1
# Acer2 desktop wireless added as 2
# HTPC added as   H or h
# Server added as S or s
# Both added as   B or b

# mac definitions

Acer1=90:FB:A6:E4:E4:0A
Acer2=1C:65:9D:15:DA:1C
HTPC=8c:89:a5:37:9d:94
Server=00:40:10:10:00:03

while [ "$input1" != quit ]; do
echo
echo -e "Which PC would you like to wake?\n"

# These are choice descriptions below

echo -e "1) Acer1\n"
echo -e "2) Acer2\n"
echo -e "h) HTPC\n"
echo -e "s) Server\n"
echo -e "b) wake Server, wait 60sec, then wake HTPC\n"
echo -e "q) quit and take no further action\n"
echo -e "Waiting on your choice\n"

read input1
if [ $input1 == 1 ]; then
/usr/bin/clear
echo
/usr/bin/wakeonlan $Acer1
echo
sleep 1; /usr/bin/clear; /usr/local/bin/wol.sh
exit 1
fi

if [ $input1 == 2 ]; then
/usr/bin/clear
echo
/usr/bin/wakeonlan $Acer2
echo
sleep 1; /usr/bin/clear; /usr/local/bin/wol.sh
exit 1
fi

if [ $input1 == H ] || [ $input1 == h ]; then
/usr/bin/clear
echo
/usr/bin/wakeonlan $HTPC
echo
sleep 1; /usr/bin/clear; /usr/local/bin/wol.sh
exit 1
fi

if [ $input1 == S ] || [ $input1 == s ]; then
/usr/bin/clear
echo
/usr/bin/wakeonlan $Server
echo
sleep 1; /usr/bin/clear; /usr/local/bin/wol.sh
exit 1
fi

# to wake Server before the HTPC wakes

if [ $input1 == B ] || [ $input1 == b ]; then
/usr/bin/clear
echo
/usr/bin/wakeonlan $Server
echo
echo -e "Server waking, now waiting for 60sec then waking HTPC\n"
sleep 60
/usr/bin/wakeonlan $HTPC
echo
sleep 2; /usr/bin/clear; /usr/local/bin/wol.sh
exit 1
fi

if [ $input1 == Q ] || [ $input1 == q ]; then
/usr/bin/clear
echo
echo -e "later gator!\n"
sleep 2; /usr/bin/clear
exit 1
fi

done

User avatar
Jerry3904
Administrator
Posts: 21881
Joined: Wed Jul 19, 2006 6:13 am

Re: script for using trim on a ssd and a wake on lan script

#2 Post by Jerry3904 »

Looks good, thanks. I finally looked up Wake on LAN to see exactly how it works:
Ethernet connections, including home and work networks, wireless data networks and the internet itself, are based on packets of data sent between computers. Wake-on-LAN ("WOL") is implemented using a specially designed packet called a magic packet, which is sent to all computers in a network, among them the computer to be woken up. The magic packet contains the MAC address of the destination computer, an identifying number built into each network interface card ("NIC") or other ethernet device in a computer, that enables it to be uniquely recognized and addressed on a network. Powered-down or turned off computers capable of Wake-on-LAN will contain network devices able to "listen" to incoming packets in low-power mode while the system is powered down. If a magic packet is received that is directed to the device's MAC address, the NIC signals the computer's power supply or motherboard to initiate system wake-up, much in the same way as pressing the power button would do.
Production: 5.10, MX-23 Xfce, AMD FX-4130 Quad-Core, GeForce GT 630/PCIe/SSE2, 16 GB, SSD 120 GB, Data 1TB
Personal: Lenovo X1 Carbon with MX-23 Fluxbox and Windows 10
Other: Raspberry Pi 5 with MX-23 Xfce Raspberry Pi Respin

User avatar
GDixon
Posts: 51
Joined: Fri Nov 02, 2007 4:39 pm

Re: script for using trim on a ssd and a wake on lan script

#3 Post by GDixon »

Made some changes to the trim script so it prompts for the root password and uses a user name of your choice, I can make it use the logged in name but when you sign in as root the name changes to root so for now i made the name as a choice. if anyone has a suggestion on fixing this ugly way to use a name let me know.

I'm still working out how to get a dialog box like is uesd in synaptic or check apt GPG for root log in.

I also made the wol script use the logged name so no need to make a choice since it isn't run as root.


Code: Select all

#!/bin/bash


# Script has to be run as root/superuser 

# This Script is to be used to trim a ssd drive

# trim command is /sbin/fstrim -v   Use of the -v is for verbose
# command for root                           /sbin/fstrim -v /
# command for home                           /sbin/fstrim -v /home
# command for additional drive/partition     /sbin/fstrim -v /media/Storage

# place script in /usr/local/bin and make executable or
# anywhere in your path where you place your scripts
# you will have to make changes in the script if other than /usr/local/bin

# This is to check if your root/superuser and if not prompts for the root password

if [ $(id -u) != "0" ]; then
/usr/bin/clear
    echo
    echo -e "You must be root/superuser to run this script $USER\n" >&2

# su -c ''  will give a password prompt for this sesion and then close when you quit the script

su -c '/usr/local/bin/trim.sh'

    exit 1
fi

# definition of sda1, sda2, sda3, ALL sda's

# sda1 /               added as 1
# sda2 /home           added as 2
# sda3 /media/Storage  added as 3
# All                  added as a or A

# Variables that can be changed to match system

name=Greg   # insert name of script user you would like to use
sda1=/
sda2=/home
sda3=/media/Storage

# clear the terminal to start fresh

/usr/bin/clear

while [ "$input1" != quit ]; do
    echo
    echo -e "Which SSD would you like to Trim $name?\n"

# These are menu choice descriptions below

    echo -e "1) Trim sda1 /\n"
    echo -e "2) Trim sda2 /home\n"
    echo -e "3) Trim sda3 /media/Storage\n"
    echo -e "a) Trim sda1, sda2, sda3\n"
    echo -e "q) quit and take no further action\n"
    echo -e "Waiting on your choice $name\n"

# to trim one at a time

#sda1 /

read input1
if [ $input1 == 1 ]; then
/usr/bin/clear
    echo
    echo -e "Trim running on sda1 $name\n"
/sbin/fstrim -v $sda1
sleep 3; /usr/bin/clear; /usr/local/bin/trim.sh
exit 1
fi

#sda2 /home

if [ $input1 == 2 ]; then
/usr/bin/clear
    echo
    echo -e "Trim running on sda2 $name\n"
/sbin/fstrim -v $sda2
sleep 3; /usr/bin/clear; /usr/local/bin/trim.sh
exit 1
fi

#sda3 /media/Storage

if [ $input1 == 3 ]; then
/usr/bin/clear
    echo
    echo -e "Trim running on sda3 $name\n"
/sbin/fstrim -v $sda3
sleep 3; /usr/bin/clear; /usr/local/bin/trim.sh
exit 1
fi

# to trim all at once

if [ $input1 == A ] || [ $input1 == a ]; then
/usr/bin/clear
    echo
    echo -e "Trim running on sda1 $name\n"
/sbin/fstrim -v $sda1
    echo
    echo -e "Trim running on sda2 $name\n"
/sbin/fstrim -v $sda2
    echo
    echo -e "Trim running on sda3 $name\n"
/sbin/fstrim -v $sda3
sleep 4; /usr/bin/clear; /usr/local/bin/trim.sh
exit 1
fi

if [ $input1 == Q ] || [ $input1 == q ]; then
/usr/bin/clear
    echo
    echo -e "later gator!\n"
sleep 3; /usr/bin/clear    
exit 1
fi

done

wol with changes

Code: Select all

#!/bin/bash

# get wakeonlan from the repository and install first
# place script in /usr/local/bin and make executable so everyone can use

# definition of MAC addresses for network

# macs for Acer desktop 
# Realtek   90:FB:A6:E4:E4:0A
# Wireless  1C:65:9D:15:DA:1C

# menu definitions

# Acer1 desktop realtek  added as 1
# Acer2 desktop wireless added as 2
# HTPC added as   H or h
# Server added as S or s
# Both added as   B or b

# mac definitions/Variables that can be changed to match system needs

Acer1=90:FB:A6:E4:E4:0A
Acer2=1C:65:9D:15:DA:1C
HTPC=8c:89:a5:37:9d:94
Server=00:40:10:10:00:03

while [ "$input1" != quit ]; do
echo
echo -e "Which PC would you like to wake up $USER?\n"

# These are choice descriptions below

echo -e "1) Acer1\n"
echo -e "2) Acer2\n"
echo -e "h) HTPC\n"
echo -e "s) Server\n"
echo -e "b) wake Server, wait 60sec, then wake HTPC\n"
echo -e "q) quit and take no further action\n"
echo -e "Waiting on your choice $USER\n"

read input1
if [ $input1 == 1 ]; then
/usr/bin/clear
echo
/usr/bin/wakeonlan $Acer1
echo
sleep 1; /usr/bin/clear; /usr/local/bin/wol.sh
exit 1
fi

if [ $input1 == 2 ]; then
/usr/bin/clear
echo
/usr/bin/wakeonlan $Acer2
echo
sleep 1; /usr/bin/clear; /usr/local/bin/wol.sh
exit 1
fi

if [ $input1 == H ] || [ $input1 == h ]; then
/usr/bin/clear
echo
/usr/bin/wakeonlan $HTPC
echo
sleep 1; /usr/bin/clear; /usr/local/bin/wol.sh
exit 1
fi

if [ $input1 == S ] || [ $input1 == s ]; then
/usr/bin/clear
echo
/usr/bin/wakeonlan $Server
echo
sleep 1; /usr/bin/clear; /usr/local/bin/wol.sh
exit 1
fi

# to wake Server before the HTPC wakes

if [ $input1 == B ] || [ $input1 == b ]; then
/usr/bin/clear
echo
/usr/bin/wakeonlan $Server
echo
echo -e "Server waking, now waiting for 60sec then waking HTPC $USER\n"
sleep 60
/usr/bin/wakeonlan $HTPC
echo
sleep 2; /usr/bin/clear; /usr/local/bin/wol.sh
exit 1
fi

if [ $input1 == Q ] || [ $input1 == q ]; then
/usr/bin/clear
echo
echo -e "later gator!\n"
sleep 2; /usr/bin/clear
exit 1
fi

done

Post Reply

Return to “Scripts”