Job Control and Shell Variables Tutorial

1. ps command and process IDs

Experiment using the ps (process status) command. Without any extra parameters this just gives information about the current terminal and what this shell is running. Try using various flags e.g:

ps ax
ps lax
ps axf

Then read the manpage ps(1) You can do this using the command:

man 1 ps

Use the bash command to run a child shell and use the ps command from within this. Identify the PID and PPID (Process ID and Parent PID) of the child shell. Use the exit command and repeat.

Experiment with the top command. This displays the most CPU using programs by default. Use the h key to get help within top and from the help display try and work out how to change the sort order, so that you are displaying the most memory using programs instead of the most CPU using ones. To get out of top use the q key

.

2. Running commands in background

Experiment with the sleep command to cause a 5 second pause,

sleep 5

Use a similar command in background by giving the shell an ampersand parameter (&) at the end of the command line:

sleep 15 &

What is the difference ? Try running another command while sleep runs in background. Is the ampersand command parameter sent to the sleep program after the number of seconds for it to sleep, or does this cause the shell to behave in a different way ? Try runing the ps command to obtain information about the process ID and parent process ID of the sleep process, while the sleep process is running in background.

3. Killing processes

Read the kill(1) manpage. kill enables you to send various signals to a process identified by process ID (the PID column in the ps listing). The SIGKILL or 9 signal:

kill -9 ProcessID 

Can't be caught so should be a sure kill (i.e. if you own the victim process or are logged in as root).

Using some sleep processes in background, experiment by killing these.

Other signals can be caught by programs which include special signal handling code. See if you can use Google to find an explanation of what a SIGHUP or 1 signal is used for within the Sendmail (Mail Transfer Agent or mail server program). Why might it more useful for this to be done using a signal than by using a graphical user interface control ?

Scan the following manpages: signal(7), kill(1).

4. Stopping, backgrounding and foregrounding processes and jobs

Place a foreground sleep process ( e.g. sleep 60 ) into a stopped state using the <CTRL> and <z> keys together. Identify the jobnumber of this process using the jobs command. Put the stopped job into background using the bg jobnumber command. Then put this job into foreground again using the fg jobnumber command.

Start 2 background sleep processes and use the jobs command to identify these. Use jobs -l to obtain the process IDs. Kill the first one.

Another student is trying to learn how to use vi, but he thinks pressing control and Z keys together exits the program. Then when he tries editing the same file vi outputs with an error message. What does this error message say and mean, and how should your friend resolve the situation so vi doesn't output this message and he can use vi correctly on the file he started working with ?

5. Environment and other Variables

Have a look at the environment variables available using the:

env

command. You may need to pipe the output of env through more. By using the

echo $HOME

command, obtain the value of the login or home directory.

Experiment by setting the prompt to a different value in a child shell, with the command:

PS1=myprompt

What is changed and what happens when you exit the subshell ? Page the file /etc/skel/.bashrc and see if you can identify where the usual shell prompt is set - you don't need to understand how it is set. Try changing the prompt to `pwd` (Note use of grave quotes. On UK keyboards, this is usually the key to the left of the numeral 1 above the q key.) Does this prompt change when you change to a different directory ? Try setting the prompt in this way starting in different directories and try and find out how what you saw happened. Also try changing the prompt to $(pwd) which should result in the same effect. If your keyboard doesn't have grave quotes easily accessible, use $(command) as a replacement for `command`.

Use exit to come out of the child shell.

6. Bash builtin commands

Read parts of bash-builtins(1) which tell you how some of these commands directly built into the bash shell work. Try to read sections describing cd, jobs, fg and bg commands in detail. These bash shell builtin commands are listed in alphabetical order. You might also usefully learn how to search for particular strings within the less pager to skip over other entries, as this is a long document.

The difference between a builtin and an external command is that when you enter an external command the shell searches all the directories included within the PATH environment variable to find another program to run, but if the command is builtin, the code needed to run this command is part of the bash program itself.

Try using the which command e.g. which ps to find out where the programs implementing the following commands are located:

ps, man, adduser, cd

Why does which not report the location of a program implementing the cd command ?

Look at the manpage for which(1). Then by using the command

echo $?

Identify the return codes of which directly after use of the commands which cd and which ls .

7. Setting the PATH environment variable

Change the PATH environment variable so that it can run an external command from the current working directory, in addition to all the other directories already in this variable. Using a text editor create a shell program called hello containing the following 2 lines:

#!/bin/bash
echo hello bash shell world!

Use the command chmod +x hello to make it executable. Run it using the command hello without a ./ in front.

Why is the current working directory not usually included within the PATH ? What kind of attack becomes possible if it is included ?