process_action support action status

pull/40/head
YuQing 2021-06-26 11:00:52 +08:00
parent 3a61bf8074
commit 3ad4a89ff5
3 changed files with 34 additions and 11 deletions

View File

@ -1,4 +1,7 @@
Version 1.53 2021-06-26
* process_action support action status
Version 1.52 2021-06-08 Version 1.52 2021-06-08
* process_stop more gracefully (kill -9 on timeout) * process_stop more gracefully (kill -9 on timeout)
* add function fc_queue_pop_to_queue_ex * add function fc_queue_pop_to_queue_ex

View File

@ -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; int result;
if ((result=get_pid_from_file(pidFilename, &pid)) != 0) { if ((result=get_pid_from_file(pidFilename, pid)) != 0) {
if (result == ENOENT) { if (result == ENOENT) {
return false; return result;
} }
else { else {
fprintf(stderr, "get pid from file: %s fail, " \ fprintf(stderr, "get pid from file: %s fail, " \
"errno: %d, error info: %s\n", "errno: %d, error info: %s\n",
pidFilename, result, strerror(result)); pidFilename, result, strerror(result));
return true; return result;
} }
} }
if (kill(pid, 0) == 0) { if (kill(*pid, 0) == 0) {
return true; return 0;
} }
else if (errno == ENOENT || errno == ESRCH) { else if (errno == ENOENT || errno == ESRCH) {
return false; return ENOENT;
} }
else { else {
result = errno != 0 ? errno : EPERM;
fprintf(stderr, "kill pid: %d fail, errno: %d, error info: %s\n", fprintf(stderr, "kill pid: %d fail, errno: %d, error info: %s\n",
(int)pid, errno, strerror(errno)); (int)*pid, result, strerror(result));
return true; 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) int process_action(const char *pidFilename, const char *action, bool *stop)
{ {
const bool bShowError = true; const bool bShowError = true;
int result;
pid_t pid;
*stop = false; *stop = false;
if (action == NULL) if (action == NULL)
{ {
@ -341,6 +344,23 @@ int process_action(const char *pidFilename, const char *action, bool *stop)
*stop = true; *stop = true;
return process_stop_ex(pidFilename, bShowError); 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) else if (strcmp(action, "restart") == 0)
{ {
return process_restart(pidFilename); return process_restart(pidFilename);

View File

@ -42,7 +42,7 @@ int process_stop_ex(const char *pidFilename, const bool bShowError);
int process_restart(const char *pidFilename); 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); int process_action(const char *pidFilename, const char *action, bool *stop);