In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "Linux effective management process commands", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "Linux effective management process commands" it!
In general, the life cycle of an application process has three main states: 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 lifecycle of the process.
Start the process
The easiest way to start a process is to type its name on the command line and press enter. If you want to start the Nginx web server, type nginx. Maybe you just want to see the version.
Alan@workstation:~$ nginx alan@workstation:~$ nginx-vnginx version: nginx/1.14.0 to view your executable path
The above demonstration of the startup process assumes that the executable is in your executable path. Understanding this path is the key to starting and managing processes reliably. 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/binWHICH
Use the which command to see the full path to the executable.
Alan@workstation:~$ which nginx/opt/nginx/bin/nginx
I will use the popular web server software Nginx as my example. Assume that Nginx is installed. If the command to execute which nginx returns nothing, then Nginx cannot be found because it only searches the executable path that you specify. There are three ways to remedy a situation where a process cannot simply be started by name. The first is to type the full path-although, I am reluctant to enter the whole path, will you?
Alan@workstation:~$ / home/alan/web/prod/nginx/sbin/nginx-vnginx version: nginx/1.14.0
The second solution is to install the application in a directory in the executable path. However, this can sometimes be impossible, especially if you don't have root permissions.
The third solution is to update your executable path environment variables, including the installation directory of the specific application you want to use. This solution is related to 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. It's much easier!
Alan@workstation:~$ echo $PATH/home/alan/web/prod/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin alan@workstation:~$ which nginx/home/alan/web/prod/nginx/sbin/nginx alan@workstation:~$ nginx-v nginx version: nginx/1.14.0 keeps the process running NOHUP
The process may not continue to run when you log out or close the terminal. This special case can be done by keeping the process running before the command to be run using the nohup command. In addition, attaching a & symbol 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 returns the PID on which the process is running. I'll talk more about PID next.
Manage running processes
Each process has a * process identification number (PID). This is the number we use to manage each process. We can also use the process name, which I will demonstrate below. There are several commands that 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 on the current terminal. As shown below, the * * column contains PID.
Alan@workstation:~$ psPID TTY TIME CMD23989 pts/0 00:00:00 bash24148 pts/0 00:00:00 ps
I want to see the Nginx process I started earlier. To do this, I told ps to show me each running process (- e) and a complete list (- f).
Alan@workstation:~$ ps-efUID PID PPID C STIME TTY TIME CMDroot 100 Aug18? 00:00:10 / sbin/init splashroot 2 00 Aug18? 00:00:00 [kthreadd] root 4 20 Aug18? 00:00:00 [kworker/0:0H] root 6 20 Aug18? 00:00:00 [ Mm_percpu_wq] root 7 20 Aug18? 00:00:00 [ksoftirqd/0] root 8 20 Aug18? 00:00:20 [rcu_sched] root 9 20 Aug18? 00:00:00 [rcu_bh] root 10 20 Aug18? 00:00:00 [migration/0] root 11 20 Aug18? 00:00:00 [watchdog/0] root 12 2 0 Aug18? 00:00:00 [cpuhp/0] root 13 2 0 Aug18? 00:00:00 [cpuhp/1] root 14 2 0 Aug18? 00:00:00 [watchdog/1] root 15 2 0 Aug18? 00:00:00 [migration/1] root 16 20 Aug18? 00:00:00 [ksoftirqd/1] alan 20506 20496 0 10:39 pts/0 00:00:00 bashalan 20520 1454 0 10:39? 00:00:00 nginx: master process nginxalan 20521 20520 0 10:39? 00:00:00 nginx: worker processalan 20526 20506 0 10:39 pts/0 00:00:00 man psalan 20536 20526 0 10:39 pts/0 00:00:00 pageralan 20564 20496 0 10:40 pts/1 00:00:00 bash
You can see the Nginx process in the output of the ps command above. This command displays 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 feed this output to grep and filter to show only nginx.
Alan@workstation:~$ ps-ef | grep nginxalan 20520 1454 0 10:39? 00:00:00 nginx: master process nginxalan 20521 20520 0 10:39? 00:00:00 nginx: worker process
It's even better. As we can quickly see, Nginx has 20520 and 20521 PID.
PGREP
The pgrep command simplifies the problems encountered in calling grep alone.
Alan@workstation:~$ pgrep nginx2052020521
Suppose you are in a managed 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-u alan nginx2052020521PIDOF
Another good thing to use is pidof. This command checks the PID of a specific binary, even if another process with the same name is running. To create an example, I copy my Nginx to the second directory and start it with the appropriate path prefix. In real life, this instance may be located in different locations, such as directories owned by different users. If I run two Nginx instances, the pidof output shows all their processes.
Alan@workstation:~$ ps-ef | grep nginxalan 20881 1454 0 11:18? 00:00:00 nginx: master process. / nginx-p / home/alan/web/prod/nginxsecalan 20882 20881 0 11:18? 00:00:00 nginx: worker processalan 20895 1454 0 11:19? 00:00:00 nginx: master process nginxalan 20896 20895 0 11:19? 00:00:00 nginx: worker process
Using grep or pgrep will display PID numbers, but we may not be able to tell which instance is which.
Alan@workstation:~$ pgrep nginx20881208822089520896
The pidof command can be used to determine the PID for each specific Nginx instance.
Alan@workstation:~$ pidof / home/alan/web/prod/nginxsec/sbin/nginx20882 20881 alan@workstation:~$ pidof / home/alan/web/prod/nginx/sbin/nginx20896 20895TOP
The top command has a long history and is useful for looking at the details of running processes and quickly identifying issues such as memory consumption. Its default view is shown below.
Top-11:56:28 up 1 day, 13:37, 1 user, load average: 0.09,0.04, 0.03Tasks: 292 total, 3 running, 225 sleeping, 0 stopped, 0 zombie%Cpu (s): 0.1 us, 0.2 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 stKiB Mem: 16387132 total, 10854648 free, 1859036 used, 3673448 buff/cacheKiB Swap: 0 total 0 free, 0 used. 14176540 avail Mem PID USER PR NI VIRT RES SHR S% CPU% MEM TIME+ COMMAND17270 alan 200 3930764 247288 98992 R 0.7 1.55 gnome-shell20496 alan 58.22 gnome-shell20496 alan 20 0816144 45416 29844 S 0.5 0.3 gnome-terminal-21110 alan 22.16 gnome-terminal-21110 alan 20 041940 3988 3188 R 0.0.017 top 1 root 20 0225564 9416 6768 S 0.0 0.10 systemd 2 root 20 00 00 S 0.0 0.00: 00.01 kthreadd 4 root 0-2000 0 I 0.0 0.00: 00.00 kworker/0:0H 6 root 0-20 000 I 0.0 0.00: 00.00 mm _ percpu_wq 7 root 20 00 00 S 0.0 0.0 0:00.08 ksoftirqd/0
You can change the update interval by typing the letter s and the number of seconds you like to update. To make it easier to monitor our sample Nginx process, we can use the-p option and pass PID to call top. This output is much cleaner.
Alan@workstation:~$ top-p20881-p20882-p20895-p20896 Tasks: 4 total, 0 running, 4 sleeping, 0 stopped, 0 zombie%Cpu (s): 2.8us, 1.3 sy, 0.0 ni, 95.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 stKiB Mem: 16387132 total, 10856008 free, 1857648 used, 3673476 buff/cacheKiB Swap: 0 total, 0 free, 0 used. 14177928 avail Mem PID USER PR NI VIRT RES SHR S% CPU% MEM TIME+ COMMAND20881 alan 20 0 12016 348 0 S 0.0 0.00: 00.00 nginx20882 alan 20 0 12460 1644 932 S 0.0 0.00: 00.00 nginx20895 alan 200 12016 352 0 S 0.0 0.00: 00.00 nginx20896 alan 20 0 12460 1628 912 S 0.0 0.00: 00.00 nginx
When managing processes, especially when terminating processes, it is very important to correctly determine the PID. In addition, if top is used in this way, top needs to be informed that there is a new process every time one of these processes stops or a new process starts.
Terminating the process KILL
Interestingly, there is no stop command. In Linux, there is a kill command. Kill is used to send signals to processes. The most commonly used signals are "SIGTERM" or "SIGKILL". However, there is more. Here are some examples. The complete list can be displayed in kill-L.
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR111) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
Note that signal 9 is SIGKILL, and usually we issue commands such as kill-9 20896. The default signal is 15, which is SIGTERM. Keep in mind that many applications have their own stop methods. Nginx uses the-s option to pass signals, such as stop or reload. In general, I prefer to use the specific methods 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-9 20896 alan@workstation:~$ pgrep nginx20881208822089522123PKILL
The command pkill is similar to pgrep in that it can be searched by name. This means that you must be very careful when using pkill. In my Nginx example, if I wanted to kill only one instance of Nginx, 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-s stop/home/alan/web/prod/nginxsec/sbin/nginx-s stop
If I want to use pkill, I can include the-f option to let pkill filter the entire command line argument. Of course, this also applies to pgrep. So, before I execute pkill-f, I can first confirm it with pgrep-a.
Alan@workstation:~$ pgrep-a nginx20881 nginx: master process. / nginx-p / home/alan/web/prod/nginxsec20882 nginx: worker process20895 nginx: master process nginx20896 nginx: worker process
I can also use pgrep-f to narrow my results. Pkill stops the process with the same parameters.
Alan@workstation:~$ pgrep-f nginxsec20881 alan@workstation:~$ pkill-f nginxsec
The key point for pgrep (and pkill in particular) to remember is that you must always ensure that your search results are accurate 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 man page for each command. While most of these commands exist on platforms such as Linux, Solaris, and BSD, there are some differences. Always test and be ready to correct as needed when working on the command line or writing a script.
At this point, I believe you have a deeper understanding of the "Linux effective management process command", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.