0

I have an application that freezes. I can't terminate it with ^C, so I have to do following:

$ ./myapp 
^C^Z
[1]+  Stopped                 ./myapp
$ pgrep -lf myapp
18479 bash ./myapp
$ kill -9 18479
$ fg
bash: fg: job has terminated
[1]+  Killed                  ./myapp
$

It is OK to do it in rare cases. But if I'm an author of this application and try to drill-down problem that causes freeze, it is very annoying to do this over and over again. Is there a way to save few keystrokes in such scenario?

Update:

pgrep + kill could be replaced with pkill -f -9 myapp, thanks to Lee B

Ideally I want to shorten path to just one keystroke, like ^C. It doesn't matter on what level it is implemented: on gnome terminal, on bash, wherever.

5 Answers 5

1

If Ctrl-C doesn't work, you should be able to use Ctrl-\, which will send a KILL signal, rather than the TERM signal that ctrl-c sends.

1
  • Wow! That's all I need. Tanks!
    – nkrkv
    Oct 9, 2009 at 8:57
1

killall -9 myapp ? I'm not quite sure if that's what you're looking for but that's how I stop applications forcefully a lot of the time. :)

1
  • "myapp: no process killed". Looks like it is because I launch it as "./myapp", i.e not simply "myapp"
    – nkrkv
    Oct 8, 2009 at 8:14
1

pkill -9 myapp

Your distro may not provide pkill, but if not, get it.

Still, this is not a good way of terminating software programmatically. You're grepping for a string, and killing anything that matches. There's no guarantee that some unrelated app won't match. Say your app is "toast", for timing when bread is nicely toasted. You grep for that and kill it. No problem. But someone else might run "video_toaster_daemon". Video Toaster is the name of a professional video effects system. Now, your script has just killed some company's TV graphics project. They're a little upset ;)

Instead, look into:

  • For apps, make sure ctrl-C does kill it. There should be some way to trap the signal, and shut down any threads or whatever is normally preventing the exit.

  • If your app is really a service, look into the way that most init.d scripts run programs and save their process IDs to kill them later. Stopping your service should be as simple as /etc/init.d/myserver stop.

2
  • Thanks, pkill saves time. In my case -f flag is necessary. Alas there is no flag to limit process count that will cause an error if there are more processes than that. It would be a safety net.
    – nkrkv
    Oct 8, 2009 at 9:04
  • Hahha, I've never looked at the -f flag, and was using ps piped to grep myself in that case. We've both learned similar shortcuts for slightly different situations then. Nice symmetry there :)
    – Lee B
    Oct 8, 2009 at 9:41
1

If you used Control-Z to background the process, you can use job control as a shortcut. I often do:

^z
kill %1

kill -9, etc, will also work with %1 style arguments. See the "Job Control" section of the bash manpage for more about job control. The jobs command, in particular, is also helpful.

0

Why don't you debug the application and try to prevent the freeze?

If you want to do this in a quick way, kill -9 $(pgrep myapp)

1
  • I do, but to debug I have to run application often. Your recipe does not work. It just prints kill usage help string.
    – nkrkv
    Oct 8, 2009 at 8:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .