Date: Sun, 15 Sep 2013 07:56:17 +0100 From: Matthew Seaman <m.seaman@infracaninophile.co.uk> To: freebsd-questions@freebsd.org Subject: Re: howto kill x if x is running? Message-ID: <52355A11.7040808@infracaninophile.co.uk> In-Reply-To: <20130915062046.GA12535@ethic.thought.org> References: <20130915062046.GA12535@ethic.thought.org>
next in thread | previous in thread | raw e-mail | index | archive | help
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--
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?52355A11.7040808>