Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Linux by (18.4k points)

I am trying to detect shut down or reboot from Linux in the c program. I found that the program can use a signal(SIGTERM, handler) (SIGKILL, handler). But these two will trigger if the user kills the process with a command, too.

At some solutions, they said can use the runlevel but it doesn't work. Don't know if the process is killed before the system init the run level. I even try to put the script in rcx.d, but it still doesn't work.

Can anyone have some advice? I need to run on many kinds of Linux systems.

I used the R solution, but I still don't see my data were clear at reboot or shutdown. Is it my function is wrong??

int start() {

    if (initIni() == EXIT_FAILURE)

        return EXIT_FAILURE;

    struct sigaction act;

    memset(&act, '\0', sizeof(act));

    act.sa_sigaction = &signal_callback_handler;

    act.sa_flags = SA_SIGINFO;

    sigaction(SIGTERM, &act, NULL);

....

}

void signal_callback_handler(int signum, siginfo_t *siginfo, void *context) {

    if (signum == SIGTERM && (long)siginfo->si_pid == 1) {

        clearData();

    }

    exit(signum);

}

 I also used David Schwartz's run level(8) nothing helped me.

1 Answer

0 votes
by (36.8k points)
edited by

Catch SIGTERM. In a handler, read the /var/run/utmp file to get a runlevel. 

Come and join Linux training to gain great knowledge. 

Do check out the video below

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...