我写了一个C++程序来检查进程是否正在运行。这个过程在后台独立启动。我的程序在前台运行时运行良好,但是当我安排它时,它不起作用。pidof从另一个后台进程的后台脚本
int PID= ReadCommanOutput("pidof /root/test/testProg1"); /// also tested with pidof -m
我做出/etc/cron.d/myscript时间脚本如下安排它: -
45 15 * * * root /root/ProgramMonitor/./testBkg > /root/ProgramMonitor/OutPut.txt
可能是什么原因呢?
string ReadCommanOutput(string command)
{
string output="";
int its=system((command+" > /root/ProgramMonitor/macinfo.txt").c_str());
if(its==0)
{
ifstream reader1("/root/ProgramMonitor/macinfo.txt",fstream::in);
if(!reader1.fail())
{
while(!reader1.eof())
{
string line;
getline(reader1,line);
if(reader1.fail())// for last read
break;
if(!line.empty())
{
stringstream ss(line.c_str());
ss>>output;
cout<<command<<" output = ["<<output<<"]"<<endl;
break;
}
}
reader1.close();
remove("/root/ProgramMonitor/macinfo.txt");
}
else
cout<<"/root/ProgramMonitor/macinfo.txt not found !"<<endl;
}
else
cout<<"ERROR: code = "<<its<<endl;
return output;
}
其输出来作为 “ERROR:代码= 256”
感谢advacee。
您应该编辑您的问题表现出更多的代码,特别的代码你'ReadCommanOutput' –
请转储您的'ReadCommanOutput'垃圾,并使用'popen' –
我已经使用popen先生,它没有奏效。 –