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!

上一篇:Java框架之Spring(四)


下一篇:Web API (四) 特性路由(Attribute Route)