B.6 clockwatch.c

     

B.6 clockwatch .c

  # cat clockwatch.c  /**************************************************************************** clockwatch Simple daemon program that spawns a child then dies. The child will wake-up and write the date and time into a logfile. The logfile is called "watchlog". By default it lives in /tmp To change the location simply pass a directory name on the command line # clockwatch /var/adm Will result in the logfile /var/adm/watchlog To kill clockwatch use SIGUSR1. This will get rid of a PID file. The PID file is in the same directory as the logfile e.g. /var/adm/.watchpid from the previous example. If the PID file exists in the next invocation, clockwatch will terminate as it "worried" the old PID file is still hangin' around. Enjoy ... KIM Charles Keenan 2003 charles@keenan-consultants.co.uk **************************************************************************** */ #include <stdio.h> #include <limits.h> #include <unistd.h> #include <signal.h> #include <time.h> #include <sys/stat.h> struct timeval tp; struct timezone tzp; struct tm *times; char WATCHPID[1024]; char WATCHLOG[1024]; char hostname[16]; FILE *pidFILE; FILE *logFILE; goodbye() { gettimeofday(&tp, &tzp); times=localtime(&tp.tv_sec); fprintf(logFILE,"EXITING (%s) %.2d/%.2d/%d @ %.2d:%.2d:%.2d\n", hostname, times->tm_mon + 1, times->tm_mday, (times->tm_year)+1900,times->tm_hour,times->tm_min,times->tm_sec); fflush(logFILE); fclose(logFILE); unlink(WATCHPID); exit(1); } main(argc,argv) int argc; char *argv[]; { int pid; struct stat filebuf; switch ( argc ) { case(1): strcpy(WATCHPID,"/tmp/.watchpid"); strcpy(WATCHLOG,"/tmp/watchlog"); break; case(2): if ( stat(argv[1], &filebuf ) == -1 ) { fprintf(stderr,"%s : Directory does not exist ! Exiting.\n",argv[1]); exit(1); } strcpy(WATCHPID,argv[1]); strcat(WATCHPID,"/.watchpid"); strcpy(WATCHLOG,argv[1]); strcat(WATCHLOG,"/watchlog"); break; default: fprintf(stderr," 
  # cat clockwatch.c  /**************************************************************************** clockwatch Simple daemon program that spawns a child then dies. The child will wake-up and write the date and time into a logfile. The logfile is called "watchlog". By default it lives in /tmp To change the location simply pass a directory name on the command line # clockwatch /var/adm Will result in the logfile /var/adm/watchlog To kill clockwatch use SIGUSR1. This will get rid of a PID file. The PID file is in the same directory as the logfile e.g. /var/adm/.watchpid from the previous example. If the PID file exists in the next invocation, clockwatch will terminate as it "worried" the old PID file is still hangin' around. Enjoy ... KIM Charles Keenan 2003 charles@keenan- consultants .co.uk **************************************************************************** */ #include <stdio.h> #include <limits.h> #include <unistd.h> #include <signal.h> #include <time.h> #include <sys/stat.h> struct timeval tp; struct timezone tzp; struct tm *times; char WATCHPID[1024]; char WATCHLOG[1024]; char hostname[16]; FILE *pidFILE; FILE *logFILE; goodbye() { gettimeofday(&tp, &tzp); times=localtime(&tp.tv_sec); fprintf(logFILE,"EXITING (%s) %.2d/%.2d/%d @ %.2d:%.2d:%.2d\n", hostname, times->tm_mon + 1, times->tm_mday, (times->tm_year)+1900,times->tm_hour,times->tm_min,times->tm_sec); fflush (logFILE); fclose(logFILE); unlink(WATCHPID); exit(1); } main(argc,argv) int argc; char *argv[]; { int pid; struct stat filebuf; switch ( argc ) { case(1): strcpy(WATCHPID,"/tmp/.watchpid"); strcpy(WATCHLOG,"/tmp/watchlog"); break; case(2): if ( stat(argv[1], &filebuf ) == -1 ) { fprintf(stderr,"%s : Directory does not exist ! Exiting.\n",argv[1]); exit(1); } strcpy(WATCHPID,argv[1]); strcat(WATCHPID,"/.watchpid"); strcpy (WATCHLOG,argv[1]); strcat(WATCHLOG,"/watchlog"); break; default: fprintf(stderr,"\007Useage : %s [<directory>]\n",argv[0]); exit(1); break; } if ( fork() == 0 ) { /* Child */ setsid(); if ( ( stat(WATCHPID, &filebuf ) ) == 0 ) { fprintf(stderr,"%s : Old PID file exists. Unknown problem! Exiting.\n",WATCHPID); exit(1); } if ( ( pidFILE=fopen(WATCHPID,"w") ) == NULL ) { fprintf(stderr, "ERROR: Cannot open PID file : %s. Exiting !\n", WATCHPID); exit(2); } pid=getpid(); fprintf(pidFILE,"%d\n", pid); fclose(pidFILE); if ( ( logFILE=fopen(WATCHLOG,"w") ) == NULL ) { fprintf(stderr, "ERROR: Cannot open LOG file : %s. Exiting !\n", WATCHLOG); exit(2); } if ( gethostname(&hostname, sizeof(hostname)) != 0 ) { perror("hostname"); exit(1); } fprintf( stdout ,"Starting Clockwatch ; logfile = %s\n",WATCHLOG); signal(SIGUSR1, goodbye ); for ( ; ; ) { gettimeofday(&tp, &tzp); times=localtime(&tp.tv_sec); fprintf(logFILE,"%s %.2d/%.2d/%d @ %.2d:%.2d:%.2d\n", hostname, times->tm_mon + 1, times->tm_mday, times->tm_year,times->tm_hour,times->tm_min,times->tm_sec); fflush(logFILE); sleep(10); } } else { exit(0); } } # 
7Useage : %s [<directory>]\n",argv[0]); exit(1); break; } if ( fork() == 0 ) { /* Child */ setsid(); if ( ( stat(WATCHPID, &filebuf ) ) == 0 ) { fprintf(stderr,"%s : Old PID file exists. Unknown problem! Exiting.\n",WATCHPID); exit(1); } if ( ( pidFILE=fopen(WATCHPID,"w") ) == NULL ) { fprintf(stderr, "ERROR: Cannot open PID file : %s. Exiting !\n", WATCHPID); exit(2); } pid=getpid(); fprintf(pidFILE,"%d\n", pid); fclose(pidFILE); if ( ( logFILE=fopen(WATCHLOG,"w") ) == NULL ) { fprintf(stderr, "ERROR: Cannot open LOG file : %s. Exiting !\n", WATCHLOG); exit(2); } if ( gethostname(&hostname, sizeof(hostname)) != 0 ) { perror("hostname"); exit(1); } fprintf(stdout,"Starting Clockwatch ; logfile = %s\n",WATCHLOG); signal(SIGUSR1, goodbye); for ( ; ; ) { gettimeofday(&tp, &tzp); times=localtime(&tp.tv_sec); fprintf(logFILE,"%s %.2d/%.2d/%d @ %.2d:%.2d:%.2d\n", hostname, times->tm_mon + 1, times->tm_mday, times->tm_year,times->tm_hour,times->tm_min,times->tm_sec); fflush(logFILE); sleep(10); } } else { exit(0); } } #


HP-UX CSE(c) Official Study Guide and Desk Reference
HP-UX CSE(c) Official Study Guide and Desk Reference
ISBN: N/A
EAN: N/A
Year: 2006
Pages: 434

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net