Hello Michael:
If I have not misunderstood your question you are trying to catch "ctrl-c" and
"ctrl-z" signals. Usually the system call "signal" was used but this is the old
method for
catching signals. Now is best-used the system call sigaction to perform this
operation.
Here is the code of a small program catching the signal SIGINT (ctrl-c). The
program
counts down from 10 to 1 and every time the ctrl-c is pressed, the signal is
catched and
and a warning message is printed out. Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
main () {
void sigint_handler (int signo);
int i;
struct sigaction sa;
char s[100];
sigset_t mask;
sigemptyset(&mask);
sa.sa_handler = sigint_handler;
sa.sa_mask = mask;
sa.sa_flags = SA_RESTART;
sigaction(SIGINT, &sa, NULL); // you should error-check this line.
for (i = 10; i >= 1;i--) {
printf("%d\n", i);
sleep(1);
}
Quote:}
void sigint_handler(int signo) {
printf("catching SIGINT signal\n");
Quote:}
This code has been compiled with "cc -o catch catch.c" and executed in Sun
Solaris 5.5.1
Take a look at man pages for "sigaction" and "sigemtpyset" for more details.
Best regards,
Oscar Morales.
> If I have
> trap 'some_jobs; exit 1' 1 2 3 15
> Then it will trap Control-C (signal numver 2?) and
> perform "some_jobs" and exit.
> However while shell script perform "some_jobs", it received
> repeated Control-C, what is going to happen? How can I
> ignore the repeated Control-C and finish the jobs. Thanks.
> --
> unix programs: niftp (non-interactive recursive ftp), hide (hide command args),
> submit (replace nohup), etc from ftp://ftp.mindspring.com/users/mwang/unix-prog