I think one of the best ways to learn about system D services is just to create one ourselves. So I've created a Ruby program on the left. And the goal is to get it onto our machine and manage as a system D service.

Now if you're not familiar with Ruby, don't let that bother you. But I would highly recommend learning Ruby. It's an amazing language. But you should be able to understand this program without understanding too much Ruby. It's a very simple program.

So let's walk through it. At the top, the first thing I'm doing is telling the interpreter or telling Bash that this is going to be a Ruby program, just like any of our other scripts. And then on line 10, we're going to do something called trap a signal.

So this is a type of signal that we can send the process. And this is telling the Ruby program that if we get a signal called you USR1 and call this function here. So if you're familiar with JavaScript, this do keyword here is kind of like passing a callback function.

So this code inside of here will get executed if this process gets a USR1 signal. And what it will do when it gets that signal is just print out to the log that we've received the USR signal. And that might be an example of a program that reloads itself if it gets a signal like USR1. It might reload its configuration.

So down on line 15, that's going to be another kind of signal that we could send, the term signal. So this is the default signal that the kill command sends. Then it tells the process to die, to just shut down. And so what we do when we get that signal is write out to the console that we've received the term signal and then just to die.

And we'll exit with an exit code of 0. Then down here on 22, we're just going to loop indefinitely until we die. And print out Hello from the service with the current time and then sleep for 5 seconds. So every 5 seconds, print out this Hello from the service.

These flush statements here, that just tells Ruby to write to the log right away, not to queue up these messages until there's enough. Just write it right away so that we can see it. Let's before we get into system D, actually just start up this program ourselves. So up in the top right console, I'm going to change into the Vagrant folder.

And remember, the Vagrant folder is the one that is shared between our local machine and the virtual machine. And there you see the HelloWorld.rb file in there. Now Ubuntu comes with Ruby installed. So what we can do to start up this program is say Ruby hello.rb. And it'll start printing to the console every 5 seconds, hello from the service.

Now let's send it a couple of these signals that we've defined up here, that we've defined handlers for. So I've opened up a terminal down in the lower right so that we can send signals to this process. First, we need to figure out what the process ID is. So I'll say PSAF. And it looks like the process ID for this one is 7654.

So let's go ahead and send the USR1 signal. That's going to be like simulating a reload. We'll say kill and then we'll send the USR1. I can just use this dash here for short instead of the dash s. And we'll send that to a process with PID 7654.

Great, it worked just as we expected. So right here, the process or the code trapped this signal and then printed out to the console the current time and that it had received the USR1 signal. Now let's send it the term signal. That will be the signal that system D sends it to terminate itself, to die. So we'll say kill and we'll send the term signal this time again to process 7654.

And this time it says, hey, I got the signal term to die. I notice it actually did quit. So we don't have that process running anymore. So I just wanted to go through this manually so that we know what's happening when system D is sending signals to our process.