prashanthkvs
prashanthkvs

Reputation: 263

Finding the process for the shell script

Suppose if one executes

while [ true ]; do echo "hello"; done; on the shell directly.

How do I find the process for the same and kill it afterwards?

Upvotes: 0

Views: 36

Answers (2)

l0b0
l0b0

Reputation: 58768

Depends a bit how hacky you want to be, and how much you're willing to break.

If you run this in Bash, for example, and try to look for the process in for example ps wafux, there is not going to be any subprocess started by any of those commands since they are all built-ins (see man bash for details). The only way to kill that from outside the same shell is to kill the shell itself, with for example killall bash. That's not very nice, but should at least accomplish the result in "normal" cases on some *nixes.

If it's the last background process you started in the shell you can kill it in the same shell using kill $!. If you haven't started any other bash processes since starting it you can run pkill --newest bash from other shells.

Process Management is a great relevant article which highlights some incredibly ugly hacks which are far too common and dangerous. The whole site is well worth a read.

Upvotes: 1

anubhava
anubhava

Reputation: 784878

Not really an answer for your question but would have been too cumbersome to express in a comment.

The way I use to detect these never ending scripts I end up creating a symlink of bash using

ln -s /bin/bash ~/bin/mybash

and then use run your scripts using ~/bin/mybash. Something like:

~/bin/mybash -c 'while [ true ]; do echo "hello"; done;'

Then you can search process list by mybash instead of bash.

Upvotes: 1

Related Questions