Linux 判断进程是否已经运行的程序
2024-10-21 10:15:29
bool
ServerProcess::isAlreadyRunning()
{
#ifndef __linux__
WarningLog(<<"can't check if process already running on this platform (not implemented yet)");
return false;
#else
if(mPidFile.size() == )
{
// if no PID file specified, we do not make any check
return false;
} pid_t running_pid;
std::ifstream _pid(mPidFile.c_str(), std::ios_base::in);
if(!_pid.good())
{
// if the file doesn't exist or can't be opened, just ignore
return false;
}
_pid >> running_pid;
_pid.close(); StackLog(<< mPidFile << " contains PID " << running_pid); Data ourProc = Data("/proc/self/exe");
Data otherProc = Data("/proc/") + Data(running_pid) + Data("/exe");
char our_exe[], other_exe[];
int buf_size; buf_size = readlink(ourProc.c_str(), our_exe, );
if(buf_size < || buf_size == )
{
// if readlink fails, just ignore
return false;
}
our_exe[buf_size] = ; buf_size = readlink(otherProc.c_str(), other_exe, );
if(buf_size < || buf_size == )
{
// if readlink fails, just ignore
return false;
}
other_exe[buf_size] = ; if(strcmp(our_exe, other_exe) == )
{
ErrLog(<<"already running PID: " << running_pid);
return true;
}
return false;
#endif
}
最新文章
- ajax的post方式和get方式比较,以及需要注意的地方
- Util应用程序框架公共操作类(五):异常公共操作类
- ASP.NET WEB API必知必会:特性路由
- IOS 网络浅析-(八 NSURLSession简介)
- 编写爬虫程序的神器 - Groovy + Jsoup + Sublime
- button swift
- iOSbase64
- JS中的substring和substr函数的区别
- yii2源码学习笔记(十三)
- mysql 增量导入到elasticsearch
- Mysql日期函数,时间函数使用的总结
- de4dot命令 v2.0.3.3405
- ini文件必须要全路径名啊
- 第三章 go语言 程序的流程控制
- Short But Scary 解题报告
- Redis 中可以存储的五种基本类型
- C# Note26: [MethodImpl(MethodImplOptions.Synchronized)]与lock机制
- RISC与CISCCPU构架
- 「雅礼集训 2017 Day1」 解题报告
- 向ACCESS数据库中的表导入EXCEL表,在 System.Data.OleDb.OleDbException 中第一次偶然出现的“System.Data.dll”类型的异常