Disclosure of Invention
The application provides a background task monitoring method, which is characterized in that the method is applied to network equipment, the network equipment runs a monitoring task corresponding to a background task, and the method comprises the following steps:
the monitoring task periodically reads the background task marked as a stop running state in the background task monitoring list; the background task monitoring list comprises an API (application programming interface) corresponding to a background task;
calling an API (application programming interface) corresponding to the background task to determine whether the background task needs to be started;
and if so, starting the background task.
Optionally, the network device is loaded with a scheduling program; the scheduling program is used for executing a task termination process corresponding to the background task; further comprising:
the scheduling program monitors the event message of the stop of the background task;
and responding to the event message, and issuing a notification message that the background task stops running to the monitoring task.
Optionally, the periodically reading, by the monitoring task, the background task marked as a stop-running state in the background task monitoring list includes:
receiving the notification message sent by the scheduling program;
and responding to the notification message, and periodically reading the background tasks marked as the stop running state in the background task monitoring list.
Optionally, the background task monitoring list includes a starting mode corresponding to the background task; the initiating includes:
inquiring a starting mode corresponding to the background task in the background task monitoring list;
and starting the background task based on the inquired starting mode.
Optionally, the method further includes:
after the background task is started, generating a log file related to the starting of the background task;
and reporting the generated log file.
The present application further provides a background task monitoring device, which is applied to a network device, where the network device runs a monitoring task corresponding to a background task, and the monitoring task includes:
the monitoring module is used for periodically reading the background tasks marked as the running stop state in the background task monitoring list by the monitoring task; the background task monitoring list comprises an API (application programming interface) corresponding to a background task;
the determining module is used for calling an API (application program interface) corresponding to the background task to determine whether the background task needs to be started or not;
and the starting module is used for starting the background task if the starting module needs to be started.
Optionally, the apparatus further comprises:
the scheduling module is used for monitoring the event message of the stop of the background task;
and responding to the event message, and issuing a notification message that the background task stops running to the monitoring task.
Optionally, the monitoring module is further configured to:
receiving the notification message sent by the scheduling module;
and responding to the notification message, and periodically reading the background tasks marked as the stop running state in the background task monitoring list.
Optionally, the background task monitoring list includes a starting mode corresponding to the background task; the start module is further configured to:
inquiring a starting mode corresponding to the background task in the background task monitoring list;
and starting the background task based on the inquired starting mode.
Optionally, the apparatus further comprises:
a reporting module for
After the background task is started, generating a log file related to the starting of the background task;
and reporting the generated log file.
In the method provided by the application, the monitoring task periodically reads the background task marked as a stop running state in the background task monitoring list; the background task monitoring list comprises an API (application programming interface) corresponding to a background task; calling an API (application programming interface) corresponding to the background task to determine whether the background task needs to be started; and if so, starting the background task.
In the scheme, before the background task marked as the running stop state is started, the monitoring task judges whether the background task needs to be started or not through the API (application programming interface), so that the resource waste of the system is avoided.
Detailed Description
In the prior art, in order to restart a background task that is stopped due to an unexpected reason in an operating system, a monitoring task is directly communicated with the background task, and the background task is restarted when the task is monitored to be stopped. The method of establishing communication can be roughly divided into a method of heartbeat message and a method of polling.
The monitoring task may be understood as a background task started in the operating system, and is used for monitoring other background tasks.
The operating system may further include a scheduler, where the scheduler may be understood as a section of code cached in the operating system for executing background task management, and when all background tasks in the operating system are finished, the scheduler is called to execute a task termination process, where the task termination process includes recovering resources (such as a memory, a file, a task ID, and the like), closing an external connection, deleting a temporary file, and the like. In the apparatus item, the scheduling module may be equivalently understood as a scheduler.
When the monitoring task directly communicates with the background task by monitoring the heartbeat message of the background task, please refer to fig. 2, where fig. 2 is a signaling flowchart of the background task monitoring method. The background task periodically sends heartbeat messages to the monitoring task, if the monitoring task does not receive the heartbeat messages sent by the background task within a certain time, the background task is determined to stop running, and the monitoring task starts the background task according to a preset starting mode corresponding to the background task.
When the monitoring task directly communicates with the background task through a polling method, please refer to fig. 3, where fig. 3 is a signaling flowchart of the background task monitoring method. Referring to table 1, table 1 is a background task monitoring list shown in the prior art, each line in the background task monitoring list corresponds to a background task, and each line may include a task name and a starting mode of the corresponding background task and path information of a file (PID file) storing the task identification ID. And each background task writes the task identification ID into a corresponding PID file during initial starting. The monitoring task periodically traverses the background task monitoring list, and checks whether a task identifier ID in the running background task is equal to a task identifier ID stored in a PID file in each line aiming at each line, if so, the task is still in a running state; if not, the task is stopped running, and the background task is started according to the starting mode described in the row. Through the background task monitoring list, the monitoring task can realize real-time monitoring on the background task.
In the prior art, the monitoring task directly starts the background task marked as the stop running state, and does not make a judgment on whether the background task needs to be started, which may cause waste of system resources.
| Task name | Starting mode | PID file path information |
| cpu_monitor | /usr/bin/cpu_monitor-d | /var/run/cpu_monitor.pid |
TABLE 1
In view of the above considerations, the present application provides a background task monitoring method. In the method provided by the application, the monitoring task periodically reads the background task marked as a stop running state in the background task monitoring list; the background task monitoring list comprises an API (application programming interface) corresponding to a background task; calling an API (application programming interface) corresponding to the background task to determine whether the background task needs to be started; and if so, starting the background task.
In the scheme, before the background task marked as the running stop state is started, the monitoring task judges whether the background task needs to be started or not through the API (application programming interface), so that the resource waste of the system is avoided.
The present application is described below with reference to specific embodiments and specific application scenarios.
Referring to fig. 1, fig. 1 is a background task monitoring method provided in an embodiment of the present application, and is applied to a network device, where the network device runs a monitoring task corresponding to a background task, and executes the following steps:
s101, the monitoring task periodically reads a background task marked as a stop running state in a background task monitoring list; the background task monitoring list comprises an API (application programming interface) corresponding to a background task;
s102, calling an API (application program interface) corresponding to the background task to determine whether the background task needs to be started;
s103, if yes, starting the background task.
The network device may include any general-purpose device having an operating system; for example, it may be a personal computer, a server, or the like, in which an operating system is installed.
The API interface may be an interface function provided by the background task, and the interface function may be configured to determine whether to start the background task corresponding to the interface function. The API interface is called by an interface calling method corresponding to the API interface, the calling method is implemented based on a programming language, and various implementation manners are available, which are not intended to limit the present invention.
Referring to fig. 4, fig. 4 is a signaling flowchart of a background task monitoring method according to this embodiment.
In this example, the monitoring task may generate a background task monitoring list, please refer to table 2, where table 2 is a background task monitoring list shown in this embodiment, and the list may include all background tasks running in the operating system; each line of the background task monitoring list corresponds to a background task, and each line may include a task name, a task state, a starting mode, and an API interface of the background task corresponding to the line. Through the background task monitoring list, the monitoring task can manage and update the background task.
| Task name | Task state | Starting mode | API interface |
| cpu_monitor | Run/stop | /usr/bin/cpu_monitor-d | check_cpu_monitor_state |
TABLE 2
In this example, the operating system further includes a scheduler, and the task termination process executed by the scheduler adds an event of listening to the task ending operation on the basis of the prior art. And the scheduling program monitors the background task in real time, and when the scheduling program monitors an event that the background task stops running, the scheduling program sends a notification message to the monitoring task, wherein the notification message is used for indicating the background task to stop running.
In this way, the scheduler can only issue the notification message that the background task stops running to the monitoring task when monitoring that the background task stops running, so that a large amount of communication between the monitoring task and the background task in the prior art is avoided, and waste of system resources is avoided.
In this example, after receiving the notification message sent by the scheduler, the monitoring task marks the background task indicated by the message as a stop-running state in the generated background task monitoring list.
In this example, the monitoring task may periodically traverse the background task monitoring list, and read the background task marked as the stop-running state in the background task list.
In this example, after the background task marked as the stop operation state is read, the monitoring task calls an API interface corresponding to the background task to determine whether the background task needs to be started.
The API interface may determine whether a background task corresponding to the API interface needs to be started according to a system type, a deployment state, configuration data, and the like. It is understood that the above examples are only illustrative and not intended to limit the present invention, since the determination conditions of the system type, the deployment state, and the configuration data are very complicated and flexible.
By the method, on one hand, before the background task marked as the running stop state is started, the monitoring task judges whether the background task needs to be started or not through the API, so that the resource waste of the system is avoided.
On the other hand, because the monitoring task is realized through the API, the source code corresponding to the background task does not need to be modified, and the realization is simpler and more convenient; the problem of PID multiplexing caused by PID adopted in the prior art is also avoided.
In an embodiment shown in the figure, after the background task of the marked interface, which is in the running stop state, is read, the monitoring task determines whether the background task marked in the running stop state is running in the operating system by checking a policy, and when it is determined that the background task marked in the running stop state is not running, determines whether the background task needs to be started by calling an interface function corresponding to the background task.
Wherein the checking policy may include:
in an operating system, the monitoring task queries whether the name of the running task is the same as the name of the task marked as the running stop state;
if the task does not exist, determining that the task stops running in the operating system;
otherwise, it is determined that the task is running in the operating system.
By this embodiment, interference of some specific factors can be excluded, for example, in a time interval of two traversals of the monitoring task, the background task marked as the stop running state is restarted by other means; the other way may be that the system maintenance personnel manually start the background task.
In this example, if it is determined that the background task needs to be started, a starting mode corresponding to the background task is queried in the background task monitoring list, and the background task is started based on the queried starting mode.
In this example, after the monitoring task restarts the background task, the monitoring task may further generate a log file related to the starting of the background task, and report the generated log file to the server, so that an administrator may find the reason why the background task stops running by accessing the log file stored on the server, and solve the problem. The file related to the starting of the background task may include a task name, a starting time, a last running time, a reason for stopping running, and the like of the background task.
In the method provided by the application, the monitoring task periodically reads the background task marked as a stop running state in the background task monitoring list; the background task monitoring list comprises an API (application programming interface) corresponding to a background task; calling an API (application programming interface) corresponding to the background task to determine whether the background task needs to be started; and if so, starting the background task.
In the scheme, before the background task marked as the running stop state is started, the monitoring task judges whether the background task needs to be started or not through the API (application programming interface), so that the resource waste of the system is avoided.
Corresponding to the method embodiment, the application also provides an embodiment of the device.
Referring to fig. 5, fig. 5 is a diagram illustrating a backgroundtask monitoring apparatus 50 applied to a network device according to an embodiment of the present application; referring to fig. 6, a hardware architecture related to a network device carrying the backgroundtask monitoring device 50 generally includes a CPU, a memory, a non-volatile memory, an internal bus, and the like; taking a software implementation as an example, the backgroundtask monitoring device 50 may be generally understood as a computer program loaded in a memory, and a logic device formed by combining software and hardware after a CPU runs, where the backgroundtask monitoring device 50 is applied to a network device, and the network device runs a monitoring task corresponding to a background task, where the device includes:
themonitoring module 501 is configured to periodically read a background task marked as a stop-running state in a background task monitoring list by the monitoring task; the background task monitoring list comprises an API (application programming interface) corresponding to a background task;
a determiningmodule 502, configured to invoke an API interface corresponding to the background task to determine whether the background task needs to be started;
a startingmodule 503, if starting is needed, for starting the background task.
In this example, the apparatus further comprises:
ascheduling module 505, configured to monitor an event message that the background task stops running;
and responding to the event message, and issuing a notification message that the background task stops running to the monitoring task.
In this example, the monitoring module is further configured to:
receiving the notification message sent by the scheduling module;
and responding to the notification message, and periodically reading the background tasks marked as the stop running state in the background task monitoring list.
In this example, the background task monitoring list includes a starting mode corresponding to the background task; the start module is further configured to:
inquiring a starting mode corresponding to the background task in the background task monitoring list;
and starting the background task based on the inquired starting mode.
In this example, the apparatus further comprises:
areporting module 504, configured to
After the background task is started, generating a log file related to the starting of the background task;
and reporting the generated log file.
For the device embodiments, since they substantially correspond to the method embodiments, reference may be made to the partial description of the method embodiments for relevant points. The above-described embodiments of the apparatus are merely illustrative, and the units described as separate parts may or may not be physically separate, and parts displayed as units may or may not be physical units, may be located in one place, or may be distributed on a plurality of network units. Some or all of the modules can be selected according to actual needs to achieve the purpose of the scheme of the application. One of ordinary skill in the art can understand and implement it without inventive effort.
The above description is only exemplary of the present application and should not be taken as limiting the present application, as any modification, equivalent replacement, or improvement made within the spirit and principle of the present application should be included in the scope of protection of the present application.