This is a small Tutorial which helps us in making a script and running it again and again to Shut down PC after all my downloads complete !! I am using it on Ubuntu but it should work on other Linux distros too!
#power_off Shell Script
no_cons=`netstat -nt | egrep ESTABLISHED | wc -l`
echo "$no_cons Connections" #Display no. of active Connections if [ $no_cons -eq 0 ]thenecho "Your sys Idle now shutting it down"sudo shutdown -P 5 # will shutdown PC in 5 minutes if 0 connectionelseecho "Still Downloading...." #displays message if download is on !!fi
save this script it in your home directory and make it executable by writing :
On Terminal:
chmod +x power_off #power_off is the name of the script here !!
now we have to make a cronjob so that we can run again and again at specified time intervals(like every 5 minutes or so) !!
For this we will use cron utility which helps in scheduling jobs. For more on cron have a look at http://kevin.vanzonneveld.net/techblog/article/schedule_tasks_on_linux_using_crontab/
On terminal:
crontab -e #edit the crontab file
this will open up an editor (as given in EDITOR environment variable in ~/.bashrc)
now in this file in a new line write
*/5 * * * * ~/power_off
# This will run our script every 5 minutes after you quit your editor if no errors it will show "crontab: installing new crontab" !!
now the only problem we are left to solve is that to run shutdown -P as a normal user without prompting a password !! For more on this refer to https://help.ubuntu.com/community/Sudoers#Common Tasks from where the following part is copied:
On Terminal:
sudo visudo
An editor will open where do the following:
Now we are done with this save it and quit if no errors it will come out !!
Note: Please close any browser window with a site which updates it automatically otherwise you sys will not shutdown (coz a connection is always)!!
Hope this helps someone! till then bye Jai FOSS !!
PS: Thanks to Mr. Arun Khan who gave me imp. pointers for the script above (on ILUGC ML) !!
On Terminal:
sudo visudo
An editor will open where do the following:
Add some cmnd aliases (under their something like #Cmnd_Alias heading in the file opened) as follows:
Cmnd_Alias SHUTDOWN_CMDS = /sbin/shutdown, /sbin/halt, /sbin/reboot
You also need to add a user specification (at the end of the file after the "%admin ALL = (ALL) ALL" line so it takes effect - see above for details):
ALL=(ALL) NOPASSWD: SHUTDOWN_CMDS
Obviously you need to replace "" with the username of the user who needs to be able to shutdown the pc without a password. You can use a user alias here as normal.
Now we are done with this save it and quit if no errors it will come out !!
Note: Please close any browser window with a site which updates it automatically otherwise you sys will not shutdown (coz a connection is always)!!
Hope this helps someone! till then bye Jai FOSS !!
PS: Thanks to Mr. Arun Khan who gave me imp. pointers for the script above (on ILUGC ML) !!