Giter VIP home page Giter VIP logo

killmesoftly's Introduction

Kill Me Softly

Kill processes The Right Way.

Purpose

An incredible number of times, I had to perform a simple task: killing a process, and making sure the process was dead before proceeding.

Sometimes the process doesn't die by SIGTERM, and you need to send a SIGKILL.

In some (rare) situations, the process doesn't die by SIGKILL either.

Sometimes the process just takes some time to die.

By the way, what I usually want is something like hey, kill this process and don't go on until it's dead.

It's stupid, but it's everything killmesoftly does; it tries to send a SIGTERM to a process, if it doesn't die within 15 seconds it sends a SIGKILL, if it still doesn't die it reports it.

AFAIK there's no other tool to do that, even though such logic is implemented in a lot of software and service scripts.

Install

Standalone executables

This is the recommended way to go if you just want to use this tool, at least until I don't submit it to Homebrew and Linuxbrew.

Go anywhere in your executable path and download the latest standalone version of kmsp and kmsn.

wget https://www.franzoni.eu/releases/killmesoftly/master/kmsp https://www.franzoni.eu/releases/killmesoftly/master/kmsn
chmod +x ./kmsp ./kmsn

From Github source

Build and install

Just clone this repository, or download a packed zip, and use

make install

by default, this will install standalone executables in /usr/local/bin. If you want to change your install destination:

make PREFIX=/tmp/asd install

will install in /tmp/asd/bin or

make BINDIR=/tmp/mydir install

will install in /tmp/mydir.

Make-less installation

Otherwise, for a make-less install, you could just copy kmsp and kmsn in your path, set them executable, make sure kms_functions is in the same directory, and you're ready to go.

Developer-wise installation

If you want to develop changes to killmesoftly, just symlink kmsp, kmsn and kms_functions to a directory in your path (e.g. ~/bin)

Usage

You can feed the two scripts with process names (or matching expression, that will be sent to pgrep) or PIDs, respectively.

The approach is blocking by design: the script won't exit until the process is actually dead, and latter PIDs or names won't be killed until previous ones are dead.

If there's an error on one parameter (e.g. you've specified a name which matches more than one process, or a non-existent PID) the scripts will go on with the other parameters; on the contrary, if there's an hard error (e.g. a process that doesn't exit on a SIGKILL) the whole kill chain will be stopped and an error will be returned.

The default timeout is 15 seconds. It's currently hardcoded.

kmsn

Kill processes softly by name matching pattern.
Usage:
kmsn first_pgrep_pattern [second_pgrep_pattern] <...>

kmsp

Kill processes softly by pid number
Usage:
kmsp first_pid_to_kill [second_pid_to_kill] <...>

Requirements

Those should be available on almost any Linux system, even on minimal installs, and are installed by default in OSX.

  • Linux or OSX
  • Bash
  • Kill and pgrep executables (e.g. procps package on Ubuntu/Debian)

License

This is Apache-2.0 licensed, which is the most permissive license I could think of. Feel free to include and modify this code however you like.

TODOs

  • *BSD support
  • a --all switch for kmsp that allows killing all processes that match a certain parameter
  • consider whether we should return different exit codes in the event some parameters are wrong; currently we return a nonzero exit code only on zero-parameters passed or hard failures.
  • allow specifying a different signal rather than SIGTERM for soft kill, and/or additional signals to be sent before/after SIGTERM.
  • configurable timeouts
  • consider transforming such scripts in a set of functions that can be sourced, rather than used as executables
  • add options for killing process groups and/or check whether children have actually exited
  • add options for matching on full name, not just on process name
  • remove dependency on bash and let sh suffice.
  • add tool in Linuxbrew and Homebrew repositories for easy install.
  • make the testsuite runnable on OSX (currently prevented by extended sed regexp)

Thanks

The following people helped developing this software in some way or another, without a specific order.

  • Jean-Philippe Daigle
  • Laurent Cozic

killmesoftly's People

Contributors

alanfranz avatar jpdaigle avatar

Stargazers

Rob avatar Mathieu Aubin avatar Grzegorz Nowak avatar JMV avatar Serhii Shyran avatar Michael Rowell avatar Matthew Elphick avatar Angus H. avatar shekk avatar Andrii Tykhonov avatar  avatar Dibayendu Dey avatar Max Azoury avatar Isaiah Choe avatar Adrien avatar Ted Wexler avatar Iulian Dumitru avatar Jan Roudaut avatar Justin Alan Ryan avatar Ivan Vanderbyl avatar hq1 avatar Derek Shockey avatar Zack Huang avatar Steve avatar Bruno Rocha avatar Christoph Grabo avatar Nick Bottomley avatar Daniel Bergmann avatar Vincent C Fulco avatar Nazeeruddin Ikram avatar N/A avatar Illya Kysil avatar Kexin Xie avatar Pierre-Yves Landuré avatar  avatar Omar Bahareth avatar Matúš Láslofi avatar Andrea Usuelli avatar Kevin Spencer avatar  avatar Joe Esposito avatar gaurav patel avatar Ted Lilley avatar Alex Sherwin avatar Ned Ludd avatar Philip Kalinsky avatar Dennis Prochniak avatar David avatar Alexander Putilin avatar wei shi avatar stephen avatar Randall Degges avatar  avatar Erin Marshall avatar  avatar Mathieu Berube avatar Winton Welsh avatar Rafał Ochmański avatar  avatar Andrew Hodges avatar Devstator avatar Grady O'Connell avatar Rogerio Marques avatar Alix Axel avatar Abdullah avatar Ronen avatar Brandon Anzaldi avatar  avatar masukomi (a.k.a. Kay Rhodes) avatar Artis Schlossberg avatar Ido Rosen avatar John C Barstow avatar  avatar  avatar anekinone avatar Patrick Shampine avatar Phil Kates avatar JN avatar Steven McGrew avatar Adam Walsh avatar Sean Mooney avatar Bruno Sutic avatar Public Profile avatar Amr Tj. Wallas avatar Clark Minor avatar Ahmad Benmrad avatar Paul Meserve avatar Nejuf Rowan avatar Herbert Fischer avatar Steven avatar  avatar ckjoris avatar Jens Rantil avatar Rafael de F. Ferreira avatar Z. Charles Dziura avatar  avatar Patricio Villalobos avatar Donovan H. avatar Igor Sobreira avatar James B. Pollack, MFA  avatar

Watchers

 avatar  avatar Antonio Forgione avatar James Cloos avatar Soe San Win avatar  avatar

killmesoftly's Issues

Partial matching of command line

in one shell do

$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...

in another shell

$ ./kmsn SimpleHTTPServer
No process matches given pattern SimpleHTTPServer

while

$ ps aux | grep SimpleHTTPServer
rochacb+  6270  0.1  0.0  43340 12660 pts/16   S+   17:16   0:00 python -m SimpleHTTPServer

but it works if I do

$ ./kmsn python -m SimpleHTTPServer

Can't work with partial matching?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.