Sponsored Content
Top Forums Programming Problem on capturing system Shutdown Post 302847623 by hakermania on Tuesday 27th of August 2013 12:57:16 PM
Old 08-27-2013
Ubuntu Problem on capturing system Shutdown

I am having exactly the same problem with Application Cleanup during Linux Shutdown but the thread is old and closed. The only difference is that I use sigaction() instead of signal(), which is recommended, as far as I know.

This is my code:

Code:
#include <stdio.h>
#include <signal.h>
#include <unistd.h>

void handler(int signal)
{
    FILE *out=fopen("test.txt","at");
    if (out)
    {
        fprintf(out,"got %d\n",signal);
        fclose(out);
    }
}

int main()
{
    struct sigaction sigIntHandler;

    sigIntHandler.sa_handler = handler;
    sigemptyset(&sigIntHandler.sa_mask);
    sigIntHandler.sa_flags = 0;

    sigaction(SIGINT, &sigIntHandler, NULL);
    sigaction(SIGTERM, &sigIntHandler, NULL);
    sigaction(SIGHUP, &sigIntHandler, NULL);
    sigaction(SIGQUIT, &sigIntHandler, NULL);
    while(1)
        sleep(30);
}

And this is some usage that shows that it is working:

Code:
$ rm -rf test.txt
$ ./signal_handling_test &
[1] 3599
$ kill -s SIGTERM 3599
$ cat test.txt
got 15

If I normally shutdown or log out then the process is killed without the clean up taking place.

I am running under Ubuntu 13.04.
 

8 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Capturing Oracle Shutdown error

Hi, Iam calling 3 sql scripts through one shell script 'rmia.sh'. Till now this was working fine, but last time while calling the SQL scripts, Oracle was down. But amazingly the exit status was '0' (success)!!! Below is the shell code: #!/usr/bin/ksh -x assign_file asql a.sql 1... (15 Replies)
Discussion started by: ganapati
15 Replies

2. Shell Programming and Scripting

Shutdown a system by MAC?

Ok here is the problem we have 2 v440 with same IP address running solars 9. one remains on the other remains off. They are both configured exactly the same for redundant purposes for the software we use. This was the best/worst idea. Great because down time is only a mere minutes. The bad is the... (7 Replies)
Discussion started by: deaconf19
7 Replies

3. Solaris

logs for system shutdown

I am working on a SUN T2000 machine with Solaris 10 running on it. When I checked the system this morning, I found it to be turned off. The lastreboot command showed that the system had been shut down the previous night. I want to find out how the system was shut down. I have run hardware health... (2 Replies)
Discussion started by: batman727
2 Replies

4. Shell Programming and Scripting

Sending mail on system shutdown

I want to only send a mail before my system goes for a shutdown or reboot. I don't want a mail when it comes up after a reboot or is normally started.. How can i achieve this? (2 Replies)
Discussion started by: proactiveaditya
2 Replies

5. Windows & DOS: Issues & Discussions

system restarts after shutdown!!

Hello, from last few days my laptop is not whutting down properly.. when ever i ty to shutdown it restarts again.. what may be problem?? antivirus is updated till date.. and i use windows xp sp2.... regards, deepak. (5 Replies)
Discussion started by: smarty86
5 Replies

6. Shell Programming and Scripting

Capturing awk's system(cmd) output

Hi everybody, I am working on a bigger awk script in which one part is comparing the size of two files. I want to evaluate which file is bigger and then just save the bigger one. I got it all working except for the part where I want to figure out which file is bigger; the one awk is currently... (2 Replies)
Discussion started by: iMeal
2 Replies

7. Solaris

Shutdown system option

I am working on sunos solaris. I want to know which is good for system shut-down ? (3 Replies)
Discussion started by: Jitesh Varshney
3 Replies

8. Red Hat

shutdown system/myeclipse

Hi All, I have one situation to shut-down the system through shell script.I need script command to shut-down the system and process should end(safe-mode) the MyEclipse. (0 Replies)
Discussion started by: stsivaraj
0 Replies
shutdown(8)						      System Manager's Manual						       shutdown(8)

Name
       shutdown - close down the system at a given time

Syntax
       /etc/shutdown [ -k ] [ -r ] [ -h ] [ -o ] time [ warning-message ... ]

Description
       The command provides an automated shutdown procedure that a superuser can use to notify users when the system is shutting down.

       The  time is the time at which will bring the system down.  It may be the word `now', indicating an immediate shutdown, or specify a future
       time in one of two formats: + number or hour : min.  The first form brings the system down in number minutes.  The second brings the system
       down at the time of day indicated, using a 24-hour clock format.

       At intervals which get shorter as shutdown nears, warning messages are displayed at the terminals of all users on the system.  Warning mes-
       sages are also sent to users who are logged in to a remote system that has mounted a file system or directory from the local  system  using
       NFS.   Five  minutes  before  shutdown, or immediately if shutdown is timed for less than five minutes, logins are disabled by creating and
       writing a message there.  If this file exists when a user attempts to log in, prints its contents and exits.   The  file  is  removed  just
       before exits.

       At  shutdown  time, a message is written in the file This message contains the time of shutdown, who ran shutdown, and the reason.  Then, a
       terminate signal is sent at to bring the system to single-user state.

       If the or options are used, then executes or avoids shutting the system down (respectively).  The option is for use by only.  It  indicates
       to that it is being called by and not to return to the user.

       You should place the time of the shutdown and the warning message in Use the message to inform the users about when the system will be back
       up and why it is going down.

Restrictions
       You can kill the system only between now and 23:59, if you use the absolute time for shutdown.

Files
       Tells login not to let anyone log in

       Log file for successful shutdowns

See Also
       login(1), wall(1), halt(8), opser(8), reboot(8), rwalld(8c)

																       shutdown(8)
All times are GMT -4. The time now is 02:33 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy