473,394 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Can someone help me with skeleton C to handle SIGTERM

I'm trying to debug a Perl problem whose symptoms are that a running
Perl process either doesn't receive, or fails inexplicably to handle, a
SIGTERM sent from the OS during shutdown (Linux Fedora Core 4, if it
matters). One way to find out whether it's a Perl problem or an OS
problem is for me to run a program in some other language and see if it
receives SIGTERM when Linux shuts down (as it's supposed to, according
to the manuals).

I have written C in my time but I'm very rusty, and I'm hoping that
someone could either sketch out, or point me at, some skeleton code
which I could then perfect to do the job. I'll be OK with debugging and
so forth, but I know that I could spend ages rootling around looking for
the right #include statements and constants for the signalling and the
syslogging.

All I need is a program that can be run in the background (CRON) and
will syslog a message when it gets a SIGTERM. The original Perl program
is this (which most C programmers will understand, I hope).

#! /usr/bin/perl
use strict;
use warnings;
use Sys::Syslog;
use sigtrap 'handler' =\&terminate, 'normal-signals';
my $loop_count = 0;
my $timer = 0;
my $sleep_size = 10;
my $loop_size = 6;
openlog 'clpm.pl', 'cons,pid', 'user';
syslog 'info', 'starting up';
while (1) {
while (++$loop_count < $loop_size) {
sleep $sleep_size;
}
$timer += $loop_count*$sleep_size;
$loop_count = 0;
syslog 'info', "Running for $timer seconds";
}
syslog 'info', 'How did we get here?';

sub terminate {
syslog 'info', 'Normal signal received';
exit 0;
}

All help gratefully received.

--

Henry Law <>< Manchester, England
Sep 1 '06 #1
6 2747
On 2006-09-01, Henry Law <ne**@lawshouse.orgwrote:
I'm trying to debug a Perl problem whose symptoms are that a running
Perl process either doesn't receive, or fails inexplicably to handle, a
SIGTERM sent from the OS during shutdown (Linux Fedora Core 4, if it
matters). One way to find out whether it's a Perl problem or an OS
problem is for me to run a program in some other language and see if it
receives SIGTERM when Linux shuts down (as it's supposed to, according
to the manuals).

I have written C in my time but I'm very rusty, and I'm hoping that
someone could either sketch out, or point me at, some skeleton code
which I could then perfect to do the job. I'll be OK with debugging and
so forth, but I know that I could spend ages rootling around looking for
the right #include statements and constants for the signalling and the
syslogging.

All I need is a program that can be run in the background (CRON) and
will syslog a message when it gets a SIGTERM.
#include <stdio.h>
#include <unistd.h>
#include <signal.h>

void handler(int v)
{
signal(SIGTERM, handler);
fprintf(stderr, "Signal handler called with %d\n", v);
}

int main(int argc, char **argv)
{
signal(SIGTERM, handler);

while (1)
sleep(3600);

return 0;
}

HTH. The proper NG would be comp.unix.programmer.
Sep 1 '06 #2
Ben C wrote:
On 2006-09-01, Henry Law <ne**@lawshouse.orgwrote:
>All I need is a program that can be run in the background (CRON) and
will syslog a message when it gets a SIGTERM.

#include <stdio.h>
Wonderful; that'll be enough to get me going. Thank you very much.
HTH. The proper NG would be comp.unix.programmer.
Sorry; since I particularly wanted C (because I used to speak a bit) I
thought this was a better place.

--

Henry Law <>< Manchester, England
Sep 1 '06 #3
Henry Law wrote:
Ben C wrote:
On 2006-09-01, Henry Law <ne**@lawshouse.orgwrote:
All I need is a program that can be run in the background (CRON)
and will syslog a message when it gets a SIGTERM.
#include <stdio.h>

Wonderful; that'll be enough to get me going. Thank you very much.
HTH. The proper NG would be comp.unix.programmer.

Sorry; since I particularly wanted C (because I used to speak a bit)
I thought this was a better place.
The problem is, there's no way to do in C. None of "background",
"CRON", "SIGTERM", or "syslog" are topical here.

An extension like POSIX might help, hence Ben's suggestion that you
move to comp.unix.programmer where they discuss things like that.


Brian
Sep 1 '06 #4
Default User wrote:
Henry Law wrote:
Ben C wrote:
>On 2006-09-01, Henry Law <ne**@lawshouse.orgwrote:
All I need is a program that can be run in the background (CRON)
and will syslog a message when it gets a SIGTERM.
>
#include <stdio.h>
Wonderful; that'll be enough to get me going. Thank you very much.
HTH. The proper NG would be comp.unix.programmer.
Sorry; since I particularly wanted C (because I used to speak a bit)
I thought this was a better place.

The problem is, there's no way to do in C. None of "background",
"CRON", "SIGTERM", or "syslog" are topical here.
That would be standard C without Unix specific functions
and definitions.

Sep 1 '06 #5
Spiros Bousbouras wrote:
Default User wrote:
The problem is, there's no way to do in C. None of "background",
"CRON", "SIGTERM", or "syslog" are topical here.

That would be standard C without Unix specific functions
and definitions.

You mean the topic of this newsgroup? Yeah.

Brian
Sep 1 '06 #6
Ben C wrote:
>
On 2006-09-01, Henry Law <ne**@lawshouse.orgwrote:
I'm trying to debug a Perl problem whose symptoms are that a running
Perl process either doesn't receive, or fails inexplicably to handle, a
SIGTERM sent from the OS during shutdown (Linux Fedora Core 4, if it
matters). One way to find out whether it's a Perl problem or an OS
problem is for me to run a program in some other language and see if it
receives SIGTERM when Linux shuts down (as it's supposed to, according
to the manuals).

I have written C in my time but I'm very rusty, and I'm hoping that
someone could either sketch out, or point me at, some skeleton code
which I could then perfect to do the job. I'll be OK with debugging and
so forth, but I know that I could spend ages rootling around looking for
the right #include statements and constants for the signalling and the
syslogging.

All I need is a program that can be run in the background (CRON) and
will syslog a message when it gets a SIGTERM.
.... snip non-standard off-topic code ...
>
HTH. The proper NG would be comp.unix.programmer.
This is the only part of your article that is topical in c.l.c.

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
Sep 1 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andrew Athan | last post by:
I have a python program (snippet below) which does not want to seem to die when I issue a sys.exit() inside the SIGTERM handler. The output below is generated as the result of sending several...
4
by: Teresa | last post by:
Platform: Unix Tru 64 OSF 4.0D Problem: I spawned 3 processes. Proc A, B and C. My problem is that the parent process, proc A, sends SIGTERM to proc C; However, proc C does not exit some...
0
by: Teresa | last post by:
Platform: Unix Tru 64 OSF 4.0D Problem: I spawned 3 processes. Proc A, B and C. My problem is that the parent process, proc A, sends SIGTERM to proc C; However, proc C does not exit some...
0
by: Manlio Perillo | last post by:
Hi. I have tried this script: from ctypes import cdll, c_int from signal import SIGTERM, SIGINT from time import sleep msvcrt = cdll.LoadLibrary("MSVCR71") # is this the lib to use?
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.