0

I'm developing an application on Debian 10, My application need to saving current data to a file before shut down. I cant handle event when system shutdown, any idea to help me resolve this problem?

Edit: That is my code

struct sigaction action;

memset(&action, 0, sizeof(action));
action.sa_handler = proc_term;
sigaction(SIGTERM, &action, NULL);
signal(SIGINT, proc_end);
signal(SIGTERM, proc_term);

Below is code for proc_term function

void proc_term()
{
    LOG_WARN("Process was force killed");
    is_forcedexit = 1;
}
user1234
  • 25
  • 9
  • 3
    Catch `SIGTERM`; you should have 5 seconds before you get `SIGKILL` (which you can't do anything about). EDIT: I see by your tags you already know about [tag:sigterm]`; what do you mean by "can't handle event when system shutdown"? Do you have code that you are using but doesn't work? – Amadan Jan 16 '23 at 04:00
  • @Amadan thank for quick answer. Yes, I used sigterm to try to handle it when system shutting down, but it does not working. It only working if I kill my aoo by process id or Ctrl+C. That mean Sigterm only can handle if system is running, if system is going to shutdown, App kill be killed with not able to handle Sigterm? – user1234 Jan 16 '23 at 06:24
  • Probably not the problem here, but `signal` and `sigaction` do the same job, so you don't need both for `SIG_TERM`. Pick one ([probably `sigaction`](https://stackoverflow.com/questions/231912/what-is-the-difference-between-sigaction-and-signal)). Now, how do you know it does not work? Not with `is_forcedexit` - you can't check a variable after the system shuts down. I don't know how `LOG_WARN` is defined - if it is writing to disk, do you ensure the file stream is flushed? – Amadan Jan 16 '23 at 06:43
  • Thank you for figure out. I will try it. With this code, When I use Ctrl-C or kill by process ID, a warning log was printed out to syslog file. But when I try to reboot system or poweroff, there is no log appeared on syslog file, that is the reason I think that its not working – user1234 Jan 16 '23 at 06:54

0 Answers0