LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-25-2012, 05:28 AM   #1
gnuub
LQ Newbie
 
Registered: May 2011
Posts: 13

Rep: Reputation: 2
Python/Bash script to check conditions for running "poweroff" command


Hi all.

I am trying to create a python script that I can run before going to sleep that will shut my computer off when my current torrents are done downloading.

However, i am having trouble finding a way to do this.

I tried to use the disk space (df command) as a variable, and check it against another one taken in a few minutes but it seems that once you start downloading a file it immediately takes the full files size into consideration when checking remaining disk space.

Also I cannot find a command that will give me a current, static download speed to use for the two variables.

Any ideas on how I can otherwise accomplish this task?

Last edited by gnuub; 11-25-2012 at 05:30 AM.
 
Old 11-25-2012, 05:56 AM   #2
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
I wrote a script to do this a while ago, but it is in bash:

Code:
#!/bin/ash

if test $# = 0
then
	interface=eth0
elif test $# = 1
then
	interface="$1"
else
	echo "Usage: $(basename $0) [interface]"
	exit 1
fi

# make sure the interface is sane
if ! ifconfig "$interface" grep 'RX packets' | awk '{ print $3 }' | grep '^[0-9]*$'
then
	exit 1
fi

while :
do
  oldvar="$(ifconfig "$interface" | grep 'RX packets' | awk '{ print $3 }')"
  sleep 30
  var="$(ifconfig "$interface" | grep 'RX packets' | awk '{ print $3 }')"
  if test "$oldvar" = "$var"
  then
    shutdown -h now
    exit 0
  fi
done

exit 0
It checks received packets every 30 seconds, and if you haven't received any packets it shuts down. You can change the time if you need to.

I used this for regular downloads and only rarely torrents, but it should still work because it is monitoring received packets not sent packets.

Last edited by H_TeXMeX_H; 12-04-2012 at 03:29 AM. Reason: fixed script
 
Old 11-25-2012, 03:03 PM   #3
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,970

Rep: Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849
Hi,

the program qbittorrent has an option to shut the computer down after downloading is done.

Markus
 
1 members found this post helpful.
Old 12-04-2012, 03:31 AM   #4
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
I fixed the script, it was not working properly before.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH script "if then" with multiple conditions dohpaz Programming 8 05-24-2012 04:31 PM
Bash Conditions: "(" versus "[" mstng_67 Programming 7 07-12-2010 06:08 PM
bash script: how to check stderr after command "find" Vilmerok Programming 1 07-08-2009 07:35 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
Bash Script: Problem running variable command containing "" Paasan Programming 2 01-21-2004 01:45 AM


All times are GMT -5. The time now is 05:27 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration