Skip to main content
Welcome guest. | Register | Login | Post

execute on ctrl-c before terminating previous command

4 replies [Last post]
libervisco's picture
Offline
Joined: 2006-05-04

I've been looking for a bandwidth monitor of some kind that will keep some kind of nicely readable log even after disconnection (compared to iptraf, bwm and some others).
All monitors I've seen were just too complicated for this task that should be rather simple (they require mysql, php and whatnot). So I did a simple script that does the job. I will post it on my blog if you want to see it, since that's about the first really useful thing I actually wrote myself in some kind of "computer" language (bash scripting this time). It's too simple of course, but yeah. Laughing out loud

Anyway, the thing is that it uses "ifconfig ppp0" command to get the RX and TX data and I want it to run and log that data every time I terminate connection, but BEFORE it is actually terminated. If the script runs after connection is already gone and ppp0 interface is down already it is too late and there's nothing to log.

The script would first run "pppd call edge" as that's the connect command for me. That would establish ppp connection which can be terminated by "ctrl-c" command. What I want now is that when I do "ctrl-c" to terminate connection it first has to run "/etc/ppp/ppplog/ppptraf.sh" (my log script) and only then actually terminate connection.

I tried this with this command:

trap '/etc/ppp/ppplog/ppptraf.sh' SIGINT

but it is still too late as it runs that after ppp0 is already down.

So I'm looking for a way to run a command at "ctrl-c" but before "ctrl-c" terminates the previous command.. It's a bit tricky but I think it should be possible somehow, or at least something to that effect.

Any ideas?

Thanks

dylunio's picture
Offline
Joined: 2005-12-20

Hmm, I wouldn't go around the problem quite the same way as you, though it my be slightly more tedious.
You write a script that logs the bandwidth, and then kills the connection, so something like:

#!/bin/bash

#Log bangwidth
Foo Bar Baz

#Kill connection
killall ppp #or something like this

and then when you want to switch off the connection you just run the script.

libervisco's picture
Offline
Joined: 2006-05-04

Yes, that's what I came to afterall..

That's actually good for me. I made a script called "hail" that basically just runs "pppd call edge" and another script called "unhail" that does "killall pppd" and runs the log script (as you described it above).

However, the pppd command poseses the terminal windows so it can't be used afterwards and the unhail has to be ran from another terminal..

Is there a way to free the terminal once the command has ran the pppd?

Even better would be if a script could run pppd command and then move on to the next command (like the one starting my dvb connection) instead of just standing at the pppd line.

Thanks

dylunio's picture
Offline
Joined: 2005-12-20

Erm, so you want to run hail in a terminal and not have any ouput? If so you could try running it in the background: hail & or if that doesn't work try putting a redirect on the output, so within hail you'd have: pppd call edge &> /dev/null

I hope this helps;

dylunio

libervisco's picture
Offline
Joined: 2006-05-04

Thanks, that did helped. Maybe it isn't the nicest solution, but I created a "hail" file that only launches "/usr/bin/hail.sh &" with that &quo