点击(此处)折叠或打开
- #include <stdio.h>
- #include <float.h>
- #include <time.h> //time_t
- #include <signal.h>
- #include <sys/types.h> //fork
- #include <unistd.h> //fork
- #include <sys/types.h> //open
- #include <sys/stat.h> //open
- #include <fcntl.h> //open
- #include <stdio.h>
- #include <stdlib.h>
- #include <list>
- #include <string>
- #include <string.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <iostream>
- #include <float.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <time.h>
- #include <errno.h>
- #include <time.h>
- #include <sys/types.h>
- #include <dirent.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <fstream>
- #include <sys/param.h>
- using namespace std;
- string BSC_PATH,BSC_NAME;
- int BSC_NAME_LENGTH;
- int main(int argc,char **argv)
- {
- struct sigaction act;
- int error,in,out;
- time_t now;
- int memory;
- long int M15=0;
- FILE* f;
- char buffer[1024]={0};
- while(1)
- {
- time_t timep,timers;
- struct tm tblock;
- struct tm tnow;
- struct tm files_wait;
- time(&timep);
- printf("%s",asctime(gmtime(&timep)));
- tnow= *localtime(&timep);
- int NowHour=0,NowMin=0,Mon=0,Day=0,Yers=0;
- Yers=tnow.tm_year+1900;
- Mon=tnow.tm_mon+1;
- Day=tnow.tm_mday;
- NowHour=tnow.tm_hour;
- NowMin=tnow.tm_min;
- int FIAG = 0;
- cout<<"NOW time is Year:"<<Yers<<"-"<<Mon<<"-"<<Day<<" "<<NowHour<<":"<<NowMin<<endl;
- if ( NowMin == 59 )//|| (NowHour == 15 && NowMin == 29) || (NowHour == 23 && NowMin == 59))
- {
- f=popen("((ss=`date +%s/900*900`));echo $ss","r");
- if (f==NULL)
- {
- return 1;
- }
- if (fscanf(f,"%ld",$M15)!=1)
- {
- return 2;
- }
- cout<<M15<<endl;
- sprintf(buffer,"Run.sh %ld >/dev/null 2>&1",M15)
- system(buffer);
- cout<<"end"<<endl;
- string CMD = "date >> /home/omcbo/RunRLDCPII.sh.log";
- system(CMD.c_str());
- sleep(60);
- }
- else
- {
- sleep(50);
- }
- sleep(10);
- }
- exit(0);
- }
点击(此处)折叠或打开
- $ cat test.c
- #include <stdio.h>
- int main()
- {
- FILE* f;
- int i;
- f = popen("echo 123456","r");
- if(f==NULL)
- return 1;
- if(fscanf(f,"%d",&i)!=1)
- return 2;
- printf("%d\n",i);
- return 0;
- }
- $ gcc test.c
- $ ./a.out
- 123456
- char buffer[1024] = { 0 };
- sprinft(buffer,"shutdown /s /t %d",n);
- system(buffer);
- system函数需要字符串首地址作为参数,
- 所以就把命令和参数都保存到字符型数组中再把数组首地址传进去就行了