KILL without PID

Hellow Experts

i have one problem.
i run one script in backgroun.
and i want to kill that script with only script name.....
so what's the solution..

for your info
my script name is "testscript" n it contains "sleep 100"

thanks....

If your system has pidof, use that. Otherwise, the customary solution is to run grep on a ps listing of your processes, and use that to find the PID to pass to kill. However, a naive attempt will have the problem that it will find itself in the process listing, and commit suicide instead of kill the intended target. The proper workaround for that is to use a regular expression which does not directly match itself as the search string.

Unfortunately, the options and output format of ps varies from one system to another. The following works for me on a recent version of Ubuntu.

ps t | awk '$5 ~ /^[t]estscript/ { print $1 }' | xargs -r kill

The use of [t] instead of just a plain t is the workaround for the "script will kill itself" problem. The use of xargs -r prevents kill from being run at all if there are no matches (there would only be a warning message about running kill with no arguments, so that is not a very fatal problem).

The option t and the field numbers $1 and $5 might need to be changed for your system. If you google for a similar solution for your particular platform, look out for the problems outlined above. For stylistic reasons, a single awk script should be preferred over what is affectionately called Useless Use of Grep.

If you are using Linux, you can use the killall command to kill a process using the process name(s) (not the PID).

See, for example: killall(1): kill processes by name - Linux man page

Se all, pkill: pkill(1) - Linux man page

I got the solution
Use
kill `ps -C test.sh -o pid=`

On Linux, you could simple type:

killall testscript

Something simple:
killall procname

Another way:
inside your script, create a temporary file that holds the current pid of your script. When you wish to terminate the process, use that as such: kill -9 `cat pidfile`

If you want something very specific and accurate:
- setuid() to another user which only runs that process (script)
- pkill -9 -u youruser
You can also use pkill to kill all processes using a certain terminal pkill -9 -t pts/1

(But don't use kill -9 if you don't know exactly what you are doing.)

Hello everyone, this is my first post.

I have a problem using killall with a python script, I cannot make it work. How should I make the call?

Welcome to the forum :slight_smile:

Your post should have been a new thread and not adding to the existing one ! :slight_smile:

ps -ef|grep scriptname|awk '{print $2}'|xargs kill -9

Once again, no need to kill -9. Why use a bazooka when a bow and arrow will do? A normal kill (equivalent to kill -15 or kill -TERM) gives the process to be killed an opportunity to catch the signal, close files and network connections, release shared memory and other IPC resources, and terminate cleanly. kill -9 does not give it this opportunity.

era was right the first time...

use pidof if you can:

on my linux system ps has a -C switch to select by command name i.e.

ps -C httpd -o pid=

will output the pids of the webserver processes running

Then again, linux mostly also has pidof as well, so that would be easier.

To get the row out of a ps listing for one process using grep, and to not get the grep row I use:

ps -ef |grep httpd |grep -v grep

Though that won't help if your grep doesn't have the -v switch.

wempy, another neat trick to do that with just one grep:

ps -ef | grep [h]ttpd

/me restricts the temptation to point to the beginning of this thread

It's ok era, I'll do it for you :wink:

:o Sorry about that... it's one of my pet hates too!