Activity Jobs and Processes
Activity Jobs and Processes
Activity Jobs and Processes
Objectives:
1. Define how jobs and processes works in Linux.
2. List and identify the processes running in the Linux operating system.
3. Select the most important processes in Linux.
4. Apply the Linux commands as well as Bash commands to monitor, and manage Linux processes
and jobs.
Concepts
Job
Every command you give is a job that is executed. A job can be suspended, placed in the
background, moved back to the foreground or terminated.
Most of the commands that we encountered such as pwd is executed fast but when you are
for example dealing with copying large amount of data, it will take a little while finish the job thus
occupying your terminal (your terminal will be unaccessible unless you put the job in the
background). In these cases, the job can be placed in the background, allowing you to execute other
commands in the meantime.
Job Control
• Most shells offer job control
◦ The ability to stop, restart and background a running process
• The shell lets you put '&' on the end of a command line to start it in the background
• You can hit ' Ctrl + Z ' to suspend a running foreground job.
• Suspended and backgrounded jobs are given numbers by the shell.
• These numbers can be given to shell job-control built-in commands
• These are jobs, fg, and bg
jobs
• The jobs built-in prints a listing of active jobs and their job numbers
$jobs
[1] - Stopped vim index.html
[2] Running netscape &
[3] + Stopped man ls
bg
• Re-starts a suspended job, running it in the background
• bg %1 will background job number 1
• bg with no arguments will operate on the current job
Example: after running gv and suspending it with Ctrl+Z, use bg to start it running again in
the background.
Process
In our previous lessons last semester we were able to talk or communicate with the operating
system through the shell. Now, we need to study the processes that runs in Linux in more detail.
Process Properties
• A process has a user id (uid) and a group id (gid) which together specify what permissions
it has
• A process has a parent process id (ppid) – the pid of the process which created it
◦ The kernel starts an init process with pid 1 at boot-up
◦ Every other process is a descendant of pid 1
• Each process has its own working directory, initially inherited from its parent process
• There is an environment for each process - a collection of named environment variables and
their associated values.
◦ A process's environment is normally inherited from its parent process
bash
bash vi
apache
apache
apache
apache
• (Apache starts many child processes so that they can serve HTTP requests at the same time)
ps Options
Option Description
a Show processes owned by other users
f Display process ancestors in a tree-like format
u Use the 'user' output format, showing user names and process start times
w Use a wider output format. Normally each line of output is truncated; each use
the w option makes the window wider
x Include processes which have no controlling terminal
-e Show information in all processes
-l Use a long output format
-f Use a full output format
-C cmd Show only processes named cmd
-U user Show only processes owned by user
pstree Options
Option Description
-a Display commands' arguments
-c Don't compact identical subtrees
-G Attempt to use terminal-specific line-drawing characters
-h Highlight the ancestors of the current process
-n Sort processes numerically by pid, rather than alphanumeric by name
-p Include pids in the output
Examples:
$ cp file backup/file
Ctrl + Z
$ bg
• Using Crl + Z, we are able to put the job in the background and make the terminal available
for other tasks
2. Alternately you can do this
$ cp file backup/file &
$ jobs
$ bg
$ ps
• Immediately following example number 4, we execute the jobs command to display the
processes that you (the user) have run.
• Alternately we can use the ps command.
Ctrl + Z
$ fg %1
• We consulted the manual page for the sleep command using the man command
• Executing Ctrl + Z suspends the less pager for the sleep manual
• To foreground the less pager we give the shell a fg %1 command
7. Show all the processes running on your machine in a hierarchical format starting from upper
parent process, init
skx@taltos:~$ pstree
init-+-apache2---10*[apache2]
|-atd
|-clamd
|-cron
|-events/0
|-exim4---exim4
|-freshclam
|-getty
|-gpg-agent
|-khelper
|-ksoftirqd/0
|-kthread-+-aio/0
| |-ata/0
| |-ata_aux
| |-kblockd/0
| |-khubd
| |-kjournald
| |-kmirrord
| |-kseriod
| |-kswapd0
| |-2*[pdflush]
| |-xenbus
| `-xenwatch
|-memcached
|-migration/0
|-monit---{monit}
|-munin-node
|-mysqld_safe-+-logger
| `-mysqld---16*[{mysqld}]
|-pdnsd---4*[{pdnsd}]
|-python
|-qpsmtpd-forkser
|-roundup-server---roundup-server
|-screen---bash---irssi
|-ssh-agent
|-sshd-+-sshd---sshd---bash
| `-sshd---sshd---bash---pstree
|-syslog-ng
`-watchdog/0
Exercises:
1.
Execute the command:
$ sleep 100
Ctrl + Z
Specify the output. What does the sleep command do? How does Ctrl + Z affects the previous
command - sleep command?
2. How would you move the stoped job in number 1 to the background? (Specify the exact
command)
$ sleep 140
Ctrl + Z
$ sleep 130
Ctrl + Z
$ jobs
4. How would you move the second active job to the background? (Specify the exact command)
5.How would you run xclock in the background using &. (Specify the exact command)
6. Can command(s) will you use to bring xclock into the foreground? (Specify the exact command)
7. Run
$ gedit
This should run gedit in the foreground. You will not be able to use the shell.
Try suspending gedit with Ctrl + Z.
What happened when you executed Ctrl + Z?
How would you put gedit into the background?
8. Use the top command to show the processes running on your machine. (Specify at least 5 lines of
ouput).
9. Make top sort by memory usage, so that the most memroy-hungry processes appear on top.