Linux console keyboard handler
4 answers
The simplest solution is to press Ctrl-C in the terminal window. Your application will stop immediately, or you can handle the event with a SIGINT signal handler.
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
volatile int exit_loop;
void sig_hnd( void ){ exit_loop=1; }
int main(void){
signal( SIGINT, (void (*)(int))sig_hnd );
for( exit_loop=0; !exit_loop; ){
puts( "do some work" );
sleep(1);
}
puts( "\nend of work\n" );
}
+1
source to share
I think you are asking the wrong question. Why are you using an interactive terminal at all for a long running process? Why not just run as a daemon and log the "best" solution at regular intervals? The terminal is intended for interactive human use. There are better ways to handle software that should run for "months".
+1
source to share