Mint Serena 64 bit using the Mate desktop environment. Most of the following scripts will run on this machine, although some have only been tested with the Gnome desktop. Extra software requirements can be met by the Ubuntu and safe PPA repositories. Scripts may be re-written or added to from time to time, the date serves as the version number. Syntax highlighting on these pages is provided by GVim's "Convert to HTML" option.

Bittorrent Monitor

#!/bin/bash
# Filename: dlfmon
# Author: robz
# Version: 091110
# Used with "Transmission" to monitor the "Torrents/Incomplete" folder.
# I have "unmetered" bandwidth in the small hours but dislike leaving my PC
# switched on all the time, (greener:)) so this is what this script is for.
# You may need "trickle" and "rtcwake" from the repositories and "shutdown"
# added to the sudoers list.
# In a terminal do "sudo visudo" then at the bottom of the list paste this:
# "%user ALL=NOPASSWD: /sbin/shutdown" all without quotes and replacing "user"
# with your user name, then do ":wq" to save and exit. Ref: man visudo.
# Use this line for your launcher, alter to suit.
# gnome-terminal --geometry 50x29+20+50 -x /home/user/.scripts/dlfmon

## Vars.
IN=7200                                     # Halt pc in - seconds.
SEED=600                                    # Default seed for - seconds.
EVERY=30                                    # Test dir every - seconds.
UMDL=02:15                                  # Unmetered download start.
CHECK=1                                     # Checks done counter.
AT=`date -d $UMDL +%s`                      # Date/time re-start - epoch.
DIRI=/home/$USER/Torrents/Incomplete/
DIRC=/home/$USER/Torrents/Complete/
LOG=/home/$USER/Torrents/torrents.log
PROG="transmission-gtk"                     # monitor this prog.

stats() ## Function download/upload stats ---------------------------------##
{
for n in {2..5}; do
    RAW=`vnstat --dumpdb -i ppp0 | grep "h;$n;"`
    UP=$(echo $RAW | cut -d ";" -f5)
    DOWN=$(echo $RAW | cut -d ";" -f4)
    let "DOWN_TOTAL += DOWN"
    let "UP_TOTAL += UP"                        # Loop vnstat for traffic.
done
UP_TOTAL=$(echo "scale=2; $UP_TOTAL / 1024" | bc)
DOWN_TOTAL=$(echo "scale=2; $DOWN_TOTAL / 1024" | bc)
echo "Traffic for this session:" >> $LOG
echo -n "Up: "$UP_TOTAL"MiB | Down: "$DOWN_TOTAL"MiB | " >> $LOG
echo -n "Total: "$(echo "scale=2; $UP_TOTAL + $DOWN_TOTAL" | bc) >> $LOG
echo -e "MiB\nThese figures might also include the auto update cron job\n"\
"check the Update Manager for new downloads.\n" >> $LOG
}

## shutdown nicely function.
haltpc ()
{
killall -w -q "$PROG" && stats
echo "Run completed, PC shutdown at: "$(date +%H:%M:%S) | tee -a $LOG
echo -e "\n==============================================================\n"\
>> $LOG
# "shutdown" needs to be added to sudoers list for next bit to work.
sync && sudo shutdown -h now
exit
}

## Suspend, Wait or Start now.
tput bold; tput setaf 6;
ANS=""
read -p "Do you wish to suspend and restart at $UMDL? (y)es (n)ext " ANS
if [ "$AT" -lt `date +%s` ]; then               # Is time set already gone?
    AT=$((AT + 86400))                          # if true add a day.
fi
WAKE=$(($AT - `date +%s`))                      # Calc. wake in $AT seconds.
if [ $ANS = "y" ]; then                         # Deferred start yes?
    sudo /bin/true                              # Get root privileges.
    tput setaf 3
    sudo killall rtcwake; clear                 # Kill any residual process
    echo "Suspend until $UMDL option chosen -------" | tee -a $LOG
    echo -e "To cancel - Ctrl+c within the next 10 seconds.\n"
    sleep 10; poff -a                           # Dissconnect
    sync && sudo rtcwake -u -s $WAKE -m on &    # Set wake-up (background).
    sleep 2; sudo /usr/sbin/pm-suspend          # Workaround for rtcwake bug
    ############################################# PC suspended here ########
    xset dpms force off                         # Awake with VDU off.
    CHECK=0; sleep 20; pon adsl; sleep 20       # Start & test for ADSL.
    while ! ping -c 1 www.google.com > /dev/null && let "CHECK += 1"; do
        if [ "$CHECK" -gt "18" ]; then          # Ping for 180 seconds.
            echo "Torrent log for "$(date +%a" "%x) >> $LOG
            echo "Network connection unreachable" >> $LOG
            haltpc                              # No internet? call shutdown.
        fi
    sleep 10
    done
elif [ $ANS = "n" ]; then
    clear
    read -p "Do you wish to countdown to $UMDL (y)es (n)ext " ANS
    if [ $ANS = "y" ]; then
        clear; tput setaf 3; tput civis
        echo "Wait until $UMDL option chosen -------" | tee -a $LOG
        for i in $(eval echo {$WAKE..0..10}); do
            printf "\r%s""Countdown to unmetered ADSL: "$i" seconds     "
            tput el
            sleep 10
        done
    fi
fi

## Start Bittorrent client Transmission GUI, with limited bandwidth
echo "Torrent log for "$(date +%a" "%x) >> $LOG
"$PROG" &
# TODO
# Required parameters already set for GUI, but maybe the daemon or remote
# option would be a better choice to avoid potential config. conflicts.

## Check dir, ready/or not? - start process when true.
clear; tput bold
while $(ls -A $DIRI); do
    tput cup 0 0; tput smso
    printf "\r%s"">>>>> Waiting for files in >>>>>\n$DIRI"
    sleep 5
done
clear; tput rmso

## Write details of download to log.
tput bold; tput setaf 6; tput civis
echo -ne "Directory: " | tee -a $LOG
tput setaf 7
echo -e "\"$DIRI\"" | tee -a $LOG
tput setaf 6; tput sc
echo "Waiting 60 seconds for compliant peers"; sleep 60
tput rc
echo "Files found:                          " | tee -a $LOG
tput setaf 7
echo -e "$(ls -1 $DIRI)\n" | tee -a $LOG        # List and log.
tput setaf 6
echo -e "PC will be shut down when seeding is complete"
echo -e "Run started: "$(date +%a" "%x" at: "%H:%M:%S) | tee -a $LOG
DSET=$((`date +%s`+IN))                         # Default shutdown from epoch.
echo -e "and will finish no later than: "`date -d @$DSET +%H:%M:%S`"\n"
tput sc
echo -e "The directory is monitored at $EVERY""s intervals."

## Start timer
#  Check /Incomplete dir. for content, if [time up or empty] then next.
CHECK=1
while [[ $CHECK*$EVERY -lt $IN && "$(ls -A $DIRI)" ]]; do
    printf "\r%s""Working..............checks made so far: ""$CHECK "
    sleep $EVERY
    CHECK=$((CHECK+1))
done
echo

## If /Incomplete dir. is true then shutdown function.
if [[ "$(ls -A $DIRI)" ]]; then
    tput setaf 1; tput rc
    echo -e "--------------------------------------------------------------\n\
Default time limit reached torrents incomplete!\n\
$(ls -1 $DIRI)\n\
--------------------------------------------------------------" | tee -a $LOG
    haltpc                                      # shutdown function.
fi

## Torrents completed before default? then seed.
tput rc
echo -e "--------------------------------------------------------------\n\
Completed files transferred out at `date +%T`" | tee -a $LOG
echo -n "to directory " | tee -a $LOG
tput setaf 7
echo -e "\"$DIRC\"\n\
--------------------------------------------------------------" | tee -a $LOG
echo -ne "\"$DIRI\""
tput setaf 6
echo -e " is now empty."
DIFF=$((DSET-`date +%s`))                       # default time remaining.
if [ "$DIFF" -le "$SEED" ]; then
    SEED=$DIFF
fi

## Seed time allocation.
if [ "$SEED" -gt "0" ]; then
    echo -e "Torrent residual seed $SEED seconds" >> $LOG
    tput setaf 1
    for i in $(eval echo {$SEED..0}); do        # count down from seed value.
        printf "\r%s""This computer will shut down in: "$i
        tput el
     sleep 1
    done
fi
echo
haltpc                                          # shutdown function.




Powered by ScribeFire.

No comments: