User avatar
Grumpy Mike
Posts: 936
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

How to stop a running program

Wed May 30, 2012 2:57 pm

I am playing about with some C code in the hello_pi folder. Programs like hello_triangle take over the whole screen and run continuously.
I know I can stop it with a control C but if I press any other key first, like the escape then control C doesn't work any more.
Is there a way to stop a program once a key other than control C has been pressed?
I am very new to Linux so forgive me if this is a very simple question.

User avatar
markb
Posts: 124
Joined: Thu Nov 17, 2011 8:09 am

Re: How to stop a running program

Wed May 30, 2012 3:22 pm

GM,
first you need to find out the process id of your program. Open a terminal window and type:

ps <ID>

where <ID> is the user who started the program (ie on Debian, it could be the user pi)


you will see a list of processes with the format:

USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND

make a note of the process id (PID) and then type:

kill -9 PID

for example:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
pi 1234 89.1 2.4 16296 6172 ? S 17:55 0:00 /usr/bin/piprogram
.
.
.

kill -9 1234

User avatar
Grumpy Mike
Posts: 936
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: How to stop a running program

Wed May 30, 2012 3:36 pm

Thanks
ps <ID>
where <ID> is the user who started the program (ie on Debian, it could be the user pi)
So when I try this I get
ERROR: Process ID list syntax error
Then a load of stuff giving me options, when I append the options like -p it still gives me the same error message.

Anyway, how will it know the process ID before the .bin file starts running? Once running there is no way to open a terminal window that I know of.

User avatar
jojopi
Posts: 3268
Joined: Tue Oct 11, 2011 8:38 pm

Re: How to stop a running program

Wed May 30, 2012 4:02 pm

Grumpy Mike wrote:II know I can stop it with a control C but if I press any other key first, like the escape then control C doesn't work any more.
Ctrl+C clears any pending input and sends an interrupt signal to the current foreground process. It should not matter if you have typed other characters first. I can not reproduce any problem interrupting hello_triangle after pressing Esc.

The only ways I can see that your problem could have arisen are if you pressed Alt+left/right arrow keys or Alt+F1-F6 function keys while the demo was running. If you try these at the prompt you will see that they switch between six independent virtual consoles. With full-screen 3D or video running you would not be able to see this effect, but nevertheless Ctrl+C will only signal the right process when the correct console is selected.

Incidentally, for processes that ignore interrupt signals, there is a more abrupt option, Ctrl+\, which sends a quit signal. But you should only try this if Ctrl+C fails. Similarly, if you ever do need to kill processes from the command line you should start with plain "kill" and proceed to "kill -9" only as a last resort. The more abrupt methods give the process no chance to clean up before exiting.

Return to “Beginners”