📚 LinuxDocs
Topics:
All Pages8021X HOWTOACP ModemACPI HOWTOADSL Bandwidth Man..ATA RAID HOWTOATM Linux HOWTOAX25 HOWTOAccessibility Dev ..Accessibility HOWTOAdv Bash Scr HOWTOAdv Routing HOWTOAntares RAID sparc..Apache Compile HOWTOApache WebDAV LDAP..Assembly HOWTOAstronomy HOWTOAthlon Powersaving..Authentication Gat..Autodir HOWTOAviation HOWTOAvr Microcontrolle..BRIDGE STP HOWTOBTTVBackspaceDeleteBandwidth Limiting..Bangla HOWTOBash Prompt HOWTOBattery PoweredBelarusian HOWTOBelgian HOWTOBeowulf HOWTOBocaBogoMipsBootdisk HOWTOBridgeC++ dlopenC C++Beautifier HO..C editing with VIM..CDROM HOWTOCDServer HOWTOCable ModemCaudium HOWTOClone HOWTOCompaq Remote Insi..Compaq T1500 HOWTOConexant+Rockwell ..Cryptoloop HOWTODB2 HOWTODHCPDSL HOWTODVD Playback HOWTODebian Binary Pack..Debian JigdoDebian and Windows..Disk Encryption HO..Disk on Chip HOWTODocBook Demystific..DocBook InstallDocBook OpenJade S..Ecology HOWTOEmacspeak HOWTOEncourage Women Li..Encrypted Root Fil..Euro Char SupportEvent HOWTOFedora Multimedia ..Finnish HOWTOFirewall PiercingFlash Memory HOWTOFont HOWTOFramebuffer HOWTOGCC HOWTOGIS GRASSGlibc Install HOWTOHOWTO HOWTOHOWTO INDEXHP HOWTOHandspring VisorHard Disk UpgradeHardware HOWTOHighQuality Apps H..Home Electrical Co..IBM7248 HOWTOIO Perf HOWTOIP AliasIP Masquerade HOWTOIRCImplement Sys Call..Indic Fonts HOWTOInfrared HOWTOIngresII HOWTOInstall StrategiesInstallation HOWTOInstallfest HOWTOIntkeybItalian HOWTOJabber Server Farm..JavaStation HOWTOKerberos Infrastru..Kernel HOWTOKerneldKodak Digitalcam H..LDAP HOWTOLDP Reviewer HOWTOLILO crash rescue ..LVM HOWTOLeased LineLegoLinksys Blue Box R..Linux+Win95Linux+Win9x+Grub H..Linux+Windows HOWTOLinux Complete Bac..Linux Crash HOWTOLinux Gamers HOWTOLinux Modem SharingLinux Promise RAID..Linux i386 Boot Co..LinuxGL QuakeWorld..Lotus DominoR5MILO HOWTOMMBase Inst HOWTOMP3 CD BurningMail User HOWTOMajordomo MajorCoo..Man PageMasquerading Simpl..Medicine HOWTOMindTerm SSH HOWTOMobile IPv6 HOWTOMock MainframeModule HOWTOModulesMotorola Surfboard..Mozilla OptimizationMulti Distro DevNCURSES Programmin..NFS HOWTONFS Root Client mi..NIS HOWTONetMeeting HOWTONetwork boot HOWTONvidia OpenGL Conf..OLSR IPv6 HOWTOOnline Troubleshoo..Oracle 9i Fedora 3..PA RISC Linux Boot..PCTel MicroModem C..PHP Nuke HOWTOPPP HOWTOPagerPalmOS HOWTOPartitionPartition Mass Sto..Partition Mass Sto..Partition RescuePine ExchangePortSlavePost Installation ..Postfix Cyrus Web ..Pre Installation C..Print2WinPrinting HOWTOProcess AccountingProgram Library HO..Proxy ARP SubnetQmail ClamAV HOWTOQmail VMailMgr Cou..Querying libiptc H..RPM HOWTOReading List HOWTORedHat CD HOWTOReliance HOWTORemote BridgingRemote Serial Cons..SCSI 2.4 HOWTOSCSI Generic HOWTOSLIP PPP EmulatorSRM HOWTOSSL Certificates H..Scanner HOWTOScientific Computi..Scripting GUI TclTkSecure CVS PserverSecure Programs HO..Security HOWTOSecurity Quickstar..Security Quickstar..Serial Laplink HOWTOSerial Programming..Slovak HOWTOSmall MemorySmart Card HOWTOSoftware Proj Mgmt..Software Release P..Sound HOWTOSpam Filtering for..Speech Recognition..SquashFS HOWTOSybase ASA HOWTOSybase ASE HOWTOSybase PHP ApacheTCP Keepalive HOWTOTamil Linux HOWTOTimePrecision HOWTOTimeSys Linux Inst..Token RingTraffic Control HO..Traffic Control tc..UPS HOWTOUnix Hardware Buye..Unix and Internet ..UpgradeUsenet News HOWTOUser Authenticatio..VB6 to TclVMS to Linux HOWTOVPN HOWTOValgrind HOWTOVideoLAN HOWTOVim HOWTOVirtual WebWebcam HOWTOWikiText HOWTOWindows Newsreader..Wireless Link sys ..Wireless Sync HOWTOXDM XtermXDMCP HOWTOXFree Local multi ..XFree86 HOWTOXFree86 R200XFree86 Second MouseXFree86 Video Timi..XML RPC HOWTOXWindow Overview H..XWindow User HOWTOXinerama HOWTOXterminalsHtml singleI810 HOWTOLibdc1394 HOWTOOpenMosix HOWTOPhhttpd HOWTOPpp sshText

11.14. Prompt Beeps After Long-Running Commands

Robb Matzke (matzke at llnl dot gov) sent me this a long time ago (sorry Robb, should have put it in sooner!). This prompt uses Perl and the builtin .:: addons.mozilla.org ::. .:: telegra.ph ::. times command to determine if the program that just finished running has used more than a certain amount of time. The assumption is that you might have changed desktops by then and notification would be nice, so it rings a bell. I've tried to avoid using Perl because the overhead is fairly high, but this is a good use for it. .:: www.fiskesnack.com ::. [Academic results for IMAP] .:: www.twitch.tv ::.

I haven't tested this prompt myself. I like the idea though. Robb includes instructions in the comments. [Reference: Oracle Database]

#!/usr/bin/perl
require 5.003;
use strict;

###############################################################################
# prompt_bell -- execute arbitrary commands contingent upon CPU time
#
# Copyright (C) 2000 Robb Matzke
#
#    This program is free software; you can redistribute it and/or modify it
#    under the terms of the GNU General Public License as published by the
#    Free Software Foundation; either version 2 of the License, or (at your
#    option) any later version.
#
#    This program is distributed in the hope that it will be useful, but
#    WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
#    Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; see the file COPYING.  If not, write to the Free
#    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
#    02111-1307, USA.
#
# Purpose:
#
#    This program is intended to be called each time a shell prompt is
#    displayed. It looks at current CPU times (user+system) for the shell and
#    its children, and if the CPU time is more than some user-specified amount
#    then user-specified commands are executed.  The author uses it to provide
#    an audio indication of when a long-running command completes.
#
# Usage:
#
#    The prompt_bell command takes two arguments: the name of a file
#    containing the latest CPU usage information for the shell and its
#    children, and some optional state information from the environment
#    variable $PROMPT_BELL_STATE.
#
#    The times file simply contains one or more times, zero or more to a line,
#    each of the form `#h#m#.#s' where `#' is a sequence of one or more
#    decimal digits and `#h' is the optional number of hours, `#m' is the
#    required number of minutes, and `#.#s' is the number of seconds and
#    fractions thereof. The total time is the sum of all the times in this
#    file. Example:
#
#        0m0.050s 0m0.060s
#        0m15.790s 0m0.220s
#
#    The output from this command is one or more semicolon-separated shell
#    commands which should be eval'd by the caller. If the difference between
#    the current CPU times and the previous CPU times (stored in environment
#    variable PROMPT_BELL_STATE) is more than $PROMPT_BELL_TIME seconds
#    (default 10) then the commands printed include the value of environment
#    variable PROMPT_BELL_CMD (default is "echo -ne '\a'").
#
#    Typical usage is:
#        eval "`prompt_bell $TIMES_FILE $PROMPT_BELL_STATE`"
#
#    and this command is usually part of the bash PROMPT_COMMAND. The author's
#    .bashrc contains the following:
#
#        PROMPT_BELL_TIME=15
#        PROMPT_BELL_CMD="echo -e 'done.\a'"
#
#        COMMAND_PROMPT='TIMES_FILE=/tmp/times.$$;
#                        times >$TIMES_FILE;
#                        eval "`prompt_bell $TIMES_FILE $PROMPT_BELL_STATE`";
#                        /bin/rm -f $TIMES_FILE'
#        export PROMPT_BELL_TIME PROMPT_BELL_CMD COMMAND_PROMPT
#
#    Note: the output of `times' is stored in a temporary file to prevent it
#    from being executed in a subshell whose CPU times are always nearly zero.
#
##############################################################################

# Convert #h#m#s to seconds.
sub seconds {
  my($hms) = @_;
  my($h,$m,$s) = $hms =~ /^(?:(\d+)h)?(\d+)m(\d+\.\d+)s/;
  return $h*3600 + $m*60 + $s;
}

# Obtain processor times in seconds
my $times_file = shift;
my $ptime_cur = 0;
open TIMES_FILE, $times_file or die "prompt_bell: $times_file: $!\n";
while (<TIMES_FILE>) {
  s/(?:(\d+)h)?(\d+)m(\d+(?:\.\d+)?)s/$ptime_cur+=$1*3600+$2*60+$3/eg;
}
close TIMES_FILE;


# Obtain previous state to compute deltas.
my $ptime_prev = shift;

# If the processor time was more than $PROMPT_BELL_TIME or 10 seconds
# then beep.
my $beep;
my $limit = exists $ENV{PROMPT_BELL_TIME}?$ENV{PROMPT_BELL_TIME}:10;
if ($ptime_cur-$ptime_prev>$limit) {
  $beep = ";" . ($ENV{PROMPT_BELL_CMD} || "echo -ne '\\a'");
}

# Generate the shell commands
print "PROMPT_BELL_STATE=$ptime_cur$beep\n";
exit 0;

Share or Research:

Share on FB Post to X LinkedIn 🤖 Ask AI about this