Get the App
SLTechnology News&Howtos  ›  Development  › 

How nodejs ends a process

Shulou Source: shulou.com Published: 2022-06-02 11:48:51 09月19日 Update

This article mainly introduces "how nodejs ends a process". In daily operation, I believe many people have doubts about how nodejs ends a process. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "how to end a process in nodejs"! Next, please follow the editor to study!

In nodejs, you can use the kill () method of the Process object to end a process, which can send a signal to the process identified by pid to kill the process group; syntax "process.kill (pid,'SIGTERM')".

This tutorial operating environment: windows7 system, nodejs version 12.19.0, DELL G3 computer.

NodeJS ends the process

The Process object is a global variable that provides information about and controls the current Node.js process. As a global variable, it is always available to Node.js applications without the use of require (). It can also use require () to explicitly access the

Process.kill (pid [, signal]) method

Added on: v0.0.06 (node)

Pid process PID

Signal | the signal to be sent. The type is a string or a number. Default value: SIGTERM.

The process.kill () method sends the signal to the process identified by pid

The signal name is a string such as' SIGINT' or 'SIGHUP'. For more information, see signal events and kill (2).

If the target pid does not exist, the method throws an error. As a special example, signal 0 can be used to test the existence of a process. On the Windows platform, an error is thrown if pid is used to kill a process group.

Even though the name of this function is process.kill (), it just sends a signal, which is similar to the kill system call. The signal sent may be to do something that has nothing to do with killing the target process.

A simple and crude code demonstration

First take a look at the official website documentation gentle code demonstration

Process.on ('SIGHUP', () = > {console.log (' receiving SIGHUP signal');}); setTimeout () = > {console.log ('exiting'); process.exit (0);}, 100); process.kill (process.pid, 'SIGHUP')

Demonstrate directly in a simple and rude way

I don't write the code carefully because of my working hours, but the overall idea is the same.

Requirements: go to the computer to see your own process pid to get your own corresponding pid and fill it into the variable. After completing the coding, use node to compile the code-> execute.

Node process.jslet pid = 11784According to the pid of the application on my PC, you can fill in the process.kill (pid, 'SIGTERM') according to the pid of your computer; / / end the pid process at this point, the study on "how nodejs ends a process" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Process signal method code computer learning variable more demo rude personal information global name character string object identity target system Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Microsoft Linux vpn Shulou Technology