Check out a free preview of the full Complete Intro to Linux and the Command-Line course:
The "Signals" Lesson is part of the full, Complete Intro to Linux and the Command-Line course featured in this preview video. Here's what you'd learn in this lesson:

Brian explains that signals are notifications that are sent to programs from the command line, and gives examples of signals such as CTRL + D -SIG, a signal used in the command line to signal the end of the use of a program. SIGTERM and SIGKILL are also discussed in this section.

Get Unlimited Access Now

Transcript from the "Signals" Lesson

>> So signals are a particular construct within bash, so you can send basically a programme of signal to do something, right? So you can have a programme that's running and it takes 10 minutes to run. And let's say that programme needs to end early, you can send it a signal that hey programme, me the user, I'm asking you to end early.

[00:00:20] Can you handle that? So it doesn't force the program to do anything. It just tells the program, here's a signal of my intent, it's up to you to do something about it. Like a good example of that would be if someone calls you right, that's a signal for them to want to talk to you but you the program don't have to answer the call, right?

[00:00:45] That's kinda what signals are as you're calling the program, it's up to the program how it wants to answer that. Just kind of like best practices of like how to handle signal so relatively most programs will handle signals in a relatively consistent way. But again, it's up to the programme to implement signals.

[00:01:04] Some of them just choose not to. So the first one that you will use all the time perhaps the most is SIGINT. So, SIG, signal, INT, interrupt. So you're telling it, the programme please interrupt. A lot of times, that just means stop running. So if I do for example, if I go back to the terminal here.

[00:01:26] If I do tail and I'm gonna do -f, which means continue to tail and I do bash_history like this, history. Notice this program is still running. What tail-f does is like, watch this file and if anything gets new, it gets added to it. Show me those things as they're being added, right?

[00:01:45] It's anticipating that things will be added to it. But let's say I'm done now and I want the program to end. If I don't do anything right now, it's just gonna keep running forever, right cuz that's what that -f does. So in order for me to get the program to stop, I have to send it as SIGINT, as SIG interrupt.

[00:02:05] So if I hit Ctrl and C, it says, please stop and then have like, the tail-f is waiting for that signal and once it catches that SIGINT, it's like cool, I'm gonna stop and that's when it stops right there. So you'll see this. What is that, it's not a circumflex.

[00:02:27] That's what it's called in language. I can't remember the name of that character right now. Anyway, so you'll see this character right here if you'd like to shift six character right there. You'll see that a lot that represents control, right? So, when I did this, you see that the circumflex and C, that is me pressing Ctrl+C.

[00:02:51] So just be aware that if you see that, in fact, if you're on a Mac keyboard, that's what it looks like. So just be aware of that. Okay. Yeah, here's another one of my favorite programs is called yes. So, a lot of these programs like in the early days were interactive and you had to answer yes to a bunch of questions.

[00:03:13] Some computer scientists got real sick of answering yes, so he wrote a program called Yes. And all it does is spam the character why and indefinitely and it just answers yes to every question that you ask it. But it will do that infinitely until you stop it. So I have to Ctrl+C and then it stops.

[00:03:36] Okay. So, that's SIGINT. That's a good one to know. There's another one called SIGQUIT. So interrupt is a like a less powerful, it's more it's more like hey, stop doing what you're doing but SIGQUIT's like, shut it down, I'm done with you. In fact, if I'd just hit Ctrl+D right now, don't, but if you did, well, I'll just show you.

[00:04:00] It'll actually end my terminal sessions like, you told me to quit, I'm quitting right? So now this is actually done I can't even restart this, so I have to reopen my shell now. Just close that old one. That's what SIGQUIT is, it's a strong signal, it's like quit the entire program.

[00:04:19] So like a good example of this, if I say Python3 and I get popped into the Python3 repl, it actually doesn't respect keyboard interrupts, right? So it's not going to end it if I tell it that so you actually have to send Python three a SIGQUIT, for it to say, okay, you wanted me to stop so I hit Ctrl+D and that's how I quit Python3, the repl for Python.

[00:04:45] So let's get to like generally my progression of things is I'll try Ctrl+C to see if it stops. If it doesn't stop, then I'll try Ctrl+D to see if that makes it stop and usually one of the two will quit any program. So, just keep those in mind.

[00:05:04] Try Ctrl+C first if that doesn't work, then try Ctrl+D. Okay, for this next one here SIGTERM. SIGTERM is, I think it's terminate. But this happens when your computer's like shutting down, right? It'll actually send SIGTERMs to all of your programs and say like, hey, we're shutting down everyone, you get your opportunity right now to shut your stuff down.

[00:05:36] I'm gonna give you a bit of time, here's your SIGTERM. And then once you're all done, then it shuts the entire terminal down. So you don't send send SIGTERMs typically through computer sending SIGTERMs to the various different programs. You can send SIGTERMs yourself but that's not what it's meant for.

[00:05:55] It's meant to be from the operating system itself because it's shutting down. And then everyone's favorite at the end is SIGKILL which is like, you're done, it's program assassination, right? It gets shut down immediately. You don't get to clean up, you don't get any options, it's just done.

[00:06:14] All right, so if I look at my programs now you can see where's the yes, I had a yes running. No I don't. So, I'll show you how to do this in a second but I'm gonna start a program running in the background. And I'm going to have it just infinitely go in the background.

[00:06:47] All right, so now you can see here, I have this yes running in the background which is infinitely spanning why, right? And it's just running in the background taking up CPU. It's not doing anything useful. So I wanna kill this program. Now I can give it like a soft SIGINT and it would totally respect that but just to show you how to do a SIGKILL like immediately kill this and don't clean up after yourself.

[00:07:11] And that's like really the big difference here is things that are sent to SIGTERM, and SIGQUIT, and SIGINT. We'll get an opportunity to wrap things up, write some files out, clear out the logs, send out something off to the server. SIGKILL will not allow it to do that.

[00:07:28] So you can use something called kill. And if I do kill. And actually I think I might have a, Yeah, okay. And then I'm going to give it this process ID 1465. So the -9 represents SIGKILL. And the way you can actually tell that is you can see number nine right here is SIGKILL.

[00:08:03] So kill-l will show you all the various different signals that are possible. And most of these I don't even know what they do and I never use them like, well, let's see like SIGPWR or I guess it's probably power SIGIO SIG [INAUDIBLE] I don't know some of these I just don't even know what they do and you never have to really use them directly.

[00:08:22] There's SIGALARM which is like sound the alarm on the computer. There's a bunch of these, but the -9 here says, send signal number nine, which is SIGKILL to process 1465, and we'll get into like the process stuff here in just a little bit, but I wanted you to know what SIGKILL does.

[00:08:42] And also I wanted you to know like what the ejector button is, which is kill -9 process ID will just destroy any process. You can also do, yeah, like this. You can do -SIGKILL like this. And I know this just just has one dash but that's how kill works.

[00:09:01] Like I said it's up to every program to implement flags however they want to, but -SIGKILL would have done that as well. Like if I do 1467 here, which I know doesn't exist, but that would have sent SIGKILL to 1467. And again, kill -l will show you all of the possible signals that you can send to programs.