守护进程与日志进程

守护进程

守护进程:在后台运行

代码设计的守护进程:

  • 第一步:调用umask将文件模式创建屏蔽字设置为0.
  • 第二步:调用fork,父进程退出。保证子进程不是话首进程,从而保证后续不和其他终端关联。
  • 第三步:设置新会话。
  • 第四步:更改工作目录到根目录
  • 第五步:关闭标准输入,重定向所有标准(输入输出错误)到/dev/NULL

使用系统守护进程,实现授时服务器


vi /etc/xinetd.d/
service date_mine
{
	disable		= no
	type		= UNLISTED
	socket_type	= stream
	protocol	= tcp
	user		= chandler
	wait		= no
	port 		= 5859
	bind		= 127.0.0.1
	server		= /bin/date
}
service xinetd start
service xinetd restart

日志进程

  • 1.查看日志进程
rsyslogd 
 ps -aux|grep syslogd
  • 自行进行日志的文件创建 添加 管理

  • 使用日志守护进程

  • 2.通过修改syslog的配置文件进行日志守护进程的使用
    cat /etc/rsyslog.conf

如何使用系统的日志进程

系统提供了一系列的函数

       #include <syslog.h>

       void openlog(const char *ident, int option, int facility); //打开一个系统日志
       
       void syslog(int priority, const char *format, ...);  //写日志
       
       void closelog(void);  //关闭系统日志

       void vsyslog(int priority, const char *format, va_list ap);

openlog参数说明
  • ident :第一个参数ident将是一个标记,ident所表示的字符串将固定地加在每行日志的前面以标识这个日志,通常就写成当前程序的名称以作标记。
  • option:第二个参数option是下列值取与运算的结果:LOG_CONS,LOG_NDELAY, LOG_NOWAIT, LOG_ODELAY, LOG_PERROR,LOG_PID,各值意义请参考man openlog手册:
    LOG_CONS
       Writedirectly to system console if there is an error while sending tosystem logger.

    LOG_NDELAY
       Openthe connection immediately (normally, the connection is opened whenthe first message is logged).

    LOG_NOWAIT
       Don’t wait for childprocesses that may have been created while logging themessage.  (The GNU C library does not createa child process, so this option has no effect onLinux.)

    LOG_ODELAY
       The converseof LOG_NDELAY; opening of the connection is delayed until syslog()is called.  (This is the default,  and need not be specified.)

    LOG_PERROR
       (Notin SUSv3.) Print to stderr as well.

    LOG_PID
        IncludePID with each message.

例如:openlog(argv[0],LOG_NDELAY|LOG_PID,LOG_DAEMON);
表示记录日志不阻塞,并且记录程序的pid,以守护进程的模式运行

facility:第三个参数facility指明记录日志的程序的类型。

		LOG_AUTH      security/authorization messages (DEPRECATED Use LOG_AUTHPRIVinstead) // 认证日志
        
        LOG_AUTHPRIV  security/authorization messages (private) // 认证优先日志
        
        LOG_CRON      clock daemon (cron and at)    //计划任务日志
        
        LOG_DAEMON    system daemons without separate facility value  //守护进程日志
        
        LOG_FTP       ftp daemon //FTP日志
        
        LOG_KERN      kernel messages (these can't be generage from user processes)  //内核日志
        
        LOG_LOCAL0 through LOG_LOCAL7  //本地日志
                       reserved for local use
        
        LOG_LPR       line printer subsystem  //打印日志
        
        LOG_MAIL      mail subsystem  //邮件日志
        
        LOG_NEWS      USENET news subsystem  //消息系统日志
        
        LOG_SYSLOG    messages generated internally by syslogd(8)
        
        LOG_USER (default)  //用户日志
                       generic user-level messages
        
        LOG_UUCP      UUCP subsystem

syslog参数说明:
  • priority:第一个参数是消息的紧急级别
level
        This determines the importance of the message. The levels are, in  order of  decreasing
        importance:
        
        LOG_EMERG     system is unusable   //系统错误
        
        LOG_ALERT     action must be taken immediately  //警告
        
        LOG_CRIT      critical conditions  //严重错误
        
        LOG_ERR       error conditions  //错误
        
        LOG_WARNING   warning conditions  //警告
        
        LOG_NOTICE    normal, but significant, condition  //注意
        
        LOG_INFO      informational message  //信息
        
        LOG_DEBUG     debug-level message //调试信息
        
        The function setlogmask(3) can be used to restrict logging tospecified levels only.

NOTES
        The  argument ident  in  the call  of openlog() is probably storedas-is.  Thus, if the
        string it points to is changed, syslog() may start prepending thechanged string, and  if
        the  string it points to ceases to exist, theresults are undefined.  Most portable is to
        use a string constant.
        
        Never pass a string with user-supplied data as a format, use thefollowing instead:
        
            syslog(priority, "%s", string);

SEE ALSO
      logger(1), setlogmask(3), syslog.conf(5), syslogd(8)

猜你喜欢

转载自blog.csdn.net/weixin_45309916/article/details/107583405