.

Labels

.

Auto shutdown after copying, downloading , converting etc

Many times it has happened that i had to sit in front of the computer late into the night or early morning to get some file to complete copying  or downloading. Before learning scripting i really searched and wished there was some program which could shutdown my system for me after some process is completed. This script works well for copying, downloading and when converting videos or other files.

First before using the script change the property of your shutdown command , so that you don't need to type your password for executing shutdown. Run the following command in terminal :
sudo chmod u+s /sbin/shutdown

There is also one more method to use shutdown without entering password by editing /etc/sodoers . But
this never worked for me ( Ubuntu 11.10 ) . If you want to use this method check this out  http://askubuntu.com/questions/168879/shutdown-from-terminal-without-entering-password   


-------------------------------------------------------------------------------------------------------------------------------------------------
HOW THIS SCRIPT WORKS:
This script continuously checks if there is any change in the  size of directory in which the process is taking place and initiates shutdown if no change is observed for a specific time.

du is a command-line tool for estimating file space usage
shutdown is command-line tool to bring the system down
bc is command-line tool to do mathematical calculation. Better if using huge values in calculation

-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------

#!/bin/bash

#for shutting the computer after completing a process
#please avoid using script in the home folder and similar folder which contains files having access permissions denied

#change interval time to your need
interval=5s

echo ".. started checking every $interval" 

then=0;

for (( i=1; i>0; i++ ));do
   sleep $interval
   now=$(du -c "$PWD" | grep total | sed 's/total//g' )
   check=$(echo "$now-$then" | bc )

   if [[ $check == "0" ]];then
        echo -e "No change in data size of current directory noticed.\nInitiating shutdown in 1 minute ..
        shutdown -h 1
   else
        then=$now
   fi
done

-------------------------------------------------------------------------------------------


To use the script inside terminal move to the directory where the copying or downloading is carried out and just run the script. Change the value of interval  to the time you find suiting. With a little bit of imagination you can change the script for many purposes. If you dont want to shutdown after completing the process , change it to something else like playing a song through mplayer or something said through espeak !!

 Now no need to waste your precious time waiting !!

Enjoy Linux !!
Dont know what to do with these codes ??  click here

No comments:

Post a Comment