Eight Linux commands that can be used to manage the entire life cycle of a process

Generally speaking, there are three main states in the life cycle of an application process: start, run, and stop. If we want to be competent administrators, every state can and should be carefully managed. These eight commands can be used to manage the entire life cycle of the process.

Start process

The easiest way to start a process is to type its name in the command line and press Enter. If you want to start the Nginx web server, type nginx. Maybe you just want to see its version.

alan@workstation:~$nginx

alan@workstation:~$nginx-v

nginxversion:nginx/1.14.0

View your executable path

The above demonstration of the startup process assumes that the executable file is located in your executable path. Understanding this path is the key to reliably initiating and managing the process. Administrators usually customize this path for the purpose they want. You can use echo $PATH to view your executable path.

alan@workstation:~$echo$PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin

WHICH

Use the which command to view the full path of the executable file.

alan@workstation:~$whichnginx

/opt/nginx/bin/nginx

I will use the popular web server software Nginx as my example. Assume that Nginx is installed. If the command which nginx executes returns nothing, then Nginx cannot be found, because it only searches the executable path you specified. There are three ways to remedy the situation where a process cannot be started simply by name. The first is to type in the full path-although, I am reluctant to type in the full path, will you?

alan@workstation:~$/home/alan/web/prod/nginx/sbin/nginx-v

nginxversion:nginx/1.14.0

The second solution is to install the application in a directory in the executable file path. However, this may sometimes be impossible, especially if you do not have root privileges.

The third solution is to update your executable path environment variables to include the installation directory of the specific application you want to use. This solution is related to the shell. For example, Bash users need to edit the PATH= line in their .bashrc file.

PATH="$HOME/web/prod/nginx/sbin:$PATH"

Now, repeat your echo and which commands or try to check the version. Much easier!

alan@workstation:~$echo$PATH

/home/alan/web/prod/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin

alan@workstation:~$whichnginx

/home/alan/web/prod/nginx/sbin/nginx

alan@workstation:~$nginx-v

nginxversion:nginx/1.14.0

Keep the process running

NOHUP

When logging off or closing the terminal, the process may not continue to run. In this special case, the process can continue to run by putting the nohup command before the command to be run. In addition, appending an ampersand will send the process to the background and allow you to continue using the terminal. For example, suppose you want to run myprogram.sh.

nohup myprogram.sh &

nohup will return the PID of the running process. Next I will talk more about PID.

Manage running processes

Each process has a unique process identification number (PID). This number is what we use to manage each process. We can also use the process name, which I will demonstrate below. There are several commands to check the status of a running process. Let's take a quick look at these commands.

PS

The most common is the ps command. The default output of ps is a simple list of processes running in the current terminal. As shown below, the first column contains PID.

alan@workstation:~$ps

PIDTTYTIMECMD

23989pts/000:00:00bash

24148pts/000:00:00ps

I want to see the Nginx process I started earlier. For this, I told ps to show me every running process (-e) and the complete list (-f).

Eight Linux commands that can be used to manage the entire life cycle of a process

You can see the Nginx process in the output of the ps command above. This command shows nearly 300 lines, but I shortened it in this example. As you can imagine, trying to process 300 lines of process information is a bit confusing. We can pipe this output to grep and filter it to show only nginx.

Eight Linux commands that can be used to manage the entire life cycle of a process

It's really better. We can quickly see that Nginx has 20520 and 20521 PIDs.

PGREP

The pgrep command simplifies the problems encountered when calling grep alone.

alan@workstation:~$pgrepnginx

20520

20521

Suppose you are in a hosting environment where multiple users are running several different Nginx instances. You can use the -u option to exclude others from the output.

alan@workstation:~$pgrep-ualannginx

20520

20521

PIDOF

Another useful one is pidof. This command will check the PID of a specific binary file, even if another process with the same name is running. To build an example, I copied my Nginx to the second directory and started it with the corresponding path prefix. In real life, this instance may be located in a different location, such as a directory owned by different users. If I run two Nginx instances, the pidof output shows all their processes.

Eight Linux commands that can be used to manage the entire life cycle of a process

Using grep or pgrep will display the PID number, but we may not be able to distinguish which instance is which.

alan@workstation:~$pgrepnginx

20881

20882

20895

20896

The pidof command can be used to determine the PID of each specific Nginx instance.

alan@workstation:~$pidof/home/alan/web/prod/nginxsec/sbin/nginx

2088220881

alan@workstation:~$pidof/home/alan/web/prod/nginx/sbin/nginx

2089620895

TOP

The top command has a long history and is very useful for viewing the details of running processes and quickly identifying problems such as memory consumption. The default view is shown below.

Eight Linux commands that can be used to manage the entire life cycle of a process

You can change the update interval by typing the letter s and the number of update seconds you like. To make it easier to monitor our example Nginx process, we can use the -p option and pass the PID to call top. This output is much cleaner.

Eight Linux commands that can be used to manage the entire life cycle of a process

It is very important to correctly determine the PID when managing the process, especially when terminating the process. In addition, if top is used in this way, every time one of these processes stops or a new process starts, top needs to be notified of a new process.

Terminate the process

KILL

Interestingly, there is no stop command. In Linux, there is the kill command. kill is used to send signals to the process. The most commonly used signals are "stop" (SIGTERM) or "kill" (SIGKILL). However, there is more. Here are some examples. The complete list can be displayed with kill -L.

Eight Linux commands that can be used to manage the entire life cycle of a process

Note that the 9th signal is SIGKILL. Usually, we will issue a command such as kill -9 20896. The default signal is 15, which is SIGTERM. Keep in mind that many applications have their own method of stopping. Nginx uses the -s option to pass signals, such as stop or reload. Generally, I prefer to use a specific method of the application to stop the operation. However, I will demonstrate using the kill command to stop the Nginx process 20896, and then use pgrep to confirm that it has stopped. PID 20896 no longer appears.

alan@workstation:~$kill-920896

alan@workstation:~$pgrepnginx

20881

20882

20895

22123

PKILL

The command pkill is similar to pgrep because it can search by name. This means that you must be very careful when using pkill. In my Nginx example, if I just want to kill an Nginx instance, I might not choose to use it. I can pass the Nginx option -s stop to a specific instance to eliminate it, or I need to use grep to filter the entire ps output.

/home/alan/web/prod/nginx/sbin/nginx-sstop

/home/alan/web/prod/nginxsec/sbin/nginx-sstop

If I want to use pkill, I can include the -f option to let pkill filter the entire command line parameters. This of course also applies to pgrep. So, before executing pkill -f, I can first confirm with pgrep -a.

alan@workstation:~$pgrep-anginx

20881nginx:masterprocess./nginx-p/home/alan/web/prod/nginxsec

20882nginx:workerprocess

20895nginx:master processnginx

20896nginx:workerprocess

I can also use pgrep -f to narrow down my results. pkill uses the same parameters to stop the process.

alan@workstation:~$pgrep-fnginxsec

20881

alan@workstation:~$pkill-fnginxsec

The key point to remember with pgrep (especially pkill) is that you must always ensure the accuracy of search results so that you don't inadvertently affect the wrong process.

Most of these commands have many command line options, so I always recommend reading the man page for each command. Although most of these commands exist on platforms such as Linux, Solaris, and BSD, there are some differences. When working on the command line or writing scripts, always test and be ready to make corrections as needed.

DP Cables

UCOAX DisplayPort cables transmit high definition audio and video from your computer to a monitor for video streaming or gaming;

The Display cable connects and configures your monitor for an Extended Desktop or Mirrored Displays.


UCOAX Custom Made Display Port (DP TO DP) cables support video resolutions up to 2560x1440 and 1080P (Full HD), and flawless audio pass-thru for uncompressed digital 7.1, 5.1 or 2 channels

Gold-plated connectors, bare copper conductors, and foil & braid shielding combine together to provide both superior performance and reliable connectivity of the DisplayPort 1.2 cable.

Flexiable length make your work and life easier.

Dp Cables,Dp To Dp Cable,Mini Dp To Dp 1.4 Cable,Dp Cable

UCOAX , https://www.ucoax.com