Check out a free preview of the full Introduction to Bash, VIM & Regex course

The "Job Control" Lesson is part of the full, Introduction to Bash, VIM & Regex course featured in this preview video. Here's what you'd learn in this lesson:

James examples job control as a method Bash handling multiple programs running in parallel. James takes questions from students.

Preview
Close

Transcript from the "Job Control" Lesson

[00:00:00]
>> James Halliday: I'd like to keep going if that's okay, and talk about job control which is an important idea in bash and also like the operating system. It contextualizes how we run programs as well I think in a nice way. So If you remember, UNIX systems are kind of first designed for doing time sharing on these big mainframes which kind of carry you over a lot of those ideas into modern systems as well.

[00:00:27]
They're still useful for time sharing operating systems which is what we all have now. We can run multiple programs at the same time, so here's a fun game we can play. It's called time cat. So if you take time cat, how you play this game is (you do time cat) you try to get as close to one second without going over as possible.

[00:00:52]
>> James Halliday: 0.5.
>> James Halliday: 0.7, 0.91, that's pretty good. And then you hit CTRL + C when you want the program to exit. And it will print the amount of time that has elapsed. So Ctrl+C is an example of a signal that gets sent to the program when you want the program to exit.

[00:01:26]
>> James Halliday: Any high scores?
>> Speaker 2: 0.97.
>> Speaker 3: Yep, 1.017.
>> James Halliday: That doesn't count, you went over it.
>> Speaker 3: [LAUGH]
>> Speaker 2: That's what I was afraid of.
>> James Halliday: [LAUGH] It's like Price is Right rules. So anyways, that's just a fun example of CTRL+C. Also if a program is running and you just want it to stop, if you hit CTRL+C,

[00:01:49]
>> James Halliday: Then it will stop. [LAUGH]
>> James Halliday: There's another fun thing called CTRL+Z. So CTRL+Z takes something from the foreground. Which is all that we've dealt with so far. So whenever you run a command, you're actually running it in the foreground and so for example if I run watch

[00:02:16]
>> James Halliday: That should one date then it's gonna just run this command every second. If I hit Ctrl+Z then it takes that program that was running in the foreground and it puts it in the background and it stops it So now that program is no longer running, and I can do other stuff.

[00:02:33]
I can take LS, take date. And when I resume that program, I can type fg, which puts that program back in the foreground. And there it is.
>> James Halliday: You can actually do this to more than one program at a time. So if I want to open up a file called cool.txt,

[00:02:52]
>> James Halliday: I can do CTRL+Z there as well, and now I see that this is job number two. And you can type this command called jobs to see what jobs are running, and if you do foreground, it's going to go the last Job that was active. But you can also specify a number for the job, like %1 refers to the first job, which is the watch kids.

[00:03:20]
And %2 is in this case VI, which is the running.
>> James Halliday: You can also use the command called kill to send a signal to the program to terminate. So, if I do kill %2, that's gonna stop the text editor, vim. So, if I type jobs now, actually it didn't kill it, because some programs are a bit touchy about what signals they accept.

[00:03:55]
So if you send just the standard which is the hangup signal to vim, then it won't actually stop. So you might have to send a different kind of signal like, if you've ever heard of -9, that's like the most severe kind of Signal you can get a program that can't be intercepted by the program itself.

[00:04:13]
So the operating system will just deal with it, and it will get rid of that program for you, so. So then if you hit Enter again, you see that, yeah that job was killed now. And there's only this one running. I think the other one will probably be better behaved.

[00:04:28]
No it's still running. Could try HUP, no doesn't do. Yeah I guess just -9 that one as well, then it's gone. I think when jobs are in a stop status they don't,
>> James Halliday: It's a little bit harder. So how the signals work with like hitting CTRL+Z and also doing fg is also with bt sending a signal, which is what killed us.

[00:04:57]
So, there's a bunch of signals, I think if you man kill, it doesn't list the signals there, but there's a man page that it references called signal. And you can read about what all the signals do, it's actually about c source, but I think if you scroll down a ways, well, I guess it's not in there.

[00:05:16]
But, anyways, there are a bunch of these signals, they also have names, so they are called things like SIGTERM, or SIGKILL, is the one that's dash nine, or SIGCUP
>> James Halliday: And how the stop and foreground works is there's one called sig cont, sig continue, for doing the foregrounding, and sig stop, which puts the Java in the background.

[00:05:40]
So it's sort of how that works behind the scenes. And this is something that the kernel handles for you.
>> James Halliday: There's also a %% which is like the same as the last job if you need to mess around with jobs. I found that it's pretty handy to use Vim.

[00:05:59]
If you're giving a presentation or something and you have Vim open, and maybe you're coding something and then you wanna like drop down to the command line really fast. You can just hit Ctrl+Z, and do some stuff, like run a program whatever, and then fg back into your text editor.

[00:06:17]
That's a pretty handy work for us sometimes.
>> James Halliday: Another way to background something right away, if you don't wanna run something and then hit Ctrl+Z right away, you can do just an N percent. So for example, if I do watch dash N one date ampersand, it's going to put that program in the background immediately.

[00:06:47]
But it's not actually going to run that program because it stopped. So what you have to do if you want one of those programs that's in the background to run in the background you can do BG which not all programs are very happy about, so like this one in particular doesn't really like being put in the background.

[00:07:12]
But for other programs, they'll just keep going, so like if I write a little script with the sub shell, I think that that'll work, so I do sleep one, date.
>> James Halliday: Right, yes, so this one is just running on its own. And so this is pretty trippy. There's actually a program running that's spamming my terminal with output but I can also type commands and things.

[00:07:39]
So if you just kill % % then it stops it.
>> James Halliday: And now, there's no more jobs. If you accidentally spawn a bunch of zombie processes that are running in the background and you don't know how to stop them, there's a really useful command called pgrep that searches for a process by its name.

[00:08:05]
So, if you remember that you started a program like watch or something, you can do, so if I start up that program again, It's like no, and if you do pgrep watch then you can see that there's all of these processes. Wanna do it over here. You could also -l and -lf will kinda list a bit more about the program.

[00:08:28]
So this is all watchdog, it's actually because the watch command is, this isn't the watch command, that's why. This is a while true command.
>> James Halliday: Now, why don't I run a watch command? Now, over here we should see it. Right. So there we go. That's our program. If I just do this other command called pkill watch, it's gonna try to kill all these jobs, but these are all owned by the root user so it's only gonna be able to kill this one which is actually what we want it to do.

[00:09:01]
And there we go, that program is already terminated. So these are some ways p-grab, p-kill, different ways of managing processes on your system. You could also kill a process by its pid it's called which is the process ID, which is this number right here. So every process has an ID that you can reference that by.

[00:09:26]
Okay, why don't we-.
>> Speaker 2: Question.
>> James Halliday: Yeah.
>> Speaker 2: If I do CTRL+Z to put a process in the background, on my terminal it says suspended, and to me the word means sleep or not running. But it's really running, right?
>> James Halliday: It depends. Sometimes, the programs, they can listen to these events and decide to do different things.

[00:09:49]
I think by default, the process will keep going if you do CTRL+Z, but some programs know that they shouldn't do anything cuz they can listen for that event, like the watch command and VI, just kinda suspends operations.
>> Speaker 2: So it depends.
>> James Halliday: It depends, yeah.
>> Speaker 2: All right.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now