Application Cleanup during Linux Shutdown

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • whatisron
    New Member
    • Feb 2010
    • 1

    Application Cleanup during Linux Shutdown

    I'm trying to do some cleanup (write open files) when Linux shuts down. I thought the right method would be to trap SIGTERM and do the necessary processing. Here's my sample code:

    Code:
    #include <stdio.h> // for File I/O
    #include <signal.h> // for signals
    #include <unistd.h> // for sleep()
    
    void handler(int signal)
    {
    	FILE *out=fopen("test.txt","at");
    	if (out)
    	{
    		fprintf(out,"got %d\n",signal);
    		fclose(out);
    	}
    }
    
    int main()
    {
    	signal(SIGTERM,handler);
    	signal(SIGINT,handler);
    	sleep(30);
    }
    When I run this, and press Ctrl-C, it write "got 2" to test.txt. However, if I logout/reboot, nothing is written to the file.

    Any help or ideas would be appreciated!

    -Ron
  • pikespeakcnc
    New Member
    • Feb 2010
    • 3

    #2
    Ron,

    When you run the program and hit Ctrl-C, you're sending a signal to it, and that's what is being saved in the file. The operating system knows nothing
    about your program, so it won't run it.

    To get the operating system to run your program, you need to look at the
    manual page for inittab. It is the process for controlling the programs that
    are run during startup and shutdown.

    Hope this helps,

    pikespeakcnc

    Comment

    Working...