diff --git a/HISTORY b/HISTORY index c723a82..f6d8239 100644 --- a/HISTORY +++ b/HISTORY @@ -1,4 +1,7 @@ +Version 1.53 2021-06-26 + * process_action support action status + Version 1.52 2021-06-08 * process_stop more gracefully (kill -9 on timeout) * add function fc_queue_pop_to_queue_ex diff --git a/src/process_ctrl.c b/src/process_ctrl.c index ce05be4..17ce452 100644 --- a/src/process_ctrl.c +++ b/src/process_ctrl.c @@ -244,33 +244,33 @@ int process_start(const char* pidFilename) } } -int process_exist(const char *pidFilename) +int process_exist(const char *pidFilename, pid_t *pid) { - pid_t pid; int result; - if ((result=get_pid_from_file(pidFilename, &pid)) != 0) { + if ((result=get_pid_from_file(pidFilename, pid)) != 0) { if (result == ENOENT) { - return false; + return result; } else { fprintf(stderr, "get pid from file: %s fail, " \ "errno: %d, error info: %s\n", pidFilename, result, strerror(result)); - return true; + return result; } } - if (kill(pid, 0) == 0) { - return true; + if (kill(*pid, 0) == 0) { + return 0; } else if (errno == ENOENT || errno == ESRCH) { - return false; + return ENOENT; } else { + result = errno != 0 ? errno : EPERM; fprintf(stderr, "kill pid: %d fail, errno: %d, error info: %s\n", - (int)pid, errno, strerror(errno)); - return true; + (int)*pid, result, strerror(result)); + return result; } } @@ -330,6 +330,9 @@ int get_base_path_from_conf_file(const char *filename, char *base_path, int process_action(const char *pidFilename, const char *action, bool *stop) { const bool bShowError = true; + int result; + pid_t pid; + *stop = false; if (action == NULL) { @@ -341,6 +344,23 @@ int process_action(const char *pidFilename, const char *action, bool *stop) *stop = true; return process_stop_ex(pidFilename, bShowError); } + else if (strcmp(action, "status") == 0) + { + *stop = true; + result = process_exist(pidFilename, &pid); + switch (result) { + case 0: + printf("Running, pid: %d\n", (int)pid); + break; + case ENOENT: + printf("NOT running\n"); + break; + default: + printf("Unkown status\n"); + break; + } + return result; + } else if (strcmp(action, "restart") == 0) { return process_restart(pidFilename); diff --git a/src/process_ctrl.h b/src/process_ctrl.h index 105bf55..d5d041c 100644 --- a/src/process_ctrl.h +++ b/src/process_ctrl.h @@ -42,7 +42,7 @@ int process_stop_ex(const char *pidFilename, const bool bShowError); int process_restart(const char *pidFilename); -int process_exist(const char *pidFilename); +int process_exist(const char *pidFilename, pid_t *pid); int process_action(const char *pidFilename, const char *action, bool *stop);