Latest / CyberCode Academy / Course 4 - Learning Linux Shell Scripting | Episode 9: Process Management, Scheduling, User Control, and Scripting Utilities
Transcript
- 0:00Speaker 1: Okay, let's unpack this. We've all had that moment, right,
- 0:02Speaker 1: You're working on a server or maybe even just your
- 0:04Speaker 1: own machine, and everything just slows to a crawl.
- 0:07Speaker 2: Yep, the digital engine is just chugging along and you
- 0:11Speaker 2: have absolutely no idea why exactly.
- 0:14Speaker 1: And systemministration these days, it's not about the physical boxes anymore.
- 0:18Speaker 1: It's about mastering that digital ecosystem. It's about seeing what's running,
- 0:22Speaker 1: managing resources and.
- 0:24Speaker 2: Automation, and that's what this deep dive is all about.
- 0:27Speaker 2: We want to give you the essential toolkit to get
- 0:29Speaker 2: that visibility and control. And it all, I mean all
- 0:32Speaker 2: of it revolves around one single concept, the process. The
- 0:36Speaker 2: process it's just a running instance of a program, your
- 0:38Speaker 2: web browser, the shell, you're typing in a little background job.
- 0:42Speaker 2: Each one is a.
- 0:43Speaker 1: Process and to control them you need to handle a
- 0:46Speaker 1: unique identifier.
- 0:47Speaker 2: Right, every single process gets a unique process ID, a PID,
- 0:52Speaker 2: and that PID is your key to everything about it,
- 0:55Speaker 2: Who owns it, how much memory it's eating, CPU time,
- 0:58Speaker 2: all of it.
- 0:59Speaker 1: So if the system slow, step one is discovery. We
- 1:02Speaker 1: need to see what's going on.
- 1:03Speaker 2: We can't fix what we can't see. So our first
- 1:06Speaker 2: tool is PS process status.
- 1:09Speaker 1: Think of it like a camera, right to snapshot of
- 1:10Speaker 1: the system in that one millisecond exactly.
- 1:13Speaker 2: But here's the classic trap, the one that gets everyone.
- 1:16Speaker 1: Oh yeah.
- 1:16Speaker 2: You just type p's and hit enter and you see
- 1:18Speaker 2: maybe what three or four lines? It's only showing the
- 1:22Speaker 2: processes attached to your current terminal.
- 1:24Speaker 1: So how do we see everything? I mean the entire system,
- 1:27Speaker 1: all the background services that are really doing the work.
- 1:29Speaker 2: That's where you need the right flags to see every process.
- 1:32Speaker 2: You use ps E E the E is for every okay,
- 1:37Speaker 2: or the more old school UNIXT way.
- 1:40Speaker 1: Is pss NAS is the important part there.
- 1:42Speaker 2: That's the magic. The NACT removes that restriction. It tells
- 1:46Speaker 2: the system show me everything, not just what's attached to
- 1:48Speaker 2: this terminal, and suddenly you see the system's real workload.
- 1:51Speaker 1: But that can be a fire hose of information, just
- 1:53Speaker 1: a huge wall of text. If I'm troubleshooting, I probably
- 1:57Speaker 1: only care about, say, memory usage and who's star the process.
- 2:01Speaker 1: How do I narrow that down?
- 2:02Speaker 2: You use the ATO flag for output. This lets you
- 2:04Speaker 2: get surgical and specify the exact columns you want to.
- 2:07Speaker 1: See, so I can ask for just what I need exactly.
- 2:10Speaker 2: You can ask for PCPU that's percentage of CPU or
- 2:15Speaker 2: PMM for percentage of memory. You can get the user
- 2:19Speaker 2: the PID, and one that's really critical for debugging, the PPID.
- 2:23Speaker 1: The parent process ID, so you can trace where a
- 2:26Speaker 1: process came from.
- 2:26Speaker 2: You got it. It helps you find the source of
- 2:28Speaker 2: a problem. So you'd run something like PPU PCPU PMM command.
- 2:33Speaker 1: Okay, that's perfect for a SATIC snapshot for deep inspection.
- 2:36Speaker 1: But what if I need to see what's happening right now,
- 2:39Speaker 1: the live pulse of the machine.
- 2:41Speaker 2: Then you don't want a snapshot, You want a video feed.
- 2:43Speaker 2: You want TOP. Yes, TOP is dynamic. It updates constantly,
- 2:47Speaker 2: usually every few seconds, and it sorts by CPU usage
- 2:50Speaker 2: by default. It's the first thing you run for a
- 2:52Speaker 2: quick system health check.
- 2:54Speaker 1: It immediately shows you who the resource hogs are instantly.
- 2:57Speaker 1: Now what if I know the name of the program
- 2:58Speaker 1: I'm after sam looking for in jenks Running a big
- 3:01Speaker 1: pass command and then searching through it feels slow.
- 3:04Speaker 2: Is there a shortcut just to get the PID?
- 3:07Speaker 1: There is a fantastic little utility called PGP. You just
- 3:10Speaker 1: type p rep and jinx and bam gives you the PIDs.
- 3:13Speaker 1: Nothing else that saves so much time.
- 3:15Speaker 2: Especially for scripting. And speaking of scripting, here's a little
- 3:19Speaker 2: pro tip. If you're using PSO to get a clean
- 3:22Speaker 2: list of PIDs for a script, just add an equal
- 3:26Speaker 2: sign like P s U I EO p I D.
- 3:29Speaker 2: That little equal sign tells it to suppress the column hatter.
- 3:32Speaker 2: You just get the numbers, clean data ready to be
- 3:35Speaker 2: piped into another command.
- 3:36Speaker 1: Oh that's brilliant. That saves so much annoying text processing later. Okay,
- 3:40Speaker 1: so we found our problem process. We have the PID. Now,
- 3:44Speaker 1: what how do we take action?
- 3:45Speaker 2: We send it a signal.
- 3:46Speaker 1: Right. Signals are like the nervous system of the OS,
- 3:49Speaker 1: a way for processes to talk to each other.
- 3:51Speaker 2: It's an interruption. You're telling a process, hey, stop what
- 3:54Speaker 2: you're doing and handle this. When you hit CTRLC, you're
- 3:57Speaker 2: sending a signal, and.
- 3:58Speaker 1: To do it manually use the kill command correct and.
- 4:01Speaker 2: By default, if you just type kill and the PID,
- 4:04Speaker 2: it sends sig term, which is signal fifteen.
- 4:07Speaker 1: The polite request.
- 4:08Speaker 2: The polite request, you're asking the process, could you please
- 4:11Speaker 2: clean up your temporary files, release your memory, and shut
- 4:15Speaker 2: down gracefully.
- 4:16Speaker 1: But politeness doesn't always work. What about that process? That's
- 4:21Speaker 1: just stuck in a loop completely unresponsive.
- 4:24Speaker 2: Then you have to ask sure. They represent the average
- 4:26Speaker 2: number of processes that were ready to run, so they
- 4:29Speaker 2: were either running or waiting for CPU time over the
- 4:31Speaker 2: last one, five and fifteen minutes.
- 4:34Speaker 1: So it's like a traffic jam indicator for your CPU.
- 4:36Speaker 2: That's a perfect analogy. If you have a four core
- 4:39Speaker 2: CPU and your one minute load average is eight point
- 4:42Speaker 2: ozh go, it means on average eight processes were ready
- 4:45Speaker 2: to go, but only four could actually run. You've got
- 4:47Speaker 2: a backup.
- 4:47Speaker 1: So if those numbers are high and staying high, you
- 4:50Speaker 1: know your system is overloaded precisely.
- 4:53Speaker 2: And for other basic info unname AA is great. It
- 4:56Speaker 2: gives you kernel version, hardware architecture, all the vitals you
- 5:00Speaker 2: need for security patching.
- 5:01Speaker 1: Now here's where things get really fascinating for me. The
- 5:05Speaker 1: proc directory. This is not a normal directory.
- 5:08Speaker 2: It is truly one of the coolest parts of the
- 5:10Speaker 2: Linux architecture. It's a pseudo file system. It lives entirely
- 5:14Speaker 2: in memory, and it's a direct window into the kernel's brain.
- 5:17Speaker 1: It's not static data at a disc. It's the live
- 5:20Speaker 1: state of the system.
- 5:21Speaker 2: It's live data and the most useful part for every
- 5:24Speaker 2: single running process. There's a directory inside PROC named after
- 5:28Speaker 2: its PID.
- 5:29Speaker 1: So if a process has PID four two ninety five,
- 5:31Speaker 1: there's a pro four two ninety five directory.
- 5:34Speaker 2: Yep, and inside that directory are files that give you
- 5:37Speaker 2: deep diagnostic info about that specific process.
- 5:40Speaker 1: What are the must snow files in there for debugging, Well, two.
- 5:44Speaker 2: Are absolutely critical. First is the environ file. It contains
- 5:47Speaker 2: every single environment variable that was passed to that process
- 5:50Speaker 2: when it launched.
- 5:51Speaker 1: That's gold. If you're trying to figure out why a
- 5:53Speaker 1: script can't find a file or connect to a proxy.
- 5:56Speaker 2: It's all right there. The other key ones are pseudo UD,
- 5:59Speaker 2: which is a link to the process's current working directory,
- 6:02Speaker 2: and x which links to the actual executable file it's running.
- 6:06Speaker 2: It's total transparency that is incredible. And while we're talking
- 6:09Speaker 2: about finding things, a couple of quick debugging tools which
- 6:12Speaker 2: tells you the exact path to a command that your
- 6:15Speaker 2: terminal is running, and what CS gives you a quick
- 6:17Speaker 2: one line summary of what any command does.
- 6:19Speaker 1: Okay, so we can now see control and diagnose the system.
- 6:23Speaker 1: The final piece of the puzzle is making the system
- 6:26Speaker 1: work for US automation.
- 6:28Speaker 2: Right scheduling those nightly backups or clean up scripts so
- 6:31Speaker 2: you don't have to think about them, and that is
- 6:34Speaker 2: the domain of kron.
- 6:35Speaker 1: The trustee scheduler. It uses a configuration file, the kron
- 6:38Speaker 1: tab to run things at specific.
- 6:40Speaker 2: Times, and it is the heart of automated system maintenance.
- 6:44Speaker 1: Let's break down that cron tab format because the system
- 6:46Speaker 1: is mercilessly strict about it.
- 6:49Speaker 2: Oh absolutely. It requires six fields in a very specific order.
- 6:53Speaker 1: The first five are the schedule and the last one
- 6:55Speaker 1: is the command right.
- 6:56Speaker 2: And it goes from most specific to most general. Start
- 7:00Speaker 2: with minute from zero to fifty nine, then our zero
- 7:03Speaker 2: to twenty.
- 7:04Speaker 1: Three, then day of the month, month, and finally day
- 7:07Speaker 1: of the week exactly.
- 7:08Speaker 2: Day of week is zero to six, with Sunday being zero.
- 7:11Speaker 1: And we use symbols to control the timing. The as
- 7:14Speaker 1: risk is the easiest.
- 7:15Speaker 2: One just means every So an asterisk in the hour
- 7:17Speaker 2: field means run this every hour, and.
- 7:19Speaker 1: For specific times you can use commas like five point
- 7:22Speaker 1: fifteen for five and fifteen minutes past the hour yep.
- 7:25Speaker 2: And for intervals, which is really useful, you use a slash,
- 7:28Speaker 2: so five in the minute field means every five minute.
- 7:30Speaker 1: And you edit your personal schedule with krontab e and
- 7:33Speaker 1: you can see it with krontab e right.
- 7:35Speaker 2: And this brings up a really really important point that
- 7:38Speaker 2: trips up almost everyone when they start with.
- 7:40Speaker 1: Kron The environment.
- 7:41Speaker 2: The environment. A kron jog runs in a very bare bones,
- 7:44Speaker 2: minimal environment. It doesn't have all the helpful settings and
- 7:47Speaker 2: PABs that your interactive terminal has.
- 7:50Speaker 1: So what's the golden rule to make sure your cron
- 7:52Speaker 1: jobs don't just fail silently?
- 7:54Speaker 2: Always? Always, always use the full absolute path to your
- 7:58Speaker 2: script or command. Don't just run MyScript dot esh, write
- 8:01Speaker 2: as urbanmyscript dot esh.
- 8:03Speaker 1: And what about things like passwords or other variables the
- 8:07Speaker 1: script might need.
- 8:08Speaker 2: If your script depends on an environment variable, you have
- 8:11Speaker 2: to define it right there in the krontab file before
- 8:14Speaker 2: the command that needs it. Otherwise the job will run,
- 8:16Speaker 2: it won't find the variable and it will fail, leaving
- 8:18Speaker 2: you completely confused.
- 8:20Speaker 1: That detail. Understanding the execution environment, that's what makes a difference. Yeah,
- 8:24Speaker 1: it connects right back to that procbeat environent file we
- 8:26Speaker 1: talked about. If a process needs it to run, you
- 8:28Speaker 1: have to provide it exactly.
- 8:29Speaker 2: So now you have the tools to manage the whole
- 8:32Speaker 2: life cycle.
- 8:33Speaker 1: We really do. We've covered seeing processes with ims and
- 8:37Speaker 1: top controlling them with kill, checking system health with uptime
- 8:40Speaker 1: and the procfile system, and automating it all with chalkron.
- 8:43Speaker 1: That really does give you total visibility and it helps you.
- 8:46Speaker 2: Put all these pieces together. Here's a little challenge for
- 8:48Speaker 2: you to think about. We know how to identify strain
- 8:51Speaker 2: and we know how to filter output. So if you
- 8:54Speaker 2: suspect a program is using way too much memory, how
- 8:57Speaker 2: would you combine the ops of so command with a
- 8:59Speaker 2: souring parameter and the head command to instantly show you
- 9:02Speaker 2: the top ten memory hungry processes on your system. Think
- 9:06Speaker 2: about how you chain those commands together.