AKASteve
Posts: 11
Joined: Fri Sep 26, 2014 8:37 pm

Gracefully ending a program from console

Fri Oct 03, 2014 3:16 am

Is there any way to request a program to exit gracefully from an SSH client?

I've got XBMC running in raspbian and so far the only way I can see to exit it is to manually navigate to the exit button and tell it to end. Does anyone know if I can request for it to exit gracefully from SSH and if so how? I recall reading somewhere that one should exit XBMC before shutting down the PI otherwise you run the risk of corruption and I would rather avoid that.

elatllat
Posts: 1337
Joined: Sat Dec 17, 2011 5:05 pm

Re: Gracefully ending a program from console

Fri Oct 03, 2014 4:03 am

Last edited by elatllat on Fri Oct 03, 2014 1:48 pm, edited 1 time in total.
SBC with 32GB RAM: https://hardkernel.com

FAQ : https://raspberrypi.stackexchange.com

Unanswered: https://www.raspberrypi.org/forums/search.php?search_id=unanswered

AKASteve
Posts: 11
Joined: Fri Sep 26, 2014 8:37 pm

Re: Gracefully ending a program from console

Fri Oct 03, 2014 12:34 pm

Will using the kill command simply end the program or tell the program to go through normal shutdown operations?

User avatar
PeterO
Posts: 5878
Joined: Sun Jul 22, 2012 4:14 pm

Re: Gracefully ending a program from console

Fri Oct 03, 2014 12:47 pm

AKASteve wrote:Will using the kill command simply end the program or tell the program to go through normal shutdown operations?
It will depend if the program handles the "SIGTERM" and "SIGINT" signals properly.

Have a look at http://en.wikipedia.org/wiki/Unix_signal fpr some background on unix signals.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
DougieLawson
Posts: 39120
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Gracefully ending a program from console

Fri Oct 03, 2014 1:32 pm

PeterO wrote:It will depend if the program handles the "SIGTERM" and "SIGINT" signals properly.
How about using SIGUSR1 or SIGUSR2 and giving those non-terminating signals a special function.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
PeterO
Posts: 5878
Joined: Sun Jul 22, 2012 4:14 pm

Re: Gracefully ending a program from console

Fri Oct 03, 2014 1:58 pm

DougieLawson wrote:
PeterO wrote:It will depend if the program handles the "SIGTERM" and "SIGINT" signals properly.
How about using SIGUSR1 or SIGUSR2 and giving those non-terminating signals a special function.
That would be fine if the OP was asking about making their own s/w deal with signals but they are asking about XBMC and I doubt they want to make their own custom version of that ;) If it already correctly handles SIGINT then that is a much easier solution !

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: Gracefully ending a program from console

Fri Oct 03, 2014 2:04 pm

The short summary to this thread is that a well-written piece of software that needs to do some "clean up" before exiting [*] should setup signal handlers for the commonly given signals (and document which ones it handles) so that it can be killed and the right thing(s) will happen.

Whether or not XBMC (or any other software) meets this criterion for quality can only be determined empirically.

[*] On the assumption that if the program just exits (the default action for most signals), that the system would be left in some kind of "bad" state.
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

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

Re: Gracefully ending a program from console

Fri Oct 03, 2014 2:27 pm

AKASteve wrote:I recall reading somewhere that one should exit XBMC before shutting down the PI otherwise you run the risk of corruption and I would rather avoid that.
Corruption of what?

Anyway, provided you shutdown cleanly (such as "sudo halt") all processes do get a catchable signal and a few seconds to clean up before they are forcibly killed. If this is not sufficient for a given application then it is buggy and killing it by hand may not work any better.

User avatar
PeterO
Posts: 5878
Joined: Sun Jul 22, 2012 4:14 pm

Re: Gracefully ending a program from console

Fri Oct 03, 2014 2:28 pm

Joe Schmoe wrote:The short summary to this thread is that a well-written piece of software that needs to do some "clean up" before exiting [*] should setup signal handlers for the commonly given signals (and document which ones it handles) so that it can be killed and the right thing(s) will happen.
+1
PeterO

EDIT: A quick GOOGLE seems to suggest that hadling SIGTERM has been considered and a patch provided for situations when XBMC has "got stuck" thus stops a mediaplayer from doing a shutdown properly.
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
PeterO
Posts: 5878
Joined: Sun Jul 22, 2012 4:14 pm

Re: Gracefully ending a program from console

Fri Oct 03, 2014 2:37 pm

jojopi wrote:
AKASteve wrote:I recall reading somewhere that one should exit XBMC before shutting down the PI otherwise you run the risk of corruption and I would rather avoid that.
Corruption of what?
Maybe databases and the like that hold metadata for the media files ? (Just a guess)
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Return to “Beginners”