Sponsored Content
Top Forums Programming How to implement SIGKILL and SIGTERM and print a message? Post 302144882 by norelco55 on Sunday 11th of November 2007 10:04:48 AM
Old 11-11-2007
Thanks for all the help. I have everything working except KIll -9, But I am learning in stages thanks to you all Smilie.


questions:
1. I am still confused about using atexit(). Isn't wastefull to resources to call another function that just exits? All I need to do really is call exit(); I havent implemented logging or anything and all filehandles and kids are closed.

2. I still need to work on the printfs..I just got back from AC at 2am. Banged this out when I got back--haha.


3. "you have done sigfillset but done nothing with the sigset_t, you need to call sigprocmask to block or unblock those signals."

I am still working on this one!!

4. There has to be a way to at least know sigkill is coming so I can print before it happens. I say this because some linux distros show the sigkill or are they faking it with sigterm? I did notice sigterm and sigkill are always together.


Here is what I Have now: Feel free to critique(so I don't learn bad Habits) Smilie.

So, is flush in or out? I am thinking, I dont need it because when I exit, that should clean up right?


Code:
static struct sigaction act; 


sigemptyset (&act.sa_mask);
  act.sa_flags = 0;

  /* Register the handler for SIGINT To Ignore. */
  act.sa_handler = SIG_IGN;
  sigaction (SIGINT, &act, NULL);

  /* Register the handler for SIGHUP To Ignore */
  act.sa_handler = SIG_IGN;
  sigaction (SIGHUP, &act, NULL);


       //calls function catcher.

  /* Register the handler for SIGTERM. */
  act.sa_handler =  catcher;
  sigaction (SIGTERM, &act, 0);


  //SIGKILL CANNOT BE CAUGHT!! KILL -9
  /* Register the handler for SIGKILL. */
  act.sa_handler =  catcher;
  sigaction (SIGKILL, &act, 0);




=======================================================
//gracefully quit on sigterm and sigkill.
void catcher(int sig)
{

  printf ("Caught signal %d\n", sig);
  fflush(stdout);
  switch (sig)

   case SIGTERM:

          printf("exiting sigterm");
          exit(1);
          fflush(stdout);
          break;

        case SIGKILL:

          printf("sigkilla");
          exit(1);
          fflush(stdout);
          break;
  }
}   // end function catcher

 

10 More Discussions You Might Find Interesting

1. Programming

signals - SIGTERM

Hi all, I need some urgent help. we are using Dynix/ptx V4.5 on i386, have several processes and instances are running on the box round the clock.we increased the processes recently. We have coded to handle the signals in our programs. Recently, we noticed most of our processes are... (2 Replies)
Discussion started by: reddyb
2 Replies

2. Shell Programming and Scripting

Why SIGKILL will occur?

Hi Gurus, I am executing my Datastage jobs on UNIX operating System. While running the jobs i am getting the following error: main_program: Unexpected termination by Unix signal 9(SIGKILL) Can any one please let me know what are the possible situations where this SIGKILL will arrise? ... (9 Replies)
Discussion started by: choppas
9 Replies

3. Solaris

To print Coloured Prelogin Message on SOLARIS--9................

Hiiii..... Every one...... I am using /etc/issue file to display Pre-login Message on my system, installed with SOLARIS-9. I am getting this Message in White fonts having Black background ( Colour of the Screen)..... So, is there ... (2 Replies)
Discussion started by: prashantshukla
2 Replies

4. Shell Programming and Scripting

Error while trying to print message

Hi all Geting this error while trying to print message as : ./logfunc: print: bad file unit number heres what i m trying to do : log_date="$(date '+%d/%m/%Y %H:%M:%S')" log_type="Message" print "${log_date}: ${log_type}" print -u3 "${log_date}: ${log_type}" this error is due to... (3 Replies)
Discussion started by: Navatha
3 Replies

5. Shell Programming and Scripting

Print a message at specific line on prompt

Hi Friends, I am using HP-UNIX(ksh). I want to print a message at specific line on the prompt screen. For Example: for num in 1 10 3 145 do echo $num // need to print this on the same line for each number sleep 2 done Actual Output: ========== 1 10 3 145 Expected Output:... (5 Replies)
Discussion started by: Niroj
5 Replies

6. Solaris

SIGQUIT and SIGKILL message

Dear All, I have machine with SunOS 5.10 Generic_138888-01 sun4v sparc SUNW,SPARC-Enterprise-T5120. Yesterday there is something at dmesg command : May 25 18:09:02 cacao_launcher: Timeout occured on heartbeat channel, cleanup engaged May 25 18:09:07 cacao_launcher: watchdog : warning,... (0 Replies)
Discussion started by: mbah_jiman
0 Replies

7. Shell Programming and Scripting

How to detect SIGTERM,SIGKILL signal in UNIX

Dear All We have JBOSS server running on Linux we need to track Graceful Shutdown(SIGTERM) and Forceful Shutdown(SIGKILL) timestamp and write it into one file, I am new to UNIX Signal processing if is it possible how to detect it? We generally do $kill PID For Graceful... (5 Replies)
Discussion started by: mnmonu
5 Replies

8. Shell Programming and Scripting

SIGSTOP and SIGKILL

Which is sent to a terminal when it closes? SIGKILL? Reason I ask is I have a script I want to run in the background, but want it to run even if the terminal window is closed. Or, I'd like it to background itself if the terminal is closed but not if its running in an open window. I will learn how... (5 Replies)
Discussion started by: DC Slick
5 Replies

9. UNIX for Advanced & Expert Users

Print message while using sqlplus

I want to connect to oracle database from solaris... After that i will drop and create a no.of tables.One of the table example is as below. sqlplus -s usrname/password@dbname << SQL >> $logfile 2>&1 echo " dropping the table1" | tee logfile DROP TABLE Table1 echo "creating the table1" |... (1 Reply)
Discussion started by: millan
1 Replies

10. Shell Programming and Scripting

Script to check the string in a file and print an attribute in the message

Hi, I am new to shell scripting and got a task to complete. Task is : we have a log file where in i need to traverse through the whole file to check the string "Person Type missing in message" and after that i need to get EMPLID=xxxxxx from the file and print details in a different file. ... (1 Reply)
Discussion started by: suren424
1 Replies
BSD_SIGNAL(P)						     POSIX Programmer's Manual						     BSD_SIGNAL(P)

NAME
bsd_signal - simplified signal facilities SYNOPSIS
#include <signal.h> void (*bsd_signal(int sig, void (*func)(int)))(int); DESCRIPTION
The bsd_signal() function provides a partially compatible interface for programs written to historical system interfaces (see APPLICATION USAGE). The function call bsd_signal(sig, func) shall be equivalent to the following: void (*bsd_signal(int sig, void (*func)(int)))(int) { struct sigaction act, oact; act.sa_handler = func; act.sa_flags = SA_RESTART; sigemptyset(&act.sa_mask); sigaddset(&act.sa_mask, sig); if (sigaction(sig, &act, &oact) == -1) return(SIG_ERR); return(oact.sa_handler); } The handler function should be declared: void handler(int sig); where sig is the signal number. The behavior is undefined if func is a function that takes more than one argument, or an argument of a dif- ferent type. RETURN VALUE
Upon successful completion, bsd_signal() shall return the previous action for sig. Otherwise, SIG_ERR shall be returned and errno shall be set to indicate the error. ERRORS
Refer to sigaction() . The following sections are informative. EXAMPLES
None. APPLICATION USAGE
This function is a direct replacement for the BSD signal() function for simple applications that are installing a single-argument signal handler function. If a BSD signal handler function is being installed that expects more than one argument, the application has to be modi- fied to use sigaction(). The bsd_signal() function differs from signal() in that the SA_RESTART flag is set and the SA_RESETHAND is clear when bsd_signal() is used. The state of these flags is not specified for signal(). It is recommended that new applications use the sigaction() function. RATIONALE
None. FUTURE DIRECTIONS
None. SEE ALSO
sigaction() , sigaddset() , sigemptyset() , signal() , the Base Definitions volume of IEEE Std 1003.1-2001, <signal.h> COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2003 Edition, Standard for Information Technol- ogy -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html . IEEE
/The Open Group 2003 BSD_SIGNAL(P)
All times are GMT -4. The time now is 03:01 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy