2. Investigating and Managing Processes
Upon completion of this unit, you should be able to:
Explain what a process is
Describe how to manage processes
Use job control tools
3. What is a Process?
A process is a set of instructions loaded into memory
Numeric Process ID (PID) used for identification
UID, GID and SELinux context determines filesystem access
• Normally inherited from the executing user
4. Listing Processes
View Process information with ps
Shows processes from the current terminal by default
a includes processes on all terminals
x includes processes not attached to terminals
u prints process owner information
f prints process parentage
o PROPERTY,... prints custom information:
• pid, comm, %cpu, %mem, state, tty, euser, ruser
Process states – running, sleeping, uninterruptable sleep, zombie.
5. Finding Processes
Most flexible: ps options | other commands
ps axo comm,tty | grep ttyS0
ps -ef
By predefined patterns: pgrep
$ pgrep -U root
$ pgrep -G student
● By exact program name: pidof
$ pidof bash
7. Signals
Most fundamental inter-process communication
Sent directly to processes, no user-interface required
Programs associate actions with each signal
Signals are specified by name or number when sent:
• Signal 15, TERM (default) - Terminate cleanly (stopping)
• Signal 9, KILL - Terminate immediately (kill)
• Signal 1, HUP - Re-read configuration files (reload)
• Kill –l list info for singal
• man 7 signal shows complete list
8. Sending Signals to Processes
By PID: kill [signal] pid ...
By Name: killall [signal] comm ...
By pattern: pkill [-signal] pattern
9. Scheduling Priority
Scheduling priority determines access to the CPU
Priority is affected by a process‘ nice value
Values range from -20 to 19 but default to 0
Lower nice value means higher CPU priority
Viewed with ps -Ao comm,nice
ex: firefox &
ps –Ao comm,nice |grep firefox
10. Altering Scheduling Priority
Nice values may be altered...
When starting a process:
$ nice -n 5 command
After starting:
$ renice 5 PID
renice 5 –p PID
Only root may decrease nice values
11. Interactive Process Management Tools
CLI: top
GUI: gnome-system-monitor
Capabilities
Display real-time process information
Allow sorting, killing and re-nicing
12. Job Control
Run a process in the background
Append an ampersand to the command line: firefox &
Temporarily halt a running program
Use Ctrl-z or send signal 17 (STOP)
Manage background or suspended jobs
List job numbers and names: jobs
Resume in the background: bg [%jobnum]
Resume in the foreground: fg [%jobnum]
Send a signal: kill [-SIGNAL] [%jobnum]
13. Scheduling a Process To Execute Later
One-time jobs use at, recurring jobs use crontab
Create at time crontab -e
List at -l crontab -l
Details at -c jobnum N/A
Remove at -d jobnum crontab -r
Edit N/A crontab -e
Non-redirected output is mailed to the user
root can modify jobs for other users
14. Using at
Setting an at job: at [options] time
Options:
-b run command only when the system load is low
-d job# delete an at job from queue
-f filename read job from a specified file
-l list jobs for that user (all jobs for root)
-m mail user quen job completes
-q queuename send the jobs to a queue (a to z and A to Z)
Time formats:
now, 17:00, +3 hours, +2 minutes, +2 days, +3 months, 19:15 3.12.10,
midnight, 4PM, 16:00 +3 days, mon, tomorrow …
Show the at jobs queue of user: atq or at –l
Deletes at jobs from the jobs queue: atrm job# [job#] …
15. Examples with at
Create an simple at job to run in 5 minutes later
$ at now+5 minutes
echo “I am running in an at job” > /tmp/test_cron
[Ctrl-D]
Create an at job which read commands from a file and run at
midnight
$ at –f /tmp/myjob.sh midnight
Using echo to run multiple commands with at
$ echo „cd /tmp; ls –a > /tmp/test_cron ‟ | at now+2 minutes
Listing all at jobs
$ at –l
$ atq
16. crontab commands
Create/edit a user crontab: crontab –e
Create a user crontab by reading from file:
crontab –e filename
Display user‟s crontab file: crontab –l
Delete user‟s crontab file: crontab –r
Edit a user‟s crontab file (for root only):
crontab –e –u username
17. Crontab File Format
Entry consists of five space-delimited fields followed by a
command line
01 * * * * root cd /tmp && ls -laht
One entry per line, no limit to line length
Fields are minute, hour, day of month, month, and day of week
Comment lines begin with #
See man 5 crontab for details
18. Examples of user crontabs
Run a command every hour from 8:00 to 18:00 everyday
0 8-18 * * * <command>
Run a command every 4 hours on the half hour (i.e 6:30, 10:30, 14:30, 16:30)
everyday
30 6-16/4 * * * <command>
Run a command every day, Monday to Friday at 01:00, and doesn‟t report to
syslog
-0 1 * * 1-5 <command>
Run the command every Monday and Tuesday at 12:00, 12:10, 12:20, 12:30
0,10,20,30 12 * * 1,2 <command>
Run a command every 10 minutes
*/10 * * * * <command>
echo “00 21 * * 7 root rm -f /tmp/*.log” >> /etc/crontab
crontab -e
00 8-17 * * 1-5 du -sh $HOME >> /tmp/diskreport
19. Grouping Commands
Two ways to group commands:
Compound: date; who | wc -l
• Commands run back-to-back
Subshell: (date; who | wc -l) >> /tmp/trace
• All output is sent to a single STDOUT and STDERR
20. Exit Status
Processes report success or failure with an exit status
0 for success, 1-255 for failure
$? stores the exit status of the most recent command
exit [num] terminates and sets status to num
Example:
$ ping -c1 -W1 localhost999 &> /dev/null
$ echo $?
2
21. Conditional Execution Operators
Commands can be run conditionally based on exit status
&& represents conditional AND THEN
|| represents conditional OR ELSE
Examples:
$ grep -q no_such_user /etc/passwd || echo 'No such user'
No such user
$ ping localhost &> /dev/null
> && echo “localhost is up"
> || echo ‘localhost is unreachable’
localhost is up
22. The test Command
Evaluates boolean statements for use in conditional execution
Returns 0 for true
Returns 1 for false
Examples in long form:
test "$A" = "$B" && echo "string" || echo "not equal"
$ test "$A" -eq "$B" && echo "Integers are equal“
Examples in shorthand notation:
$ [ "$A" = "$B" ] && echo "Strings are equal"
$ [ "$A" -eq "$B" ] && echo "Integers are equal"
23. File Tests
File tests:
-f tests to see if a file exists and is a regular file
-d tests to see if a file exists and is a directory
-x tests to see if a file exists and is executable
[ -f ~/lib/functions ] && source ~/lib/functions
24. File Tests (cont’)
Options Mean
-d file True if the file is a directory.
-e file True if the file exists.
-f file True if the file exists and is a regular file.
-h file True if the file is a symbolic link.
-L file True if the file is a symbolic link.
-r file True if the file exists and is readable by you.
-s file True if the file exists and is not empty.
-w file True if the file exists and is writable by you.
-x file True if the file exists and is executable by you.
-O file True if the file is effectively owned by you.
-G file True if the file is effectively owned by your group.
25. Scripting: if Statements
Execute instructions based on the exit status of a command
if ping -c1 -w2 station1 &> /dev/null; then
echo 'Station1 is UP'
elif grep "station1" ~/maintenance.txt &> /dev/null; then
echo 'Station1 is undergoing maintenance'
else
echo 'Station1 is unexpectedly DOWN!'
exit 1
fi