From owner-freebsd-questions@FreeBSD.ORG Sun Sep 15 06:56:31 2013 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTP id 50A5CC5 for ; Sun, 15 Sep 2013 06:56:31 +0000 (UTC) (envelope-from m.seaman@infracaninophile.co.uk) Received: from smtp.infracaninophile.co.uk (smtp6.infracaninophile.co.uk [IPv6:2001:8b0:151:1:3cd3:cd67:fafa:3d78]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id EA123221E for ; Sun, 15 Sep 2013 06:56:30 +0000 (UTC) Received: from seedling.black-earth.co.uk (seedling.black-earth.co.uk [81.2.117.99]) (authenticated bits=0) by smtp.infracaninophile.co.uk (8.14.7/8.14.7) with ESMTP id r8F6uPHW026420 (version=TLSv1/SSLv3 cipher=DHE-RSA-CAMELLIA256-SHA bits=256 verify=NO) for ; Sun, 15 Sep 2013 07:56:25 +0100 (BST) (envelope-from m.seaman@infracaninophile.co.uk) DKIM-Filter: OpenDKIM Filter v2.8.3 smtp.infracaninophile.co.uk r8F6uPHW026420 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=infracaninophile.co.uk; s=201001-infracaninophile; t=1379228185; bh=U0ltMPzJxefvZXK82Jb3NfgiM15r8QmTwZNzwLCMltE=; h=Date:From:To:Subject:References:In-Reply-To; z=Date:=20Sun,=2015=20Sep=202013=2007:56:17=20+0100|From:=20Matthew =20Seaman=20|To:=20freebsd-questi ons@freebsd.org|Subject:=20Re:=20howto=20kill=20x=20if=20x=20is=20 running?|References:=20<20130915062046.GA12535@ethic.thought.org>| In-Reply-To:=20<20130915062046.GA12535@ethic.thought.org>; b=yoaMwi3gGErZjjjjZPwh0sQNXvgr66uPDlwYhJWU0fzsV41ac7STNdaLDMiQK0cCQ MlLajqO30MauXBgvxWw5M3jlWwgPZtvI5Gd/Wk3okFZYXkNpW400Y5Mj8PeiP/cMM3 9dU1f9hvj4opwLzg0gtYoKMDidqm9jaYMVVw3zK0= Message-ID: <52355A11.7040808@infracaninophile.co.uk> Date: Sun, 15 Sep 2013 07:56:17 +0100 From: Matthew Seaman User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:17.0) Gecko/20130801 Thunderbird/17.0.8 MIME-Version: 1.0 To: freebsd-questions@freebsd.org Subject: Re: howto kill x if x is running? References: <20130915062046.GA12535@ethic.thought.org> In-Reply-To: <20130915062046.GA12535@ethic.thought.org> X-Enigmail-Version: 1.5.2 OpenPGP: id=60AE908C Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="0pPmsI4HtWJVvJfGSJOJCH8uKrrOmDRUc" X-Virus-Scanned: clamav-milter 0.97.8 at lucid-nonsense.infracaninophile.co.uk X-Virus-Status: Clean X-Spam-Status: No, score=-2.6 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU autolearn=ham version=3.3.2 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on lucid-nonsense.infracaninophile.co.uk X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 15 Sep 2013 06:56:31 -0000 This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --0pPmsI4HtWJVvJfGSJOJCH8uKrrOmDRUc Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On 15/09/2013 07:20, Gary Kline wrote: > I've evidently had too many pain meds; this shelll script should=20 > be easy. say that I have a utility xxx running sometimes. xxx is > soaking up a chunk of my load. I have to use top to find if > xxx is running, then kill -9 to kill xxx and have a steady load of, > say, between 0.10 and 0.15. what's the script that can do this? The classic answer to this is that you need to find the pid of your 'xxx' process, and then kill it using that. Some combination of ps(1) and grep(1) usually sufficed. However nowadays there's the very handy pkill(1): pkill -9 xxx Tying that in with the trigger based on system load: #!/bin/sh load=3D$(sysctl vm.loadavg | cut -d ' ' -f 3) too_high=3D$(bc -e "$load > 0.15" < /dev/null) if [ $too_high =3D '1' ]; then pkill -9 xxx fi Note the use of bc(1) to compare floating point values -- the built-in $((shell arithmetic)) or expr(1) only do integer arithmetic. One final point -- instead of killing the xxx process when the load gets too high, you could simply renice(1) it to very low priority. Or even better, use idprio(1). This won't actually affect the system load values much as 'system load' is an average of the number of processes requesting a CPU time slice. What it does do is mean that your 'xxx' process is always pretty much the last process to get any CPU time -- so everything else should remain responsive, and your xxx process will only run when the system is otherwise idle. Cheers, Matthew --=20 Dr Matthew J Seaman MA, D.Phil. PGP: http://www.infracaninophile.co.uk/pgpkey JID: matthew@infracaninophile.co.uk --0pPmsI4HtWJVvJfGSJOJCH8uKrrOmDRUc Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG/MacGPG2 v2.0.16 (Darwin) Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iEYEARECAAYFAlI1WhkACgkQ8Mjk52CukIxk8ACfTXh4sdfsoLA4BCKg8neVhkNb XkMAn3udf8h956d8WIF5B8n6nEreiLs7 =sD8y -----END PGP SIGNATURE----- --0pPmsI4HtWJVvJfGSJOJCH8uKrrOmDRUc--