Skip to content

Instantly share code, notes, and snippets.

@jsfaint
Created March 24, 2017 08:01
Show Gist options
  • Select an option

  • Save jsfaint/01c53d5c13f9e1d4fd3bf071d99f4375 to your computer and use it in GitHub Desktop.

Select an option

Save jsfaint/01c53d5c13f9e1d4fd3bf071d99f4375 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
void sig_handler(int signo)
{
printf("Signal %d", signo);
exit(1);
}
int install_sig_handler(void)
{
struct sigaction sa;
sa.sa_handler = sig_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
if (sigaction(SIGTERM, &sa, NULL) != 0) {
printf("sigaction(SIGTERM) error\n");
return -1;
}
if (sigaction(SIGINT, &sa, NULL) != 0) {
printf("sigaction(SIGINT) error\n");
return -1;
}
if (sigaction(SIGKILL, &sa, NULL) != 0) {
printf("sigaction(SIGKILL) error\n");
return -1;
}
return 0;
}
int main(int argc, char const* argv[])
{
install_sig_handler();
while(1) {
sleep(60);
}
return 0;
}
#!/bin/bash
proc_term()
{
echo "SIGTERM trapped"
exit 1
}
trap "proc_term" SIGTERM
proc_kill()
{
echo "SIGTERM trapped"
exit 1
}
trap "proc_kill" SIGKILL
while true ; do
sleep 60
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment