Monday, August 22, 2016

Managing Processes

top
• PID  The process ID of the process.
• USER The name of the user who owns the process.
• PR   The priority assigned to the process.
• NI   This is the nice value of the process.
• VIRT The amount of virtual memory used by the process.
• RES  The amount of physical RAM the process is using (its resident size) in kilobytes.
• SHR  The amount of shared memory used by the process.
•S    The status of the process.
         Possible values include the following:
               •D Uninterruptibly sleeping
               •R Running
               •S Sleeping
               •T Traced or stopped
               •Z Zombie

• %CPU     The percentage of CPU time used by the process.
• %MEM     The percentage of available physical RAM used by the process.
• TIME+    The total amount of CPU time the process has consumed since being started.
• COMMAND  The name of the command that was entered to start the process.






ps
Notice that the following information is displayed by default:
• PID The process ID of the process.
• TTY The name of the terminal session (shell) that the process is running within.
• TIME The amount of CPU time used by the process.
• CMD The name of the command that was entered to create the process.



>ps -e
To see all processes running on the system, you need to use the –e option with ps


>ps -ef
To see all processes running on the system, you need to use the –e option with ps

With the –f option, you can now view additional information, including the following:
• UID The user ID of the process’s owner.
• PPID The PID of the process’s parent process.
• C The amount of processor time utilized by the process.
• STIME The time that the process started.



>ps -efl
If you really want to crank things up, you can also use the –l option with the ps command. The –l option displays the long format of the ps output.

With the –l option, you can view the following information about processes running on your system:
•F The flags associated with the process.
This column uses the following codes:
    •1 Forked, but didn’t execute
    •4 Used root privileges
•S The state of the process.
This column uses the following codes:
    •D Uninterruptible sleep
    •R Running
    •S Interruptible sleep
    •T Stopped or traced
    •Z Zombied
•PRI The priority of the process.
•NI The nice value of the process.
•ADDR The memory address of the process.
•SZ The size of the process.
•WCHAN The name of the kernel function in which the process is sleeping. You will see a dash (–) in this column if the process is currently running.


free
The free command displays the amount of free and allocated RAM and swap memory in your system. You can use the –m option to
display memory statistics in megabytes. You can also use the –t option to display totals for each category of information.

>free -mt




pgrep
com-bines the functionality of the ps and grep commands into a single utility. When you run pgrep, you specify certain
selection criteria that you want to look for.

-P ppid matches on the specified parent process ID.
-f name matches on the specified process name.
-u user_namematches on the specified process owner.

If you want to view the name of the process as well as its PID, use the –l option with pgrep com-mand.
>pgrep -l -u root
>pgrep -P 7124
>pgrep -f  top
>pgrep -u kenmsipe



nice
The nice utility can be used on Linux to launch a program with a different priority level.The PR value is the process’s kernel priority. The higher the number, the lower the priority of the process. The lower the number, the higher the priority of the process. The NI value is the nice value of the process. The nice value is factored into the kernel calculations that determine the priority of the process. The nice value for any Linux process can range between –20 and +19. The lower the nice value, the higher the priority of the process.

>nice –n nice_levelcommand


>nice –n –15 vi


renice
Setting Priorities of Running Processes.
>renice nice_value PID


>renice 4 8488



kill
terminate a process.
>kill –signal PID

kill signals that you can send to the process. Here are the most useful of these:


•SIGHUP This is kill signal 1. This signal restarts the process. After a restart, the process will have exactly the same PID that it had before. This is a very useful option for restarting a service for which you’ve made changes in a configuration file.


•SIGINT This is kill signal 2. This signal sends a ctrl-c key sequence to the process.


•SIGKILL This is kill signal 9. This is a brute-force signal that kills the process. If the process was hung badly, this option will force it to stop. However, the process may not clean up after itself if this signal is used. The resources allocated to the process may remain allocated until the system is restarted.


•SIGTERM This is kill signal 15. This signal tells the process to terminate immediately. This is the default signal sent by kill if you omit a signal in the command line. This signal allows the process to clean up after itself before exiting.


If you experience a hung process that needs to be killed, I suggest you use the following sequence:
1.Send a SIGINT first. If it doesn’t respond, then go on to step 2.
2.Send a SIGTERM. Usually, this will fix the problem and allow the process to exit cleanly. If it doesn’t, then go on to step 3.
3.Send a SIGKILL.


killall
command is very similar to the kill command. The syntax is almost the same. The key difference is that killall uses the command name of the process to be killed instead of its PID.


>killall –15 vi






pkill
Using pkill, you can search for processes that match the search criteria you specify and then send them a certain kill signal.


>pkill -SIGTERM -f vi



Keeping a Process Running After Logout


>nohup updatemydb &





screen
he screen command is an interest-ing utility that is particularly useful if you are accessing a Linux system remotely through an SSH connection. The key benefit of screen is that it allows you to use multiple shell windows from within a single SSH session


>screen
It doesn’t appear any differently. However, it is different because you are now inside of a window within screen.

•Pressing ctrl-a and then ? causes the screen help to be displayed.
•Pressing ctrl-a and then c causes a new screen window to be created. The old window you were working in remains active along with any processes that were running within it. For example, suppose you had top running in a screen window within an SSH session. You need to check your email, but you don’t want to stop running top. To do this, you can open a new window and access your mail. As you do, top stays running in the first window.
•Pressing ctrl-a and then n toggles between open windows in screen.
•Pressing ctrl-a and then d detaches your screen window and drops you back at your original shell prompt. However, whatever you had running in the window remains running. In fact, you can log completely out of the server and everything will keep working within the detached window.
•Entering screen –r reattaches you to a detached screen window. If you have multiple detached screen windows, you’ll be prompted to specify which one you want to reattach to

No comments:

Post a Comment