1
00:00:00,270 --> 00:00:01,350
In this lesson,

2
00:00:01,350 --> 00:00:04,046
we're going to discuss how you can troubleshoot process issues

3
00:00:04,046 --> 00:00:06,600
within the Linux operating system.

4
00:00:06,600 --> 00:00:09,480
While running processes within the Linux operating system,

5
00:00:09,480 --> 00:00:12,330
your users may experience technical issues.

6
00:00:12,330 --> 00:00:14,970
To provide uninterrupted services to your users,

7
00:00:14,970 --> 00:00:16,703
you may need to be able to solve problems

8
00:00:16,703 --> 00:00:19,320
that are going to come up during operations.

9
00:00:19,320 --> 00:00:22,075
As an administrator managing multiple systems on a network,

10
00:00:22,075 --> 00:00:23,940
you're going to have installed and managed

11
00:00:23,940 --> 00:00:25,770
various services and packages

12
00:00:25,770 --> 00:00:28,110
that are required for your users to do their job.

13
00:00:28,110 --> 00:00:30,630
However, there's sometimes going to be instances

14
00:00:30,630 --> 00:00:32,280
where applications and services

15
00:00:32,280 --> 00:00:34,860
do not function as they're supposed to.

16
00:00:34,860 --> 00:00:36,390
As a system administrator,

17
00:00:36,390 --> 00:00:38,160
you're going to be expected to determine

18
00:00:38,160 --> 00:00:40,110
and resolve those problems.

19
00:00:40,110 --> 00:00:42,300
Now there are many different issues that could affect

20
00:00:42,300 --> 00:00:45,630
or be caused by a process of running instance of software

21
00:00:45,630 --> 00:00:47,340
or something else like that.

22
00:00:47,340 --> 00:00:50,790
These issues can cause instability when processes hang.

23
00:00:50,790 --> 00:00:52,200
They can consume resources

24
00:00:52,200 --> 00:00:54,180
that should be allocated to other processes

25
00:00:54,180 --> 00:00:57,060
and it can create a general sluggishness to your system

26
00:00:57,060 --> 00:00:58,890
and other issues like that.

27
00:00:58,890 --> 00:01:00,720
Processes move through a life cycle

28
00:01:00,720 --> 00:01:03,630
from creation until their eventual termination.

29
00:01:03,630 --> 00:01:06,180
Now there are five different states that a process can be in

30
00:01:06,180 --> 00:01:07,710
during this life cycle.

31
00:01:07,710 --> 00:01:10,860
Each one defines how the system or other applications

32
00:01:10,860 --> 00:01:13,230
can interact with that given process.

33
00:01:13,230 --> 00:01:14,820
Knowing a process's state

34
00:01:14,820 --> 00:01:17,130
can help you determine what to do with that process

35
00:01:17,130 --> 00:01:19,350
or it can help you diagnose problems

36
00:01:19,350 --> 00:01:21,210
concerning that process.

37
00:01:21,210 --> 00:01:24,180
The running state means the process is currently executing

38
00:01:24,180 --> 00:01:27,030
in the user space or the kernel space.

39
00:01:27,030 --> 00:01:29,730
The interruptible sleep state means the process

40
00:01:29,730 --> 00:01:31,950
relinquishes access to the CPU

41
00:01:31,950 --> 00:01:34,920
and is waiting to be reactivated by the scheduler.

42
00:01:34,920 --> 00:01:36,660
The interruptible sleep state

43
00:01:36,660 --> 00:01:38,550
means that the process will only wake up

44
00:01:38,550 --> 00:01:40,560
when its resources that it's waiting for

45
00:01:40,560 --> 00:01:42,000
become available to it.

46
00:01:42,000 --> 00:01:45,060
Otherwise, it's going to stay in a sleep state.

47
00:01:45,060 --> 00:01:47,040
Then, we have the zombie state

48
00:01:47,040 --> 00:01:49,058
and this indicates that a process was terminated

49
00:01:49,058 --> 00:01:52,620
but it has not yet been released by its parent process

50
00:01:52,620 --> 00:01:55,020
so it cannot accept a kill signal.

51
00:01:55,020 --> 00:01:56,850
The final state is a stop state

52
00:01:56,850 --> 00:01:58,662
and this state indicates that a process

53
00:01:58,662 --> 00:02:02,220
was stopped by a debugger or through a kill signal.

54
00:02:02,220 --> 00:02:04,170
Now every process on your system

55
00:02:04,170 --> 00:02:07,680
has a unique process ID known as a PID

56
00:02:07,680 --> 00:02:10,169
and it gets this when it started up by the system,

57
00:02:10,169 --> 00:02:11,756
and that way, users and the system

58
00:02:11,756 --> 00:02:14,190
can identify that process.

59
00:02:14,190 --> 00:02:17,370
The PID is a non-negative integer that increases

60
00:02:17,370 --> 00:02:20,490
for each new process that's being started on the system.

61
00:02:20,490 --> 00:02:23,490
Now the init daemon always has a PID of one

62
00:02:23,490 --> 00:02:26,370
because it's the first process to start up on the system

63
00:02:26,370 --> 00:02:28,740
and it becomes the parent for all other processes

64
00:02:28,740 --> 00:02:30,390
on that system.

65
00:02:30,390 --> 00:02:31,770
After this one is started,

66
00:02:31,770 --> 00:02:34,140
then processes are going to be able to be started up

67
00:02:34,140 --> 00:02:36,150
by the system or the user

68
00:02:36,150 --> 00:02:37,560
by that init daemon,

69
00:02:37,560 --> 00:02:39,840
and they're going to be assigned a higher available number

70
00:02:39,840 --> 00:02:42,090
because they're going to be something higher than one

71
00:02:42,090 --> 00:02:44,310
because they're started by the init daemon.

72
00:02:44,310 --> 00:02:45,990
When you're troubleshooting something,

73
00:02:45,990 --> 00:02:48,540
you need to know what the process's PID is

74
00:02:48,540 --> 00:02:50,220
or that identification number

75
00:02:50,220 --> 00:02:52,202
because this helps you understand how to terminate it,

76
00:02:52,202 --> 00:02:55,320
change its priority or perform other management tasks

77
00:02:55,320 --> 00:02:57,330
on that given process.

78
00:02:57,330 --> 00:02:58,586
Now if you want to find that,

79
00:02:58,586 --> 00:03:01,017
you can use the pgrep command.

80
00:03:01,017 --> 00:03:02,550
The pgrep command

81
00:03:02,550 --> 00:03:04,620
looks through the currently running processes

82
00:03:04,620 --> 00:03:05,997
and lists out the process IDs

83
00:03:05,997 --> 00:03:07,898
which match the selection criteria

84
00:03:07,898 --> 00:03:09,930
to your standard output,

85
00:03:09,930 --> 00:03:11,520
similar to how grep uses it

86
00:03:11,520 --> 00:03:14,610
to find matching patterns inside of a text file.

87
00:03:14,610 --> 00:03:16,020
If you use pgrep,

88
00:03:16,020 --> 00:03:17,790
you're going to be able to identify a process

89
00:03:17,790 --> 00:03:19,401
based on multiple different factors

90
00:03:19,401 --> 00:03:21,633
even if you don't know its exact PID.

91
00:03:22,560 --> 00:03:24,750
This will help you identify the PID

92
00:03:24,750 --> 00:03:27,840
and identifying the PID is typically your first step

93
00:03:27,840 --> 00:03:29,520
in managing a process.

94
00:03:29,520 --> 00:03:31,110
To run the pgrep command,

95
00:03:31,110 --> 00:03:33,074
simply type in pgrep, the options,

96
00:03:33,074 --> 00:03:35,094
and the pattern you're searching for.

97
00:03:35,094 --> 00:03:37,830
Another way to find the process idea of a running program

98
00:03:37,830 --> 00:03:39,750
is to use the pidof command,

99
00:03:39,750 --> 00:03:41,934
pidof, spelled P-I-D-O-F,

100
00:03:41,934 --> 00:03:44,340
is going to allow you to find the process ID

101
00:03:44,340 --> 00:03:46,073
of the named program that you give it.

102
00:03:46,073 --> 00:03:48,600
To use it, you're simply going to type pidof,

103
00:03:48,600 --> 00:03:50,010
and then the name of the program.

104
00:03:50,010 --> 00:03:51,900
For example, if you are running something

105
00:03:51,900 --> 00:03:53,400
like the ls command,

106
00:03:53,400 --> 00:03:56,340
you could run pidof space ls, Enter,

107
00:03:56,340 --> 00:03:57,900
and then find what was the PID

108
00:03:57,900 --> 00:04:00,390
associated with the ls command.

109
00:04:00,390 --> 00:04:03,420
Another command you can use is known as ps.

110
00:04:03,420 --> 00:04:06,330
The ps command invokes the process table

111
00:04:06,330 --> 00:04:07,740
which is a record that summarizes

112
00:04:07,740 --> 00:04:10,710
the current running processes on a given system.

113
00:04:10,710 --> 00:04:12,900
When the command is run without any options,

114
00:04:12,900 --> 00:04:14,412
it's going to display all the processes

115
00:04:14,412 --> 00:04:17,519
that are run by the current shell with the details.

116
00:04:17,519 --> 00:04:19,529
This includes things like the PID,

117
00:04:19,529 --> 00:04:21,241
the terminal associated with that process,

118
00:04:21,241 --> 00:04:23,490
the accumulated CPU time,

119
00:04:23,490 --> 00:04:26,190
and the command that started that exact process.

120
00:04:26,190 --> 00:04:28,076
However, different options may be used

121
00:04:28,076 --> 00:04:29,580
along with that command

122
00:04:29,580 --> 00:04:33,000
to filter out the displayed fields or processes too.

123
00:04:33,000 --> 00:04:34,590
The a option, for example,

124
00:04:34,590 --> 00:04:36,730
will list all user-triggered processes.

125
00:04:36,730 --> 00:04:39,360
The -e option is going to be used to list

126
00:04:39,360 --> 00:04:40,830
all of the processes.

127
00:04:40,830 --> 00:04:43,200
The -l option will list the processes

128
00:04:43,200 --> 00:04:45,180
using a long-listing format.

129
00:04:45,180 --> 00:04:47,927
And the u option is going to be used to list out processes

130
00:04:47,927 --> 00:04:50,490
along with the username and the start time.

131
00:04:50,490 --> 00:04:53,610
The r option is going to be used to exclude processes

132
00:04:53,610 --> 00:04:55,200
that are not running currently.

133
00:04:55,200 --> 00:04:57,690
And the x option is used to include processes

134
00:04:57,690 --> 00:04:59,040
without a terminal.

135
00:04:59,040 --> 00:05:01,568
The T option is going to be used to exclude processes

136
00:05:01,568 --> 00:05:03,330
that were started by any terminal

137
00:05:03,330 --> 00:05:04,770
other than the current one.

138
00:05:04,770 --> 00:05:06,976
And the dash -U option with the username

139
00:05:06,976 --> 00:05:09,240
is going to be used to display all the processes

140
00:05:09,240 --> 00:05:11,520
based on a specified user.

141
00:05:11,520 --> 00:05:15,270
The -p and PID option will display only the process

142
00:05:15,270 --> 00:05:17,610
associated with the specific PID.

143
00:05:17,610 --> 00:05:20,769
The -C command option is going to display all the processes

144
00:05:20,769 --> 00:05:22,710
by their command name

145
00:05:22,710 --> 00:05:25,413
while the --tty internal number option

146
00:05:25,413 --> 00:05:27,634
is going to be used to display all the processes

147
00:05:27,634 --> 00:05:30,360
running on a specified terminal.

148
00:05:30,360 --> 00:05:32,820
Like ps, the top command is going to list

149
00:05:32,820 --> 00:05:35,490
all the processes running on a Linux system.

150
00:05:35,490 --> 00:05:37,650
It acts as a process management tool

151
00:05:37,650 --> 00:05:39,840
by enabling you to prioritize, sort,

152
00:05:39,840 --> 00:05:42,450
or terminate processes interactively.

153
00:05:42,450 --> 00:05:44,760
It's going to display a dynamic process status

154
00:05:44,760 --> 00:05:46,860
and reflect real-time changes.

155
00:05:46,860 --> 00:05:49,500
Different keystrokes within this tool are going to execute

156
00:05:49,500 --> 00:05:51,550
various process management actions as well.

157
00:05:51,550 --> 00:05:54,000
Some of the frequently used command keys

158
00:05:54,000 --> 00:05:55,320
include the Enter key.

159
00:05:55,320 --> 00:05:56,190
And when you press that,

160
00:05:56,190 --> 00:05:59,040
it's going to refresh the status of all your processes.

161
00:05:59,040 --> 00:06:00,810
If you press Shift + N,

162
00:06:00,810 --> 00:06:02,220
you're going to sort the processes

163
00:06:02,220 --> 00:06:04,382
in decreasing order of their PID.

164
00:06:04,382 --> 00:06:05,886
If you press the M key,

165
00:06:05,886 --> 00:06:09,210
it's going to sort the processes by their memory usage.

166
00:06:09,210 --> 00:06:10,379
If you press the P key,

167
00:06:10,379 --> 00:06:13,314
it's going to sort the processes by CPU usage.

168
00:06:13,314 --> 00:06:16,620
If you press U, it's going to display the processes

169
00:06:16,620 --> 00:06:19,260
belonging to the user specified at the prompt.

170
00:06:19,260 --> 00:06:20,490
If you press K,

171
00:06:20,490 --> 00:06:22,050
it's going to terminate the process

172
00:06:22,050 --> 00:06:24,420
for which you specify the PID for.

173
00:06:24,420 --> 00:06:25,620
If you press R,

174
00:06:25,620 --> 00:06:27,420
it's going to alter the scheduling priority

175
00:06:27,420 --> 00:06:31,290
of that process and you have to specify this by its PID.

176
00:06:31,290 --> 00:06:32,370
And if you press Q,

177
00:06:32,370 --> 00:06:34,530
it's going to exit the process list.

178
00:06:34,530 --> 00:06:36,946
To run the top command, simply type in top,

179
00:06:36,946 --> 00:06:39,780
and your options at the command prompt.

180
00:06:39,780 --> 00:06:40,980
Now in addition to top,

181
00:06:40,980 --> 00:06:43,440
there's a newer version called htop,

182
00:06:43,440 --> 00:06:46,230
htop is also an interactive, system monitor,

183
00:06:46,230 --> 00:06:48,074
process viewer and process manager

184
00:06:48,074 --> 00:06:50,280
but it really does a lot more

185
00:06:50,280 --> 00:06:53,219
and in a lot easier way of using it than does top.

186
00:06:53,219 --> 00:06:56,726
This is because htop was developed back in 2004

187
00:06:56,726 --> 00:07:00,630
where top was developed all the way back in 1984.

188
00:07:00,630 --> 00:07:02,490
Now the one drawback is that htop

189
00:07:02,490 --> 00:07:05,700
is not installed by default on all Linux distributions.

190
00:07:05,700 --> 00:07:07,710
So if you want to use htop

191
00:07:07,710 --> 00:07:09,904
and it's not already installed on your distribution,

192
00:07:09,904 --> 00:07:12,270
you're going to have to install it using a package manager

193
00:07:12,270 --> 00:07:14,310
or compiling it from the source code

194
00:07:14,310 --> 00:07:15,886
and installing it on your Linux server.

195
00:07:15,886 --> 00:07:17,580
Now really, when it comes down

196
00:07:17,580 --> 00:07:20,250
to some of the big differences between top and htop,

197
00:07:20,250 --> 00:07:22,770
it really comes down to how it presents the data.

198
00:07:22,770 --> 00:07:23,670
When you're using top,

199
00:07:23,670 --> 00:07:26,040
it's a very basic black and white interface

200
00:07:26,040 --> 00:07:27,360
and it's text only.

201
00:07:27,360 --> 00:07:28,832
But when you start using htop,

202
00:07:28,832 --> 00:07:31,110
you're going to get a more colorful interface

203
00:07:31,110 --> 00:07:32,910
with some text-based graphics

204
00:07:32,910 --> 00:07:35,040
that'll depict exactly what's going on.

205
00:07:35,040 --> 00:07:38,227
For example, at the top of the screen in an htop display,

206
00:07:38,227 --> 00:07:41,273
you're going to see a graphical representation of the CPU,

207
00:07:41,273 --> 00:07:43,050
memory, and swap space

208
00:07:43,050 --> 00:07:45,720
and how much is being used and how much is free.

209
00:07:45,720 --> 00:07:47,958
Then, it will go on to show you all the different processes,

210
00:07:47,958 --> 00:07:49,782
the users that are running those processes,

211
00:07:49,782 --> 00:07:52,170
and all the information you're going to want to know

212
00:07:52,170 --> 00:07:54,357
about those processes on your given system,

213
00:07:54,357 --> 00:07:56,202
and it does this in a color-coded way,

214
00:07:56,202 --> 00:07:57,870
so it becomes really easy

215
00:07:57,870 --> 00:08:00,060
to quickly pick out the information you're looking for

216
00:08:00,060 --> 00:08:02,910
when you're using htop instead of top.

217
00:08:02,910 --> 00:08:06,543
Another command we want to talk about is systemd-analyze.

218
00:08:06,543 --> 00:08:08,970
Now the systemd-analyze command

219
00:08:08,970 --> 00:08:10,860
is used to retrieve performance statistics

220
00:08:10,860 --> 00:08:12,390
for boot operations.

221
00:08:12,390 --> 00:08:14,090
The command takes one or more sub commands

222
00:08:14,090 --> 00:08:17,100
that determine what type of information is going to be printed

223
00:08:17,100 --> 00:08:19,170
and how it's going to be printed.

224
00:08:19,170 --> 00:08:21,613
Now for process management and troubleshooting,

225
00:08:21,613 --> 00:08:24,439
blame is going to be the most relevant sub command.

226
00:08:24,439 --> 00:08:27,990
This sub command will print a list of all the systemd units

227
00:08:27,990 --> 00:08:29,580
that were executed at boot

228
00:08:29,580 --> 00:08:32,033
along with the time it took for each unit to execute.

229
00:08:32,033 --> 00:08:35,520
You can then use the systemd-analyze blame function

230
00:08:35,520 --> 00:08:37,679
to identify services and other units

231
00:08:37,679 --> 00:08:40,980
that make the system really slow during the bootup process

232
00:08:40,980 --> 00:08:43,076
To run the systemd-analyze command,

233
00:08:43,076 --> 00:08:46,290
simply type in systemd-analyze,

234
00:08:46,290 --> 00:08:48,540
the options and the sub command.

235
00:08:48,540 --> 00:08:51,570
The list of open files or lsof command

236
00:08:51,570 --> 00:08:53,337
is going to print out a list of all the files

237
00:08:53,337 --> 00:08:54,810
that are currently open

238
00:08:54,810 --> 00:08:57,276
to all your active processes on the system.

239
00:08:57,276 --> 00:08:59,420
This can include everything from a text file

240
00:08:59,420 --> 00:09:01,020
to a device file.

241
00:09:01,020 --> 00:09:03,240
Basically, any object on that system

242
00:09:03,240 --> 00:09:04,847
can be parsed as a file.

243
00:09:04,847 --> 00:09:06,674
Now sometimes, you're going to be prevented

244
00:09:06,674 --> 00:09:08,340
from modifying a file

245
00:09:08,340 --> 00:09:10,650
if it's already open by another process.

246
00:09:10,650 --> 00:09:12,882
So you can use the lsof command

247
00:09:12,882 --> 00:09:14,793
to identify the offending process

248
00:09:14,793 --> 00:09:17,711
that you need to terminate so you can take over that file.

249
00:09:17,711 --> 00:09:20,160
You can also use the lsof command

250
00:09:20,160 --> 00:09:22,317
to analyze how a process uses files

251
00:09:22,317 --> 00:09:23,730
which can be really helpful

252
00:09:23,730 --> 00:09:25,537
if you're identifying malicious processes

253
00:09:25,537 --> 00:09:28,033
or processes that have unwanted side effects

254
00:09:28,033 --> 00:09:29,591
in a given system.

255
00:09:29,591 --> 00:09:32,640
The lsof command or list of open files

256
00:09:32,640 --> 00:09:35,010
will print each file that's opened by a process

257
00:09:35,010 --> 00:09:37,020
to its own line on the screen.

258
00:09:37,020 --> 00:09:38,150
It's going to print information

259
00:09:38,150 --> 00:09:40,710
such as the name of the command or process,

260
00:09:40,710 --> 00:09:42,668
the PID, the invoking user,

261
00:09:42,668 --> 00:09:44,001
the file descriptor,

262
00:09:44,001 --> 00:09:46,980
and what kind of permissions the file has been opened with,

263
00:09:46,980 --> 00:09:49,740
as well as the type of file and the name of the file.

264
00:09:49,740 --> 00:09:50,910
To run this command,

265
00:09:50,910 --> 00:09:53,010
simply type in lsof

266
00:09:53,010 --> 00:09:55,489
and the options at the command prompt.

267
00:09:55,489 --> 00:09:58,470
Now another thing that the operating system does for us

268
00:09:58,470 --> 00:10:00,630
is it schedules out those processes

269
00:10:00,630 --> 00:10:03,180
to give each one some CPU time.

270
00:10:03,180 --> 00:10:05,070
This is known as the scheduler.

271
00:10:05,070 --> 00:10:07,320
Now the scheduler is usually really effective

272
00:10:07,320 --> 00:10:09,090
at assigning CPU time

273
00:10:09,090 --> 00:10:10,625
but not every decision it makes

274
00:10:10,625 --> 00:10:12,870
is going to be considered optimal.

275
00:10:12,870 --> 00:10:15,920
In some cases, you may need to manually tell your CPU

276
00:10:15,920 --> 00:10:18,893
to prioritize certain processes over others.

277
00:10:18,893 --> 00:10:20,877
Now processes are going to be prioritized

278
00:10:20,877 --> 00:10:24,030
based on a given number that's associated with it,

279
00:10:24,030 --> 00:10:26,790
anywhere from -20 to 19.

280
00:10:26,790 --> 00:10:30,466
This value is known as the nice value or the niceness value.

281
00:10:30,466 --> 00:10:32,220
If you have a lower number,

282
00:10:32,220 --> 00:10:34,560
it means you have a higher priority.

283
00:10:34,560 --> 00:10:36,630
For example, if you have a process

284
00:10:36,630 --> 00:10:38,940
with a nice value of -18,

285
00:10:38,940 --> 00:10:40,595
it's going to get more CPU time

286
00:10:40,595 --> 00:10:43,109
than a process with a nice value of 15

287
00:10:43,109 --> 00:10:47,010
because -18 is much smaller than 15.

288
00:10:47,010 --> 00:10:49,260
Now a process is going to inherit a nice value

289
00:10:49,260 --> 00:10:50,700
from its parent as well.

290
00:10:50,700 --> 00:10:54,300
And by default, the value it's always going to get is zero.

291
00:10:54,300 --> 00:10:56,580
The nice command will enable you to run a command

292
00:10:56,580 --> 00:10:58,860
with different nice values than the default one

293
00:10:58,860 --> 00:11:00,060
it's going to be getting.

294
00:11:00,060 --> 00:11:02,730
To do this, you're going to use the -n option,

295
00:11:02,730 --> 00:11:04,410
and then specify a number.

296
00:11:04,410 --> 00:11:06,420
If you don't specify an integer,

297
00:11:06,420 --> 00:11:09,570
it's going to go ahead and assume you wanted a 10 increment.

298
00:11:09,570 --> 00:11:11,354
Now by running nice without any options,

299
00:11:11,354 --> 00:11:14,049
you're going to see a default nice value.

300
00:11:14,049 --> 00:11:17,400
You must be the root user or have root user authority

301
00:11:17,400 --> 00:11:19,800
to run this command at a higher priority

302
00:11:19,800 --> 00:11:22,320
if you want to be able to change people's values.

303
00:11:22,320 --> 00:11:25,410
To use this, type in nice -n,

304
00:11:25,410 --> 00:11:27,240
the nice value increment you want

305
00:11:27,240 --> 00:11:29,430
and the command you want to change.

306
00:11:29,430 --> 00:11:31,470
Now the nice command is usually going to be used

307
00:11:31,470 --> 00:11:34,290
to start a new process at a given priority

308
00:11:34,290 --> 00:11:36,060
but there's another command out there

309
00:11:36,060 --> 00:11:38,490
if you want to change an existing process

310
00:11:38,490 --> 00:11:39,780
and its nice value,

311
00:11:39,780 --> 00:11:41,910
and this is known as renice,

312
00:11:41,910 --> 00:11:43,680
renice is the command to enable you

313
00:11:43,680 --> 00:11:45,210
to alter the scheduling priority

314
00:11:45,210 --> 00:11:47,460
of an already running process.

315
00:11:47,460 --> 00:11:49,320
You're going to use the renice command

316
00:11:49,320 --> 00:11:52,800
with the -n option to specify your new nice value

317
00:11:52,800 --> 00:11:54,870
that you want that process to have.

318
00:11:54,870 --> 00:11:57,060
Now if you want to, you can also renice

319
00:11:57,060 --> 00:12:00,012
not just a single process but an entire process group,

320
00:12:00,012 --> 00:12:02,910
and you do this using the -g option.

321
00:12:02,910 --> 00:12:05,783
This will cause all the processes in a given process group

322
00:12:05,783 --> 00:12:07,447
to have their nice values altered

323
00:12:07,447 --> 00:12:09,600
based on what you told it.

324
00:12:09,600 --> 00:12:11,143
Also, if you want to do a renice

325
00:12:11,143 --> 00:12:12,887
against everything with a user,

326
00:12:12,887 --> 00:12:16,200
you can do that too by using the -u option.

327
00:12:16,200 --> 00:12:18,900
This will alter the nice values of all the processes

328
00:12:18,900 --> 00:12:20,850
that are owned by a given user.

329
00:12:20,850 --> 00:12:22,080
To use renice,

330
00:12:22,080 --> 00:12:25,020
all you have to do is enter renice -n,

331
00:12:25,020 --> 00:12:26,601
the nice value, the options,

332
00:12:26,601 --> 00:12:29,587
and the identifier on the command line interface.

333
00:12:29,587 --> 00:12:31,890
Now when we're working in the command line,

334
00:12:31,890 --> 00:12:33,900
we're usually working in Bash,

335
00:12:33,900 --> 00:12:35,820
and with Bash, most of your commands

336
00:12:35,820 --> 00:12:38,610
are executed in the foreground by default.

337
00:12:38,610 --> 00:12:40,770
The problem with this is that your command prompt

338
00:12:40,770 --> 00:12:42,495
can become consumed and unusable

339
00:12:42,495 --> 00:12:45,087
until that command finishes processing.

340
00:12:45,087 --> 00:12:46,620
Now for simpler commands

341
00:12:46,620 --> 00:12:48,930
like the date command or the copy command,

342
00:12:48,930 --> 00:12:51,420
or the list command, this isn't really a problem,

343
00:12:51,420 --> 00:12:53,637
but if you're running a longer script or a longer command,

344
00:12:53,637 --> 00:12:56,244
this can start becoming a real issue for you

345
00:12:56,244 --> 00:12:58,713
and it can become possible where you have these commands

346
00:12:58,713 --> 00:13:00,870
that are just taking over your command prompt

347
00:13:00,870 --> 00:13:02,490
and you can't get a new command in

348
00:13:02,490 --> 00:13:04,530
because you're waiting for it too long.

349
00:13:04,530 --> 00:13:06,150
So one of the things you can do

350
00:13:06,150 --> 00:13:08,606
is you can actually run the commands in the background,

351
00:13:08,606 --> 00:13:11,040
and this way, you can execute them

352
00:13:11,040 --> 00:13:13,050
but not consume your shell.

353
00:13:13,050 --> 00:13:16,138
To do this, you can use the fg or foreground command

354
00:13:16,138 --> 00:13:18,298
or the bg, background command,

355
00:13:18,298 --> 00:13:19,690
and this will move the processes

356
00:13:19,690 --> 00:13:21,869
either into or out of view.

357
00:13:21,869 --> 00:13:24,180
Now there are a lot of different options you can use

358
00:13:24,180 --> 00:13:27,480
with both the foreground and background processes.

359
00:13:27,480 --> 00:13:30,419
The foreground or fg can be used with percent,

360
00:13:30,419 --> 00:13:34,380
and then a job ID to bring a job back to the foreground.

361
00:13:34,380 --> 00:13:35,790
If you use Ctrl + Z,

362
00:13:35,790 --> 00:13:37,336
this will halt a job temporarily

363
00:13:37,336 --> 00:13:39,300
so you can use the background command

364
00:13:39,300 --> 00:13:42,810
or bg and a percent sign with a job ID

365
00:13:42,810 --> 00:13:44,918
to push that job to the background.

366
00:13:44,918 --> 00:13:47,080
And if you use the ampersand sign,

367
00:13:47,080 --> 00:13:48,900
this will actually start the command

368
00:13:48,900 --> 00:13:50,130
running in the background

369
00:13:50,130 --> 00:13:52,260
when it's added at the end of the command.

370
00:13:52,260 --> 00:13:54,328
For example, if I use the date command,

371
00:13:54,328 --> 00:13:57,256
I could put date, ampersand and hit Enter,

372
00:13:57,256 --> 00:13:59,848
and it would run the date command in the background.

373
00:13:59,848 --> 00:14:02,280
Now the foreground and background command

374
00:14:02,280 --> 00:14:05,820
are actually part of a larger job control command function.

375
00:14:05,820 --> 00:14:08,036
The main function for this is known as jobs.

376
00:14:08,036 --> 00:14:10,020
If you enter jobs with a command prompt,

377
00:14:10,020 --> 00:14:12,120
it's going to list out all of those jobs you have

378
00:14:12,120 --> 00:14:14,400
either in the foreground or the background.

379
00:14:14,400 --> 00:14:15,690
This gives you a master list,

380
00:14:15,690 --> 00:14:17,313
and then you can then select which ones you want

381
00:14:17,313 --> 00:14:20,782
by using the fg or bg commands to bring them forward.

382
00:14:20,782 --> 00:14:22,950
If you want to stop a foreground job

383
00:14:22,950 --> 00:14:25,020
and place it in the background when you're working in it,

384
00:14:25,020 --> 00:14:27,690
you can simply type in Ctrl + Z.

385
00:14:27,690 --> 00:14:29,880
If you're working in the command line environment

386
00:14:29,880 --> 00:14:31,731
and you're running a program that you want to stop,

387
00:14:31,731 --> 00:14:34,890
you can force quit it by using Ctrl + C

388
00:14:34,890 --> 00:14:36,630
at the command line.

389
00:14:36,630 --> 00:14:38,970
If you want to log out of your current user session,

390
00:14:38,970 --> 00:14:41,730
you can do that by using Ctrl + D.

391
00:14:41,730 --> 00:14:42,930
All of these are shortcuts

392
00:14:42,930 --> 00:14:44,100
for you to be able to go ahead

393
00:14:44,100 --> 00:14:45,872
and move in or out of a process

394
00:14:45,872 --> 00:14:48,420
as you need to when you're working in the jobs

395
00:14:48,420 --> 00:14:50,400
or the command line interface.

396
00:14:50,400 --> 00:14:52,620
Now some other commands we have that are important

397
00:14:52,620 --> 00:14:56,190
are things like nohup and no hangup.

398
00:14:56,190 --> 00:14:58,222
Now nohup or no hangup

399
00:14:58,222 --> 00:15:00,073
is a command that's going to prevent a process

400
00:15:00,073 --> 00:15:02,649
from ending when the user logs off.

401
00:15:02,649 --> 00:15:04,890
For example, let's say an administrator

402
00:15:04,890 --> 00:15:06,180
launched a backup script,

403
00:15:06,180 --> 00:15:07,542
and then logs off the system.

404
00:15:07,542 --> 00:15:10,830
By default, that script would actually stop running.

405
00:15:10,830 --> 00:15:12,510
That would be bad because it means our backup

406
00:15:12,510 --> 00:15:14,040
is going to stop running.

407
00:15:14,040 --> 00:15:17,940
But if we use nohup in front of the normal command,

408
00:15:17,940 --> 00:15:21,210
like nohup.backup script,

409
00:15:21,210 --> 00:15:22,710
that script will continue to run

410
00:15:22,710 --> 00:15:25,020
even after the administrator logged off.

411
00:15:25,020 --> 00:15:26,250
So to run this,

412
00:15:26,250 --> 00:15:30,600
simply type in nohup and the command or script name.

413
00:15:30,600 --> 00:15:32,130
Different commands are also going to be used

414
00:15:32,130 --> 00:15:33,560
to send signals to processes

415
00:15:33,560 --> 00:15:35,250
to either terminate or kill them

416
00:15:35,250 --> 00:15:37,050
when you don't need them anymore.

417
00:15:37,050 --> 00:15:39,150
This is necessary when a process hangs,

418
00:15:39,150 --> 00:15:41,460
which causes a system to become unstable,

419
00:15:41,460 --> 00:15:44,400
or it fails to relinquish control over a file

420
00:15:44,400 --> 00:15:46,140
that you're trying to modify.

421
00:15:46,140 --> 00:15:48,900
A simple way to get this taken care of is by issuing

422
00:15:48,900 --> 00:15:50,160
the kill command.

423
00:15:50,160 --> 00:15:52,748
The kill command is going to send any specified signal,

424
00:15:52,748 --> 00:15:55,470
or by default determination signal,

425
00:15:55,470 --> 00:15:57,330
to one or more processes.

426
00:15:57,330 --> 00:15:58,892
To run this, type in kill,

427
00:15:58,892 --> 00:16:01,379
the options and the process ID.

428
00:16:01,379 --> 00:16:04,003
Another variant of this is known as pkill.

429
00:16:04,003 --> 00:16:07,340
Now the pkill command is going to send a specified signal,

430
00:16:07,340 --> 00:16:09,500
or by default determination signal,

431
00:16:09,500 --> 00:16:11,734
to a process based on a pattern.

432
00:16:11,734 --> 00:16:15,150
Now for example, let's say you start top in one terminal,

433
00:16:15,150 --> 00:16:17,590
and then you issue pkill top in another terminal,

434
00:16:17,590 --> 00:16:20,250
you're going to see that top terminates.

435
00:16:20,250 --> 00:16:24,120
This is because the word top match the pattern of T-O-P

436
00:16:24,120 --> 00:16:25,740
or the top process.

437
00:16:25,740 --> 00:16:27,057
So if you want to run a command like this,

438
00:16:27,057 --> 00:16:29,460
simply enter pkill, the options,

439
00:16:29,460 --> 00:16:31,410
and the pattern you want to terminate.

440
00:16:31,410 --> 00:16:33,236
On the other hand, the killall command

441
00:16:33,236 --> 00:16:35,134
will send any specified signal,

442
00:16:35,134 --> 00:16:37,676
or again by default determination signal,

443
00:16:37,676 --> 00:16:41,520
to all processes that are matching the name specified.

444
00:16:41,520 --> 00:16:42,510
To run this command,

445
00:16:42,510 --> 00:16:44,130
simply type in killall,

446
00:16:44,130 --> 00:16:46,620
the options and the process name.

447
00:16:46,620 --> 00:16:48,229
Now you can also use the kill command

448
00:16:48,229 --> 00:16:50,877
with the process table to end processes

449
00:16:50,877 --> 00:16:52,860
and you can do this by entering kill,

450
00:16:52,860 --> 00:16:54,480
followed by the PID

451
00:16:54,480 --> 00:16:56,940
for that specific process that you want to terminate.

452
00:16:56,940 --> 00:17:00,073
However, the process table may display processes

453
00:17:00,073 --> 00:17:01,860
that do not belong to you.

454
00:17:01,860 --> 00:17:05,278
And as a user, you can only kill things that you own.

455
00:17:05,278 --> 00:17:07,410
As root though, you can kill

456
00:17:07,410 --> 00:17:09,358
any process you want on the system

457
00:17:09,358 --> 00:17:11,579
and there are many different ways to kill a process,

458
00:17:11,579 --> 00:17:13,230
like I just mentioned,

459
00:17:13,230 --> 00:17:15,720
and each one of them will be mapped to a signal.

460
00:17:15,720 --> 00:17:19,140
Now the signal determines how to actually kill your process.

461
00:17:19,140 --> 00:17:21,354
By default, we use the termination signal.

462
00:17:21,354 --> 00:17:24,458
For example, if you want to terminate a process gracefully,

463
00:17:24,458 --> 00:17:27,491
you can give it time to clean up any last minute activities.

464
00:17:27,491 --> 00:17:30,611
However, some processes will ignore this signal

465
00:17:30,611 --> 00:17:32,490
or they'll simply fail to terminate

466
00:17:32,490 --> 00:17:34,320
in a reasonable amount of time.

467
00:17:34,320 --> 00:17:35,670
For those processes,

468
00:17:35,670 --> 00:17:38,040
you may have to issue a more aggressive signal.

469
00:17:38,040 --> 00:17:39,270
Now there are many different types

470
00:17:39,270 --> 00:17:40,980
of kill signals out there.

471
00:17:40,980 --> 00:17:42,390
Each signal has a name

472
00:17:42,390 --> 00:17:44,880
and one or more corresponding number values

473
00:17:44,880 --> 00:17:46,950
and you can use them with the kill command.

474
00:17:46,950 --> 00:17:49,770
The SIGHUP signal has a value of one,

475
00:17:49,770 --> 00:17:51,030
and it's sent to a process

476
00:17:51,030 --> 00:17:53,280
when its controlling terminal is closed.

477
00:17:53,280 --> 00:17:55,290
If that terminal is closed or disconnected,

478
00:17:55,290 --> 00:17:56,970
it's said to have hung up,

479
00:17:56,970 --> 00:17:59,250
which is why this signal is called SIGHUP,

480
00:17:59,250 --> 00:18:03,690
or S-I-G-H-U-P, which stands for signal hang up.

481
00:18:03,690 --> 00:18:06,510
This SIGINT signal is going to have a value of two

482
00:18:06,510 --> 00:18:08,670
that interrupts a process from the terminal

483
00:18:08,670 --> 00:18:10,920
and it enables it to end gracefully.

484
00:18:10,920 --> 00:18:13,140
The signal can be caught or ignored.

485
00:18:13,140 --> 00:18:14,520
This is basically the same thing

486
00:18:14,520 --> 00:18:16,710
as pressing Ctrl + C at a terminal.

487
00:18:16,710 --> 00:18:18,957
A process might change a shortcut behavior over,

488
00:18:18,957 --> 00:18:21,840
and in those cases, you can use SIGINT

489
00:18:21,840 --> 00:18:25,440
Now, the SIGKILL is going to be a signal with a value of nine

490
00:18:25,440 --> 00:18:27,570
and it kills the process immediately.

491
00:18:27,570 --> 00:18:30,090
The signal cannot be caught or ignored.

492
00:18:30,090 --> 00:18:32,940
This is typically used as a last resort.

493
00:18:32,940 --> 00:18:35,220
Another one is SIGTERM.

494
00:18:35,220 --> 00:18:37,560
This is a signal with a value of 15

495
00:18:37,560 --> 00:18:39,030
and it terminates a process

496
00:18:39,030 --> 00:18:41,040
and enables it to end gracefully.

497
00:18:41,040 --> 00:18:43,440
This signal can be caught or ignored

498
00:18:43,440 --> 00:18:45,450
and is typically sent by the kill command

499
00:18:45,450 --> 00:18:47,880
or another program and not from the terminal.

500
00:18:47,880 --> 00:18:50,970
The SIGSTOP signal is going to have a value

501
00:18:50,970 --> 00:18:53,760
of 17, 19 and 23,

502
00:18:53,760 --> 00:18:55,710
and it pauses a process.

503
00:18:55,710 --> 00:18:58,890
This signal cannot be caught or ignored by the program

504
00:18:58,890 --> 00:19:01,890
and this is typically sent from kill or another program

505
00:19:01,890 --> 00:19:04,080
and not by the terminal itself.

506
00:19:04,080 --> 00:19:08,940
The SIGSTP signal has a value of 18, 20 and 24,

507
00:19:08,940 --> 00:19:11,550
and it pauses a process from the terminal.

508
00:19:11,550 --> 00:19:14,460
This signal can be caught and it can be ignored.

509
00:19:14,460 --> 00:19:17,490
This is the same as pressing Ctrl + Z at your terminal

510
00:19:17,490 --> 00:19:20,400
and a process might change this shortcut behavior, however,

511
00:19:20,400 --> 00:19:21,677
in which case you would use

512
00:19:21,677 --> 00:19:25,053
the sigstop instead or SIGSTP.

513
00:19:25,920 --> 00:19:27,810
So let me give you a couple of examples

514
00:19:27,810 --> 00:19:29,400
to show you how this works.

515
00:19:29,400 --> 00:19:33,900
Let's say I had a process that had a process ID of 921.

516
00:19:33,900 --> 00:19:35,370
Now if I wanted to kill that,

517
00:19:35,370 --> 00:19:36,657
I could send the kill signal

518
00:19:36,657 --> 00:19:41,520
by using kill 15 921 at the command prompt.

519
00:19:41,520 --> 00:19:44,550
And this would then send the signal of 15

520
00:19:44,550 --> 00:19:47,130
to kill process 921

521
00:19:47,130 --> 00:19:49,252
which is the signal termination signal.

522
00:19:49,252 --> 00:19:51,570
Now if I wanted to use signal 17,

523
00:19:51,570 --> 00:19:54,690
I could enter kill 17 921

524
00:19:54,690 --> 00:19:56,580
and this would pause that process

525
00:19:56,580 --> 00:19:59,040
instead of removing the process entirely.

526
00:19:59,040 --> 00:20:00,210
Now most of the time,

527
00:20:00,210 --> 00:20:03,090
people who use Linux prefer to do their own troubleshooting

528
00:20:03,090 --> 00:20:06,060
and solving problems becomes an art and a science.

529
00:20:06,060 --> 00:20:07,500
Solving technical problems,

530
00:20:07,500 --> 00:20:09,300
such as those that occur with computers

531
00:20:09,300 --> 00:20:12,300
requires a great deal of specialized knowledge too.

532
00:20:12,300 --> 00:20:13,350
We may use guidelines

533
00:20:13,350 --> 00:20:15,180
when troubleshooting our process issues

534
00:20:15,180 --> 00:20:16,350
and troubleshooting begins

535
00:20:16,350 --> 00:20:17,937
with the identification of a problem

536
00:20:17,937 --> 00:20:20,850
and it doesn't end until the services have been restored

537
00:20:20,850 --> 00:20:24,450
and the problem no longer is adversely affecting our users.

538
00:20:24,450 --> 00:20:27,270
Remember, you want to gather information about a process

539
00:20:27,270 --> 00:20:30,900
including its process ID or PID and the state

540
00:20:30,900 --> 00:20:33,480
when you start to troubleshoot these process issues.

541
00:20:33,480 --> 00:20:35,970
You can use the ps command to print information

542
00:20:35,970 --> 00:20:37,680
about all your running processes

543
00:20:37,680 --> 00:20:39,780
including CPU utilization.

544
00:20:39,780 --> 00:20:41,275
For example, if you have a process

545
00:20:41,275 --> 00:20:43,980
that's consuming over 50% of the CPU,

546
00:20:43,980 --> 00:20:47,220
it may cause performance issues across the entire system

547
00:20:47,220 --> 00:20:49,290
and it becomes worth investigating.

548
00:20:49,290 --> 00:20:51,000
If you're using the top command,

549
00:20:51,000 --> 00:20:53,130
you can retrieve a dynamic and interactive list

550
00:20:53,130 --> 00:20:54,990
of all your running processes.

551
00:20:54,990 --> 00:20:57,870
If you use the systemd-analyze blame command,

552
00:20:57,870 --> 00:20:59,940
you can determine what startup processes

553
00:20:59,940 --> 00:21:02,160
are slowing down the boot operations.

554
00:21:02,160 --> 00:21:03,840
If you use lsof,

555
00:21:03,840 --> 00:21:05,580
you can identify which processes

556
00:21:05,580 --> 00:21:08,070
have files that are open right now and in use.

557
00:21:08,070 --> 00:21:09,960
If you use nice and renice,

558
00:21:09,960 --> 00:21:12,990
you can prioritize certain processes over others.

559
00:21:12,990 --> 00:21:14,970
If you use fg and bg,

560
00:21:14,970 --> 00:21:18,030
you can measure foreground and background processes.

561
00:21:18,030 --> 00:21:19,890
And if you use nohup,

562
00:21:19,890 --> 00:21:23,190
you can keep a command running even after you log off.

563
00:21:23,190 --> 00:21:26,490
Finally, if you use kill and its associated commands,

564
00:21:26,490 --> 00:21:28,440
you can terminate problem processes

565
00:21:28,440 --> 00:21:30,690
and restore the system disability.

566
00:21:30,690 --> 00:21:32,340
Each time you solve a problem,

567
00:21:32,340 --> 00:21:34,320
you're going to increase your troubleshooting skills

568
00:21:34,320 --> 00:21:35,910
by gaining more experience.

569
00:21:35,910 --> 00:21:36,960
And as you start to learn

570
00:21:36,960 --> 00:21:39,690
how to combine these different steps or skip steps,

571
00:21:39,690 --> 00:21:42,290
you can start reaching a solution much more quickly.

