Monday, March 7, 2016

Week 21: Day 054 - Process Management


Everything that happens on a Linux server requires the creation of processes. This chapter will cover specifics on what these processes do. When a process begins, it uses multiple threads, and a thread is a bunch of sub-processes happening at the same time.

To immediately start a job in the background, prefix the command by starting it with the "&" symbol. To return it to the foreground do use the "fg" command. To terminate a process use the "kill" command.


In this exercise, you apply the commands that you just learned about to manage jobs that have been started from the current shell.

1. Open a root shell and type the following commands:

sleep 3600 &
dd if=/dev/zero of=/dev/null &
sleep 7200

2. Because you started the last command with no & after the command, you have to wait 2 hours before you get control to the shell back. Type Ctrl+Z to stop it.

I can no longer access the shell, ctrl-z will stop it.

3. Type jobs. You will see the three jobs that you just started. The first two of them have the Running state, and the last job currently is in the Stopped state.

There are two running jobs, and the one that I stopped.

4. Type bg 3 to continue running job 3 in the background. Notice that because it was started as the last job, you did not really have to add the number 3.

It shows three running jobs.

5. Type fg 1 to move job 1 to the foreground.

This moves "sleep 2500" to the foreground.

6. Type Ctrl+C to cancel job number 1 and use jobs to confirm that it is now gone.

I ended that job, and it no longer exists.

7. Use the same approach to cancel jobs 2 and 3 also.

They're all dead.

8. Open a second terminal on your server.

9. From that second terminal, type dd if=/dev/zero of=/dev/null &.

Don't know what this did.

10. Type exit to close the second terminal.

11. From the other terminal, start top. You will see that the dd job is still running. From top, use k to kill the dd job.

It asked me to type the number of the running job, and I chose the one that said "dd", now it's dead.

You cannot manage a single thread, however you can manage processes. When managing processes, it's easy to identify kernel processes because it's in "[ ]" brackets. Use the "ps aux | head" command to test take a look at an example of kernel processes. Now, "ps" retrieves running processes information, there are several modifiers for it. "aux" will show you a short summary of these processes. To look for the exact command use to start a given process, type "ps -ef". To see hierarchical relationship between parent and child processes types "ps fax". Note: hyphens are optional.


In this exercise, you learn how to work with ps, nice, kill, and related utilities to manage processes.

1. Open a root shell. From this shell, type dd if=/dev/zero of=/dev/null &. Repeat this command three times.

Created 4 different jobs of this.

2. Type ps aux | grep dd. This shows all lines of output that have the letters dd in them; you will see more than just the dd processes, but that should not really matter. The processes you just started are listed last.

This searches for dd in all of the.

3. Use the PID of one of the dd processes to adjust the niceness, using renice -n 5 <PID>. Notice that in top you cannot easily get an overview of processes and their current priority.

I got an error.

4. Type ps fax | grep -B5 dd. The -B5 option shows the matching lines, including the five lines before that. Becauseps fax shows hierarchical relationships between processes, you should also find the shell and its PID from which all the dd processes were started.

5. Find the PID of the shell from which the dd processes were started and type kill -9 <PID>, replacing <PID> with the PID of the shell you just found. You will see that your root shell is closed, and with it, all of the dd processes. Killing a parent process is an easy and convenient way to kill all of its child processes also.


Review Questions

1. Which command gives an overview of all current shell jobs?

jobs

2. How do you stop the current shell job to continue running it in the background?

Ctrl-Z then bg

3. Which keystroke combination can you use to cancel the current shell job?

Ctrl-C

4. A user is asking you to cancel one of the jobs he has started. You cannot access the shell that user currently is working from. What can you do to cancel his job anyway?

ps aux and kill <PID>

5. Which command would you use to show parent-child relationships between processes?
ps fax

6. Which command enables you to change the priority of PID 1234 to a higher priority?

ps -nn p 1234

7. On your system, 20 dd processes are currently running. What is the easiest way to stop all of them?

killall dd

8. Which command enables you to stop the command with the name mycommand?

pkill mycommand

9. Which command do you use from top to kill a process?

k

10. How would you start a command with a reasonably high priority without risking that no more resources are available for other processes?

nice -5

No comments:

Post a Comment