- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- i cannot trap the SIGTERM when someone kills my pr...
Operating System - Linux
1820596
Members
1421
Online
109626
Solutions
Forums
Categories
Company
Local Language
юдл
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
юдл
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-02-2007 11:09 PM
тАО07-02-2007 11:09 PM
i am using
kill 9897
kill -15 7897
and its not catching the SIGTERM signal
this is the function daemonize :
static void daemonize( const char *lockfile )
{
pid_t pid, sid, parent;
int lfp = -1;
char * sstring="";
log_message(LOG_FILE,"iniciar al demonio");
/* already a daemon */
if ( getppid() == 1 ) return;
/* Create the lock file as the current user */
if ( lockfile && lockfile[0] ) {
lfp = open(lockfile,O_RDWR|O_CREAT,0640);
if ( lfp < 0 ) {
sscanf( sstring, "incapaz crear el archivo de la cerradura %s, code=%d (%s)", lockfile, errno, strerror(errno));
log_message(LOG_FILE,sstring);
exit(EXIT_FAILURE);
}
}
/* Drop user if there is one, and we were run as root */
if ( getuid() == 0 || geteuid() == 0 ) {
struct passwd *pw = getpwnam(RUN_AS_USER);
if ( pw ) {
log_message(LOG_FILE, "fijar a usuario a " RUN_AS_USER);
setuid( pw->pw_uid );
}
}
/* Trap signals that we expect to recieve */
signal(SIGCHLD,child_handler);
signal(SIGUSR1,child_handler);
signal(SIGALRM,child_handler);
signal(SIGHUP, child_handler);
signal(SIGTERM,child_handler);
/* Fork off the parent process */
pid = fork();
if (pid < 0) {
sscanf( sstring, "incapaz bifurcar demonio, c├│digo =%d (%s)", errno, strerror(errno));
log_message(LOG_FILE,sstring);
exit(EXIT_FAILURE);
}
/* If we got a good PID, then we can exit the parent process. */
if (pid > 0) {
/* Wait for confirmation from the child via SIGTERM or SIGCHLD, or
for two seconds to elapse (SIGALRM). pause() should not return. */
alarm(2);
pause();
exit(EXIT_FAILURE);
}
/* At this point we are executing as the child process */
parent = getppid();
/* Cancel certain signals */
signal(SIGCHLD,SIG_DFL); /* A child process dies */
signal(SIGTSTP,SIG_IGN); /* Various TTY signals */
signal(SIGTTOU,SIG_IGN);
signal(SIGTTIN,SIG_IGN);
signal(SIGHUP, child_handler);
signal(SIGTERM,child_handler);
// signal(SIGHUP, SIG_IGN); /* Ignore hangup signal */
//signal(SIGTERM,SIG_DFL); /* Die on SIGTERM */
/* Change the file mode mask */
umask(0);
/* Create a new SID for the child process */
sid = setsid();
if (sid < 0) {
sscanf( sstring, "incapaz crear una nueva sesi├│n, c├│digo %d (%s)", errno, strerror(errno));
log_message(LOG_FILE,sstring);
exit(EXIT_FAILURE);
}
/*Change the current working directory. This prevents the current directory from being locked; hence not being able to remove it.*/
if ((chdir("/")) < 0) {
sscanf( sstring, "incapaz cambiar el directorio a %s, code %d (%s)","/", errno, strerror(errno) );
log_message(LOG_FILE,sstring);
exit(EXIT_FAILURE);
}
/* Redirect standard files to /dev/null */
freopen( "/dev/null", "r", stdin);
freopen( "/dev/null", "w", stdout);
freopen( "/dev/null", "w", stderr);
/* Tell the parent process that we are A-okay */
kill( parent, SIGUSR1 );
log_message(LOG_FILE,"el salir daemonize la funci├│n");
}
this is the signal handler :
static void child_handler(int signum)
{
switch(signum)
{
case SIGALRM: exit(EXIT_FAILURE); break;
case SIGUSR1: exit(EXIT_SUCCESS); break;
case SIGCHLD: exit(EXIT_FAILURE); break;
case SIGHUP:
log_message(LOG_FILE,"a Terminar el demonio::::::::SIGHUP");
exit(EXIT_SUCCESS);
break;
case SIGTERM:
log_message(LOG_FILE,"a Terminar el demonio::::::::SIGTERM");
exit(0);
break;
}
}
this is the main ()
main()
{
daemonize();
while(1) sleep(1); /* run */
}
SLB SLB SLB Glorioso
Solved! Go to Solution.
- Tags:
- SIGTERM
2 REPLIES 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-03-2007 01:58 AM
- Tags:
- tusc
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-04-2007 07:44 AM
тАО07-04-2007 07:44 AM
Re: i cannot trap the SIGTERM when someone kills my process
hi
thanks for replying
it is working now, i had a problem with the log function, now it is logging when the daemon gets a SIGTERM.
thanks for replying
it is working now, i had a problem with the log function, now it is logging when the daemon gets a SIGTERM.
SLB SLB SLB Glorioso
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Learn About
News and Events
Support
© Copyright 2025 Hewlett Packard Enterprise Development LP