Skip to content

Killing a Subset of Processes Using One-Line of AWK, PS, GREP, XARGS, and KILL on Unix

Does this look familiar?:

ps aux|grep "YOUR_PROGRAM --WITH-SOME-SPECIAL-SWITCHES"
1000 28319 0.0 2.6 54884 47340 ? S Feb16 0:01 YOUR_PROGRAM --WITH-SOME-SPECIAL-SWITCHES
1000 28330 0.0 2.6 54880 47360 ? S Feb16 0:01 YOUR_PROGRAM --WITH-SOME-SPECIAL-SWITCHES
1000 28347 0.0 2.6 54884 47336 ? S Feb16 0:01 YOUR_PROGRAM --WITH-SOME-SPECIAL-SWITCHES
kill 28319
kill 28330
kill 28347

If it does then STOP! Use this one-liner instead to kill all processes matching YOUR_PROGRAM –WITH-SOME-SPECIAL-SWITCHES:

ps|grep -v grep|grep "YOUR_PROGRAM --WITH-SOME-SPECIAL-SWITCHES"|awk '{print $1}'|xargs kill

Here’s what the one-liner does:
1. ps Outputs all processes you’re running.
2. grep -v grep Matches any data piped (|) in other than “grep”. This prevents the one-liner from trying to kill the running grep command.
3. grep "YOUR_PROGRAM --WITH-SOME-SPECIAL-SWITCHES" Matches only output with your YOUR_PROGRAM –WITH-SOME-SPECIAL-SWITCHES in it.
4. awk '{print$1}' Select the first column of data from the previously matched output, in this case the PID (process ID).
5. xargs kill calls kill for each PID.

Note: If your want to kill _all_ processes running and not just a subset (i.e. –WITH-SOME-SPECIAL-SWITCHES) you can use killall YOUR_PROGRAM

In summary, if you find yourself killing similar processes one at a time, use this simple one-liner to kill them all at once. Also you could turn this into a simple shell script.

Share and Enjoy:
  • Print
  • email
  • Facebook
  • del.icio.us
  • Reddit
  • Digg
  • Google Bookmarks
  • Twitter

Emacs sql-mode: Emacs as a Database Client

Want a simple SQL database client? Try Emacs. Emacs has an alright sql-mode. Here’s how to use it to connect to a MySQL server.

  1. Type the meta command:
    M-x
  2. To connect the server, type:
    sql-mysql
  3. Enter username, password, host, and database name when prompted.
  4. Open a new buffer:
    C-x [new buffer name]
  5. Type the meta command:
    M-x
  6. To put the buffer in sql-mode, type:
    sql-mode
  7. To send a query to the DB, type:
    C-x C-b

Tip: If your columns wrap, unwrap them with

M-x toggle-truncate-lines

Also to set all buffers in sql-mode to use a connection, type:

M-x sql-set-sqli-buffer-generally

That’s it. Send queries and retrieve results in Emacs. On less reason to leave Emacs. Many databases are supported. My install lists the following DBs: DB2, Informix, Oracle, Sybase, Ingres, MySQL, and PostgreSQL.

Share and Enjoy:
  • Print
  • email
  • Facebook
  • del.icio.us
  • Reddit
  • Digg
  • Google Bookmarks
  • Twitter

Emacs hexl-mode: Emacs as a Hex Editor

I’m always amazed to find out what Emacs can do. I’ve read that long time users of Emacs still find commands and features they didn’t know about, even after years of use. Case in point, hexl-mode, which I recently discovered. Hexl-mode enables basic hex editing of files in Emacs. To use it, open a binary file and then enter M-x hexl-mode. For help with hexl commands types M-x h.

Share and Enjoy:
  • Print
  • email
  • Facebook
  • del.icio.us
  • Reddit
  • Digg
  • Google Bookmarks
  • Twitter

New Article: Creating Short Papers Using LaTeX

, a document preparation system, is widely used by people to write scientific papers, thesises, and dissertations, and resources on how to write such documents are plentiful on the Internet. However, articles on how to write short papers like three point essays aren’t easy to find. To fill the void, I’ve written a minimal article on how to use LaTeX to write short papers, called .

Share and Enjoy:
  • Print
  • email
  • Facebook
  • del.icio.us
  • Reddit
  • Digg
  • Google Bookmarks
  • Twitter