Full Workload
Total Page:16
File Type:pdf, Size:1020Kb
Command Line: Process Control C@ELOLJ<I GifZ\jjXe[afYZfekifc =LCCNFIBCF8; !Qn\`Ycld#g_fkfZXj\%Zfd What is happening on your Linux machine? Various shell commands All of these parameters can be com- bined – for example, pstree -apu. give you details about system processes and help you control them. C`jk`e^GifZ\jj\jn`k_gj BY HEIKE JURZIK The ps command gives you a list of the processes currently running on your sys- henever you or an application ing. All other processes share this com- tem. If you do not specify any command- launch a program, the operat- mon “ancestor” – init starts the operat- line parameters, the tool will restrict the Ning system starts one or multi- ing system’s basic programs. pstree list to the current shell. If you are inter- ple processes. These processes can enter shows the relationship between “parent” ested in all of your processes, call ps various states: They can be processed, and “child” processes. This tree struc- with the x option (Listing 1). stopped, restarted, and – under normal ture shows you at a glance who is de- The tabular output in the shell tells circumstances – stopped again. Linux scended from whom (Figure 1). you the following characteristics: has something similar to access controls PID: The process identifier, a unique for processes: Only the user that started 8[[`k`feXcFlkglk number that you can use to reference a process can stop, restart, or terminate The tool gives you more detailed output a process individually. the process. if you set the -a flag. This tells pstree to TTY: The terminal or console on The only exception to this rule is the show you, in addition, the parameters which the process was started. A root user, who can control any process with which the programs are running. question mark indicates that the pro- on a system. On top of this are processes If you use a terminal that supports cess is not running on a terminal. that run on system user accounts, such different fonts and bold type, such as STAT: The process status. The states as nobody or lp – again, only root has Gnome Terminal or KDE’s Konsole, you can be S (sleeping), R (running), full access to them. In this issue, I will might also want to try the -h parameter. D (dead, the process cannot be re- be looking at tools that help you find out This tells pstree to highlight its own pro- started), or Z (zombie, a process that more about, and control, processes. cess and its ancestors. has terminated without correctly If you would like to use this practical returning its return status). Fe\9`^=Xd`cp feature for other processes, use -H with TIME: The computational time used. Processes are never isolated and are al- the process ID, and pstree will highlight COMMAND: The full command with ways in good company. In fact, they are the specified process and its family tree. all of its command-line options. in a hierarchical structure, with process Setting the -p option tells pstree to out- The ps command offers a number of ad- number 1, init, at the top. init is the first put the process ID (PID), and -u gives ditional options for adding more infor- process that Linux launches after boot- you the user. mation to the output. For example, u AUGUST 2008 ISSUE 93 89 C@ELOLJ<I Command Line: Process Control shows the process owner and CPU cycles sole and carry on working. As an alter- The nohup program gives you a work- or memory percentage, and a gives you native, you can move the process into around by protecting the process against a list of all processes for all users. The l the background when you start it, or at the shell’s HUP signal (see the next sec- option is also practical – this lengthy a later time. tion), thus allowing it to continue run- output gives you additional information To move the process into the back- ning after you close the terminal session. on the PPID (parent process identifier) ground when you launch it, just add the In other words, this cuts the ties be- and on the UID (user identification) of ampersand character (&) to the com- tween the child process and its parent. the user who launched the process. mand line (Listing 2, line 1). The Xpdf Simply call nohup with the program To display what can be fairly lengthy window launches, the shell tells you the (and its options): command-line parameters in the COM- process ID (5622), and bash can then MAND column, you might want to set accept more commands. nohup find /scratch3/mp3 -name 5 w for wider output, and you can use the Besides the process ID, you can also "*.ogg" > ogg_liste.txt option multiple times. As shown in Fig- see the job ID in square brackets. The ure 2, you can combine these parameters job ID is allocated as a consecutive num- This approach does not automatically as needed. ber by the shell. If you launch another move the process to the background, but program in the same session, you will the methods I just described will take =fi\^ifle[Xe[9XZb^ifle[ see that bash assigns job ID 2 (Listing 2, care of this. In some cases, a program you launch in line 3). The jobs command tells you Closing the shell means that you can’t the shell might run for an extended pe- which jobs are running in the current communicate with the process – or does riod of time. Graphical programs that shell (Listing 2, line 6). it? Even if you do not have a direct ter- you launch in a terminal window block After a program has completed its minal connection, you can still control the shell, preventing any command task, the shell displays the job ID along the program using the signals discussed input. In cases like this, you can run out with a status message (Done) and the next. and grab a coffee or open a second con- program name: 8e<e[kf<m\ipk_`e^6 Listing 1: Command [3]+ Done 5 Although the name might suggest other- 01 $ ps x xpdf article.pdf wise, the kill program need not be fatal. On the contrary, you use it to send sig- 02 PID TTY STAT TIME The job ID is also useful if you need to nals to processes, including polite re- COMMAND move a background process into the quests to stop working. 03 3011 ? Ss 0:00 / foreground, or vice versa. As you might expect, non-privileged usr/bin/gnome-session If you launch a program without ap- users are only allowed to talk to their 04 3061 ? S 0:00 / pending an ampersand, you can press own processes, whereas the root user usr/bin/dbus-launch the keyboard shortcut Ctrl+Z to send it can send signals to any process. --exit-with-session /usr/bin/ to sleep. The shell confirms this action gnome-session as follows: @ejkilZk`fej 05 [...] Typing kill -l shows you the instructions [1]+ Stopped 5 that kill passes to a process. The follow- 06 3086 ? Ssl 0:02 xpdf ing are the most relevant ones for your gnome-panel --sm-client-id daily work: default1 If you now type bg (background), the SIGHUP: This tells a process to restart 07 3088 ? Ssl 0:02 process will continue to run in the back- immediately after terminating and is nautilus --no-default-window ground. The job ID is useful if you have often used to tell servers to parse mod- --sm-client-id default2 stopped several processes in a shell. The ified configuration files. bg %3 command tells the process with SIGTERM: This request to terminate Listing 2: Jobs the job ID 3 that it should start working allows the process to clean up. again. In a similar way, the fg (fore- SIGKILL: This signal forces a process 01 $ xpdf article.pdf & ground) program moves jobs into the to terminate come what may. But in 02 [1] 5622 foreground. Again, this program might 03 $ audacity & need more details in the form of a job Passwords 04 [2] 6559 ID following a percent character. ps displays the full set of command-line 05 [...] parameters in the COMMAND column. ;\kXZ_\[ Some programs, such as the wget 06 $ jobs The commands I just looked at move download manager, optionally accept 07 [1] Running xpdf article. processes to the background and option- passwords for authentication in the pdf & ally let them go on running. If you close shell. The password also appears as a command in the process list; theoreti- 08 [2]- Running audacity & the shell in which you launched the pro- gram, this also terminates all the active cally, any user on the system could sniff 09 [3]+ Running sleep 3600 & processes. sensitive data. 90 ISSUE 93 AUGUST 2008 Command Line: Process Control C@ELOLJ<I =`^li\(1K_\gjki\\ZfddXe[j_fnjpflgifZ\jji\cXk`fej_`gj`ek_\ =`^li\)1K_\gjZfddXe[j_fnjpfln_Xk`j_Xgg\e`e^fepfli j_\cc%8ccfk_\igifZ\jj\jXi\[\jZ\e[\[]ifdk_\]`ijkgifZ\jj C`elodXZ_`e\%8jpflZXej\\_\i\#pflZXeZfdY`e\gXiXd\k\ijXj cXleZ_\[fek_\jpjk\d`e`k % e\\[\[% some cases, it takes more to get rid of in the kill command line, which can be If you need more context, add the -l pa- the process. After waiting in vain for a tricky if the list is too long. rameter and pgrep will reveal the names. timeout, you have no alternative but The killall gives you a workaround – To discover the full command line, in- to reboot.