简体   繁体   English

命令“谁-b”的行为不同

[英]Command “who -b” behaves differently

I am trying to parse system boot time from "who -b" output. 我正在尝试从“谁-b”输出解析系统启动时间。

#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>

void GetSystemTurnOnTime()
{
 FILE *fp;
 char *pTemp = NULL;
 char szBuffer[1024] = {0};
 int iLen;
 struct tm tm;
 char buf[255];

 fp = popen("who -b", "r");
 fgets(szBuffer, sizeof(szBuffer), fp);

 pTemp = szBuffer;
 while(isspace(*pTemp))
    ++pTemp;
 iLen = strlen(pTemp);
 pTemp[iLen - 1] = '\0';
 printf("%s\n", pTemp);

 pclose(fp);
}


int
main(void)
{
 GetSystemTurnOnTime();
 exit(EXIT_SUCCESS);
}

Output: In normal execution it gives : system boot 2015-07-31 11:08 输出:在正常执行中它给出:系统启动2015-07-31 11:08

If i integrate with daemon then it gives: Jul 31 11:08 如果我与守护程序集成,那么它将给出:Jul 31 11:08

if i execute daemon with gdb then it again gives: system boot 2015-07-31 11:08 can any one help me why it is giving two different format. 如果我用gdb执行守护进程,则它再次给出:系统启动2015-07-31 11:08有谁能帮我为什么它给出两种不同的格式。

您可以使用以下命令:

who -b | awk '{$1=""; $2=""; print $0}' | date -f -

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM