歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> Linux編程常用

Linux編程常用

日期:2017/3/3 12:53:19   编辑:Linux技術

獲取時間

#include <sys/time.h> Linux系統的日期時間頭文件。比如:

structtimeval tv;gettimeofday(&tv,NULL);ftime()。

#include <time.h> C/C++中的日期和時間頭文件。比如:

char strtime[1024] = {0};

time_t now;

struct tm *ptm;

time(&now);

ptm = localtime(&now);

sprintf(strtime, "%d%02d%02d%02d%02d%02d", ptm->tm_year+1900,ptm->tm_mon+1,ptm->tm_mday,ptm->tm_hour,ptm->tm_min,ptm->tm_sec);

開機後台運行

可以通過增加一個(&)符號,將應用程序在後台啟動。如:/path/to/yourprogram &

監控狗

如下,每隔5秒檢查CardPlatform進程是否存在,不存在,就開啟。

#!/bin/sh

echo "enter shell" >> /usr/local/zf/CardPlatform/log

while true;do

count=`ps -e | grep CardPlatform|grep -v grep`

if [ "$?" != "0" ];then

echo "start " >> /usr/local/zf/CardPlatform/log

echo ">>>>CardPlatform isnot runing..."

/usr/local/zf/CardPlatform/CardPlatform

else

echo "running " >> /usr/local/zf/CardPlatform/log

echo ">>>>CardPlatformis runing..."

fi

sleep 5

done

打印堆棧

#include <execinfo.h>

FILE* fp = fopen("/home/3.txt","ab");

if(fp)

{

void *bt[20];

char **strings;

size_t sz;

sz = backtrace(bt, 20);

strings = backtrace_symbols(bt, sz);

for(int i = 0; i < sz; ++i)

fprintf(fp,"%s\n", strings[i]);

fclose(fp);

}

Copyright © Linux教程網 All Rights Reserved