Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize automatic restart of Monitoring Program and Program by linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

Editor to share with you how to achieve linux monitoring program and program automatic restart method, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

Automatic restart using scripts

The easiest thing that comes to mind is to use the shell script, with a general idea:

Ps-ef | grep "$1" | grep-v "grep" | wc-l is the number of processes that get $1 ($1 represents the name of the process). The script determines the next step according to the number of processes. Through an endless loop, check the number of processes of the specified program in the system every 1 second. This can also be implemented using crontab.

This method is corny and can basically solve the problem, but there is a delay of 1 second. The author did not use this method in the application. For this shell script, please refer to the attachment code at the back of the article.

Exec+fork mode

The author finally uses the exec+fork way to achieve, the specific ideas are as follows:

The 1jie exec function replaces the current process with a new process, which is specified by the path or file parameter. You can use the exec function to switch the execution of a program from one program to another

2the fork function creates a new process and creates a new table item in the process table, while the creator (that is, the parent process) continues to execute according to the original process, and the child process executes its own control flow.

When fork starts a child process, the child process has its own life cycle and will run independently. We can call the wait function in the parent process to wait for the end of the child process.

I believe that at this point, readers have been able to think of solutions: 1) use fork system call to create a child process, 2) use the exec function in the child process to execute the program that needs to be restarted automatically, 3) execute wait in the parent process to wait for the end of the child process, and then recreate a new child process.

How to use it:

#. / portmap the path of the program to be monitored # parameters required by args portmap $. / supervisor. / portmap args.

The code is as follows:

/ * * supervisor * * author: liyangguang (liyangguang@software.ict.ac.cn) * * date: 2011-01-21 21:04:01 * * changes * 1, execl to execv * / # include # include intmain (int argc, char * * argv) {int ret, I, status;char * child_argv = {0}; pid_t pid;if (argc

< 2) { fprintf(stderr, "Usage:%s \n", argv[0]);return -1;}for (i = 1; i < argc; ++i) {child_argv[i-1] = (char *)malloc(strlen(argv[i])+1);strncpy(child_argv[i-1], argv[i], strlen(argv[i]));child_argv[i-1][strlen(argv[i])] = '\0';}while(1){ pid = fork(); if (pid == -1) {fprintf(stderr, "fork() error.errno:%d error:%s\n", errno, strerror(errno));break;}if (pid == 0) {ret = execv(child_argv[0], (char **)child_argv);//ret = execl(child_argv[0], "portmap", NULL, 0);if (ret < 0) {fprintf(stderr, "execv ret:%d errno:%d error:%s\n", ret, errno, strerror(errno));continue;}exit(0);} if (pid >

0) {pid = wait (& status); fprintf (stdout, "wait return");}} return 0;}

After the end status of the child process is returned, it is stored in status, and there are several macros below to determine the end status.

WIFEXITED (status) is a non-zero value if the child process ends normally.

WEXITSTATUS (status) gets the end code returned by the child process exit (), and usually uses WIFEXITED to determine whether it ends properly before using this macro.

WIFSIGNALED (status) this macro value is true if the child process ends because of a signal

WTERMSIG (status) gets the signal code that the child process is aborted because of the signal, and usually uses WIFSIGNALED to judge before using this macro.

WIFSTOPPED (status) this macro value is true if the child process is paused. This is generally only the case when using WUNTRACED.

WSTOPSIG (status) gets the signal code that initiates the pause of the child process, and usually uses WIFSTOPPED to judge before using this macro.

Summary of the return value of wait function

The code for shell scripting is as follows:

# function: CheckProcess# function: check whether a process exists # Parameter: $1-- the name of the process to be checked # return: 0 if it exists Otherwise, return 1.#----CheckProcess () {# to check whether the input parameter is valid if ["$1" = ""] Thenreturn 1 fi # $PROCESS_NUM gets the number of specified process names. If 0 is returned for 1, it is normal. If 1 does not return 1, it means there is an error. You need to restart PROCESS_NUM= `ps-ef | grep "$1" | grep-v "grep" | wc-l` if [$PROCESS_NUM-eq 1]; thenreturn 0 elsereturn 1 fi} # check whether while already exists in the test instance [1]; do CheckProcess "test" CheckQQ_RET=$? If [$CheckQQ_RET-eq 1]; then # kills all test processes and can change any operation you need to perform killall-9 test exec. / test & fi sleep 1done above is all the contents of this article "how to monitor programs and program automatic restart methods". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report