0

This might be a very basic question but I couldn't find it anywhere on the internet. Lets assume I have a file named test with this code

echo hello
sleep 10
echo hello
sleep 10
echo hello
sleep 10

How would I go about killing that program through another terminal in my server?

bhristov
  • 3,137
  • 2
  • 10
  • 26
  • Does this answer your question? [Find and kill a process in one line using bash and regex](https://stackoverflow.com/questions/3510673/find-and-kill-a-process-in-one-line-using-bash-and-regex) – Tsyvarev Jun 23 '20 at 07:53

3 Answers3

1

I am assuming the file is test.sh

You can do:

ps -x | grep ./test.sh

This will show the processes:

11164 pts/1    S+     0:00 /usr/bin/bash ./test.sh
and a second process that will be a grep process, you won't be able to kill the process that has the word grep in it because that process completes right away

now you can kill the process using the PID:

kill 11164
bhristov
  • 3,137
  • 2
  • 10
  • 26
  • everything worked exsept the kill 11543 I got an output of -bash: kill: (11543) - No such process whenever I type kill 11543 my ourput is -bash: kill: (11543) - No such process – Binary Buffalo Jun 23 '20 at 03:56
  • @slimy try redoing the ps -x | grep ./test.sh command, if it shows the process it should be able to close the process by the PID. When you do the ps -x there will be two PID's one for the grep command and one for the script, you don't want the one with the grep command. – bhristov Jun 23 '20 at 03:59
  • im getting this error Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ – Binary Buffalo Jun 23 '20 at 04:02
  • @slimy can you show me the output of: ps -x | grep ./test.sh – bhristov Jun 23 '20 at 04:10
  • 11848 pts/0 S+ 0:00 grep ./test.sh – Binary Buffalo Jun 23 '20 at 04:15
  • @slimy that is not the correct process. There should be another one. Do you have #! /usr/bin/bash at the top of your .sh file? The grep process disappears right after it executes. – bhristov Jun 23 '20 at 04:17
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/216462/discussion-between-bhristov-and-slimy). – bhristov Jun 23 '20 at 04:19
0

Your script filename is test.

So, in another terminal, you can execute ps aux | grep test.

Then you can get the PID of test, which is located at the second column.

Then, execute kill -9 <PID>.

Yves
  • 11,597
  • 17
  • 83
  • 180
  • Im using centos 6.9 as a server and i tryed that but it still didnt work it give me and error bash: kill: (11773) - No such process Their is a file named test.sh and ive done all the steps correctly – Binary Buffalo Jun 23 '20 at 04:07
  • 1
    @slimy Are you sure that `test.sh` hasn't finished yet when you execute `kill`? – Yves Jun 23 '20 at 04:52
-1
Ctrl c

By Pressing this, you can kill that program from your terminal. And you can kill this program from your main terminal where you exicute this in first place.

Rupam
  • 75
  • 1
  • 9