LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
LinkBack Search this Thread
Old 05-04-2008, 05:51 AM   #1
creativetweak
LQ Newbie
 
Registered: May 2008
Distribution: Debian
Posts: 6

Rep: Reputation: 0
Start and stop a bash script based on network activity?


I have a wget script that I use to queue large downloads. Currently I use cron to shedule it to run late at night and kill it in the morning so I still have all of my limited bandwidth available during the day.

What I'm wanting to do is remove the cron entries and have the script start with the machine. Then I need somthing to stop the script when any of the other machines on the networks send any interesting packets (ie http, ftp, etc)

Any ideas?
 
Old 05-04-2008, 06:26 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 21,610
Blog Entries: 47

Rep: Reputation: 1413Reputation: 1413Reputation: 1413Reputation: 1413Reputation: 1413Reputation: 1413Reputation: 1413Reputation: 1413Reputation: 1413Reputation: 1413
Wouldn't it be easier to shape bandwidth? It can be done so the class wget is in starves when other prio streams are necessary.
 
Old 05-04-2008, 06:33 AM   #3
creativetweak
LQ Newbie
 
Registered: May 2008
Distribution: Debian
Posts: 6

Original Poster
Rep: Reputation: 0
From memory shaping dowstream is painfull and wastefull as there is no way to slow it short of dropping packets and hoping the sender gets the idea and reduces its tcp window.

If I'm way off let me know...
 
Old 05-04-2008, 07:10 AM   #4
seraphim172
Member
 
Registered: May 2008
Posts: 101

Rep: Reputation: 15
keep it simple

Leave cron settings as they are, put your conditional branches into the script that is called by cron.

The definition of "interesting packets (ie http, ftp, etc)" on a network is a nice, but not very helpful term. If you are doing a gigabyte download with wget and you open your browser for a Google search, what do you want to happen? Abort wget even if only 2 bytes are missing from the full download, or keep it running even if you only had 2 bytes downloaded so far.

Setting '--limit-rate' for wget will only apply to the next download. Wget will certainly retry downloading later. I don't know if it can resume exiting ones and how reliable that is.

I would rather make the script to look for a certain value in a prefs file to see if a next wget download can begin. The prefs file can be a text file with "run" or "pause" as the content, written by hand or by a webpage script.

Not very sophisticated, but quite simple. That's what I would do.

Linux Archive

Last edited by seraphim172; 06-05-2008 at 10:39 AM.
 
Old 05-04-2008, 08:06 AM   #5
creativetweak
LQ Newbie
 
Registered: May 2008
Distribution: Debian
Posts: 6

Original Poster
Rep: Reputation: 0
If I had a gigabyte+ download running and someone fires up a browser the download should be dropped instantly and hope that wget can resume downloading.

Im trying to throw together a bash script at the moment that will pipe interesting packets (with the help of some tcpdump filters) into a loop that keeps track of the last interesting packet. when the time since the last interesting packet is greater then some threshold (say 5 mins) then start the wget script. If there has been a packet in the last 5 mins then make sure the script is stopped.
 
Old 05-04-2008, 08:33 AM   #6
creativetweak
LQ Newbie
 
Registered: May 2008
Distribution: Debian
Posts: 6

Original Poster
Rep: Reputation: 0
Here it is so far... Not perfect... there seems to be some buffering happening somehwere and I dont like relying on saving the lastpacket time to disk... But it does serve as an example of what im looking for if anyone knows of anything similar already out there...

Code:
idletime=5		# how long between interesting packets before starting?
date +%s > lastpacket
status="stopped"

# 'interesting ports'.. only port 80 for now, also need to filter out traffic
# to/from localhost.
tcpdump port 80 | while read line; do
	date +%s > lastpacket
done&

while [ 1 ]; do
	sleep 1
	lastime=`cat lastpacket`
	curtime=`date +%s`
	elapsed=`expr $curtime - $lastime`

	if [ $elapsed -gt $idletime ] && [ $status = "stopped" ] ; then
		echo "Start script"
		status="running"
	fi

	if [ $elapsed -lt $idletime ] && [ $status = "running" ] ; then
		echo "Stop script"
		status="stopped"
	fi
done
 
Old 05-04-2008, 07:04 PM   #7
konsolebox
Senior Member
 
Registered: Oct 2005
Location: Philippines
Distribution: Gentoo, Slackware, LFS
Posts: 1,526
Blog Entries: 5

Rep: Reputation: 98
i suggest you do elif instead:
Code:
	if [ $elapsed -gt $idletime ] && [ $status = "stopped" ] ; then
		echo "Start script"
		status="running"
	elif [ $elapsed -lt $idletime ] && [ $status = "running" ] ; then
		echo "Stop script"
		status="stopped"
	fi
and in while:
Code:
while :; do ...; done
 
Old 05-04-2008, 07:46 PM   #8
creativetweak
LQ Newbie
 
Registered: May 2008
Distribution: Debian
Posts: 6

Original Poster
Rep: Reputation: 0
Cheers, Couldnt remember the else if bash syntax... Im assuming : evaluates to true?
 
Old 05-04-2008, 10:28 PM   #9
konsolebox
Senior Member
 
Registered: Oct 2005
Location: Philippines
Distribution: Gentoo, Slackware, LFS
Posts: 1,526
Blog Entries: 5

Rep: Reputation: 98
it's not its real purpose to return true but it always does return true.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Interrupt driven script for network start and stop MrUmunhum Linux - Networking 7 01-13-2008 06:24 PM
custom init.d script will start but won't stop Qwerty9119 Linux - General 2 05-03-2007 12:23 PM
using cron to start and stop a bash script monty Linux - Software 2 03-20-2006 02:30 PM
Bash script to alert by email 3 times then stop. pmpc00 Linux - General 2 11-04-2004 07:23 AM
bash script to stop isdn service after x mintus? paul.nel Programming 4 03-11-2004 10:01 AM


All times are GMT -5. The time now is 01:15 PM.

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
Open Source Consulting | Domain Registration