Linux C启动时创建pid文件
程序在启动时将pid写入文件,当程序再次启动时会进行检测,避免启动多个实例。
util-pidfile.h文件
#ifndef __UTIL_PID_H__
#define __UTIL_PID_H__ int PidfileCreate(const char *);
void PidfileRemove(const char *);
int PidfileTestRunning(const char *pid_filename); #endif /* __UTIL_PID_H__ */
util-pidfile.c文件
#include <stdio.h>
#include <inttypes.h> // PRIuMAX
#include <errno.h> // errno
#include <string.h> // strerror #include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h> // getpid
#include <signal.h> // kill #include "util-debug.h"
#include "util-pidfile.h" /**
* \brief Write a pid file (used at the startup)
* This commonly needed by the init scripts
*
* \param pointer to the name of the pid file to write (optarg)
*
* \retval 0 if succes
* \retval -1 on failure
*/
int PidfileCreate(const char *pidfile)
{
int pidfd = ;
char val[]; int len = snprintf(val, sizeof(val), "%"PRIuMAX"\n", (uintmax_t)getpid());
if (len <= ) {
LogError("Pid error (%s)", strerror(errno));
return(-);
} pidfd = open(pidfile, O_CREAT | O_TRUNC | O_NOFOLLOW | O_WRONLY, );
if (pidfd < ) {
LogError("unable to set pidfile '%s': %s",
pidfile,
strerror(errno));
return(-);
} ssize_t r = write(pidfd, val, (unsigned int)len);
if (r == -) {
LogError("unable to write pidfile: %s", strerror(errno));
close(pidfd);
return(-);
} else if ((size_t)r != len) {
LogError("unable to write pidfile: wrote"
" %"PRIdMAX" of %"PRIuMAX" bytes.", (intmax_t)r, (uintmax_t)len);
close(pidfd);
return(-);
} close(pidfd);
return();
} /**
* \brief Remove the pid file (used at the startup)
*
* \param pointer to the name of the pid file to write (optarg)
*/
void PidfileRemove(const char *pid_filename)
{
if (pid_filename != NULL) {
/* we ignore the result, the user may have removed the file already. */
(void)unlink(pid_filename);
}
} /**
* \brief Check a pid file (used at the startup)
* This commonly needed by the init scripts
*
* \param pointer to the name of the pid file to write (optarg)
*
* \retval 0 if succes
* \retval -1 on failure
*/
int PidfileTestRunning(const char *pid_filename)
{
if (access(pid_filename, F_OK) == ) {
/* Check if the existing process is still alive. */
pid_t pidv;
FILE *pf; pf = fopen(pid_filename, "r");
if (pf == NULL) {
LogError("pid file '%s' exists and can not be read. Aborting!",
pid_filename);
return -;
} if (fscanf(pf, "%d", &pidv) == && kill(pidv, ) == ) {
fclose(pf);
LogError("pid file '%s' exists. Is program already running? Aborting!",
pid_filename);
return -;
} fclose(pf);
}
return ;
}
util-debug.h日志打印(主要是为了方便以后使用其他打印接口,就不用再修改util-pidfile.c文件了)
#ifndef __UTIL_DEBUG_H__
#define __UTIL_DEBUG_H__ #ifndef LOG_PRINT
#define LOG_MAX_LOG_MSG_LEN 2048
#define Log(x, file, func, line, ...) \
do { \
char _log_msg[LOG_MAX_LOG_MSG_LEN]; \
snprintf(_log_msg, LOG_MAX_LOG_MSG_LEN, __VA_ARGS__); \
fprintf(stdout, "<%s> %s\n", x, _log_msg); \
} while() #define LogError(...) Log("ERROR", \
__FILE__, __FUNCTION__, __LINE__, __VA_ARGS__) #define LogWarning(...) Log("WARNING", \
__FILE__, __FUNCTION__, __LINE__, __VA_ARGS__) #define LogInfo(...) Log("INFO", \
__FILE__, __FUNCTION__, __LINE__, __VA_ARGS__) #define LogDebug(...) Log("DEBUG", \
__FILE__, __FUNCTION__, __LINE__, __VA_ARGS__) #define FatalError(...) do { \
LogError(__VA_ARGS__); \
exit(EXIT_FAILURE); \
} while()
#endif #endif //__UTIL_DEBUG_H__
main.c文件
#include <stdio.h>
#include <unistd.h> #include "util-pidfile.h" int main()
{
int result = ; const char *pidfile = "/var/run/test_pid01.pid"; if (PidfileTestRunning(pidfile) != )
return -; PidfileCreate(pidfile); while (){
sleep();
} PidfileRemove(pidfile);
return ;
}
编译完成后运行该程序,然后再启动一个终端,再次运行这个程序的时候就会打印程序已运行并退出。
<ERROR> pid file '/var/run/test_pid01.pid' exists. Is program already running? Aborting!
Linux C启动时创建pid文件的更多相关文章
- ARM linux内核启动时几个关键地址【转】
转自:http://www.cnblogs.com/armlinux/archive/2011/11/06/2396787.html 1. 内核启动地址1.1. 名词解释ZTEXTAD ...
- Linux开机启动时执行脚本的方法
方法 1 – 使用 rc.local利用 /etc/ 中的 rc.local 文件在启动时执行脚本与命令.我们在文件中加上一行来执行脚本,这样每次启动系统时,都会执行该脚本.不过我们首先需要为 /et ...
- linux 在启动时获得专用的缓冲
如果你真的需要一个大的物理上连续的缓冲, 最好的方法是在启动时请求内存来分配它. 在启动时分配是获得连续内存页而避开 get_free_pages 施加的对缓冲大小限制的唯一 方法, 不但最大允许大小 ...
- 在tomcat启动时解析xml文件,获取特定标签的属性值,并将属性值设置到静态变量里
这里以解析hibernate.cfg.xml数据库配置信息为例,运用dom4j的解析方式来解析xml文件. 1.在javaWeb工程里新建一个java类,命名为GetXmlValue.java,为xm ...
- Servlet --启动时创建、配置url、ServlectContext、初始化参数、获取资源
servlet的版本的区别 2.5版本, Servlet的配置只支持在xml文件中的配置 3.0版本: Servlet的配置支持在xml文件中的配置, 也可以使用注解的方式, 默认使用注解 让服务器在 ...
- linux下fallocate快速创建大文件
以前创建文件我一般用dd来创建,例如创建一个512M的文件: dd命令可以轻易实现创建指定大小的文件,如 dd if=/dev/zero of=test bs=1M count=1000 会生成一个1 ...
- Linux基础命令---htpasswd创建密码文件
htpasswd htpasswd指令用来创建和更新用于基本认证的用户认证密码文件.htpasswd指令必须对密码文件有读写权限,否则会返回错误码. 此命令的适用范围:RedHat.RHEL.Ubun ...
- linux下使用vim创建编辑文件
vi/vim 使用实例 如果要想用vim/vi创建一个文件(使用命令): vim test.txt 不管文件存在与否 直接输入 vi 文件名 就能够进入 vi 的一般模式 按下 i 进入输入模式( ...
- mysql启动失败:不能创建pid文件
2016-03-09T07:51:38.905444Z 0 [ERROR] /usr/sbin/mysqld: Can't create/write to file '/var/run/mysqld/ ...
随机推荐
- vueJS报错记录列表以及解决方案
1.在elem团队新出的框架里,navMenu,控制台报missing required prop "index" 解决方案: 添加index的值 2.Duplicate keys ...
- WebPack引用Bootstrap 无法使用图标的结局方案
npm i https://github.com/iconic/open-iconic.git -D 因为boostrap的css里删除了图标 分开了 我们在引入个呵呵. 下载:npm i boot ...
- NewBuiltBottomSheetDialog【新建底部对话框】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 演示在底部选项卡上方弹出底部对话框效果. 效果图 代码分析 NewBuiltBottomSheetDialog继承BottomShe ...
- 基于IdentityServer的系统对接微信公众号
业务需求 公司有两个业务系统,A和B,AB用户之间属于多对一的关系,数据库里面也就是两张表,A表有个外键指向B.现在需要实现以下几个功能. A用户扫描B的二维码,填写相关的注册信息,注册完成之后自动属 ...
- docker daemon 配置文件
Ubuntu Ubuntu 14.04 配置文件位于 /etc/init/docker.conf Ubuntu 15.04 配置文件位于 /etc/default/docker,修改配置项DOCKER ...
- js 控制随机数生成概率
基本思路:把Math.random()生成的数看着百分比,然后定义每个整数值取值范围. 'use strict'; export default class GL { /** * 构造函数 * @pa ...
- 学习笔记—MySQL基础
数据库的介绍 mysql数据库介绍 开放源码的轻量级关系型数据库管理系统,体积小.速度快.操作便捷. 数据库的启动和连接 mysql数据库启动 在终端输入以下命令,启动mysql服务器 service ...
- AIDL使用以及原理分析
AIDL使用以及IPC原理分析(进程间通信) 概要 为了大家能够更好的理解android的进程间通信原理,以下将会从以下几个方面讲解跨进程通讯信: 1. 必要了解的概念 2. 为什么要使用aidl进程 ...
- 开始食用grpc(之二)
开始食用grpc(之二) 转载请注明出处:https://www.cnblogs.com/funnyzpc/p/9570992.html ``` 前段时间有童鞋找我开专栏.搬家.甚至还有人找我写书的. ...
- 导入虚拟机vmware,此主机支持Intel VT-x,但Intel VT-x处于禁用状态和黑屏
解决方法:进入BIOS(按什么键进入bios,需要看你用什么电脑),把Intel Virtualization Technology 设置enabled 然后是黑屏解决方法:管理员模式 ...