- Podcasts
- Two's Complement
- Running Programs
Running Programs
Two's Complement
- September 12, 2025
- 41 min
About this episodeMatt and Ben discuss running in production; from running processes in screen to battling systemd configuration files. Ben sketches out…
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
Running Programs
Uh, I know that there are lots of places in the world where as a software engineer, you're expected to write code. And then there's another group or team or organization. or outsourced company that is responsible for actually taking that software and running it on computers and making sure that it continues to run on those computers and that it delivers the value that it is intended to do. And in some cases, those things are like very separated, right?
Right, right. And then on the other end of that spectrum, I think you can have situations and I have definitely been in these myself where it is like, no, we're building this for the very first time. There's no infrastructure team. There's you and you are going to compile your code. you are going to SCP your code onto a server somewhere, and then you are going to run a screen and then exec that program in the screen.
And, I think that you can in your career and I have done a lot of this as a software engineer, you can kind of like hop to the left. I don't know.... the side of that spectrum and say, all right, well, okay. I obviously don't want to run it in a screen. What else could I do? And then you start learning about like systemd and things like runit and supervisord and things like that.
Well, even screen is one level too far from, "I literally run the process and it's there and I'm watching it and I'm watching it I'll Control-C it", which you know is also valid, but it gives us a sort of starting point of like, what happens when you fire up a process and why is that not okay?
Yeah, right. Right. Yeah, that's great. I love this. Okay, so it's like, so when you do that, you're like, all right, my plan for deployment now is I'm going to SSH onto the production server or EC2 instance or whatever you got, and I'm going to copy and SCP my bits up there, and then I'm going to run
Right. Then eventually the TCP connection between your computer and the SSH daemon on the remote end will time out. There'll be a keep alive that's missing probably, or some other heart beating mechanism will go down. And the SSH daemon will say, hey, that person's gone now. It's time to clean up their session. It will, I think, kill the bash process. And the bash process then will kill all of the children that it knows about, Something like that. Or there's some... sig...Yeah, so this is this is kind of it, right? So what what is...
We close our laptop lid, we go home, we get the unfortunate call, and then we rush home, and then we open the laptop lid back up, and then we rerun the process. All right, well, I can't do that. So an enterprising person might say, okay what I'm going to do is I'm going to use the bash ampersand command because I know that will put a process into the background, right?
Right. Right. Well, I think what happens, and I think this because I've had this burn me from time to time, is that, yes, you redirected standard out, but you did not redirect standard error. And so there is actually still the daemon has a file handle that it thinks it needs to be writing to back to your thing. And so you put this in the background and you do this again and it breaks again. it does exactly the same thing all over again.
the console, the terminal, something. I'm waving my hands a lot here because that's a very, I'm less sure about it. But I certainly know that if you try and read from the console, you'll get one of the even more esoteric signals about like, hey, yeah, you can't, you're not connected to it right now. [editing matt here, SIGTSTOP maybe?] And then you'll get stopped.
Right, right. So this is where you start investigating all of the various options that you can pass to SSH when you run this, because you're like, going to make a script. I'm going to make a script that works, and I'm just going to run the script, and it's going to do my deploy, and then I'm going to trust that it works. And you start learning about, OK, well, I need to do the option that like doesn't read from standard in, because I don't want the standard in problem. And then I got to make sure that I redirect standard out and standard error so I can put this thing in the background.
So... That's what nohup's for. It's like it gets of the hang-up and there's some other things that it does. And then there's daemonization and other bits and pieces, which which I'm sure we'll get to in a second. But let's put that to one side and let's go down the rabbit hole that you've described, which is that like I'm now going to run SSH on my computer
um And I'm going to pass it rather than just SSH. I'm going to do /path/to/my/executable with all the redirects and things set and try and run it from a server and have it live on the remote machine with all of the pipes and things stdin, stderr and stdout all connected to sensible places. So go ahead.
Um, And at that point, you maybe have something where you can close your laptop and have it hang out. Now, hopefully you sent your log somewhere sensible and you don't fill up the disk with logs. You can pipe it into syslog, which is something that I do when I'm trying to punt on this problem entirely is I'm just like, you know what? There's already a log rotation system on this machine and it's called syslog. So I'm just going to pipe all my logs into that.
The first thing you need to do is fork, which gives you a new process, a shiny new process. Then you call something called setsid, which says, I would like to become the session leader for this new process that I've been created because only a process group, and I'm doing this from memory, so listener, please. And although Ben's nodding, this is not necessarily correct. So just take this massive pile of [salt]
Yes. So you fork. The child process then does setsid to become a process leader in its new group. And then if I remember rightly, you have to fork again to then dissociate yourself from any last tendrils that previous process had. And now you're running and you are completely in the clear. It's something like that. It's some weird sequence of events, which means that you have lost all connection with the previous process. And so when you run some like system process and you pass it with --d or -d, sorry, then, and it immediately returns and disappears. Apparently like, "Hey, did it do anything?" But you know, you you run PS and it's still running. That's the kind of process that it's been through. And you're, you know, you can type jobs and it won't be there. It's like completely lost from you. And probably...
I don't realize that the thing you were just talking about and I'm having the penny is dropping now some of the flags that you were talking about finding for SSH to set it up correctly might be the ones that effectively have the same side effect but I having just written something that is a daemon for the if you go back to the systemd conversation we were having last time something became a daemon and I went through that process so it's a bit
Somewhat in top of mind. And even though I had a daemonization thing there, I still, you can choose, I think, systemd, which we're going to, to say either systemd runs the process and does that for it in its own container, or it's expecting it to run in that particular way. ah And so it can babysit different types of processes, if I remember rightly. Okay, let's go back to what you said about systemd, because that sounds like a useful thing to know about. What is systemd?
Is that a thing? I feel like I've said this before on the podcast. I don't remember the difference between computer science and software engineering. We know this one computer science is solving problems with computers. Software engineering is solving the problems that you create when solving problems with computers. And ah this is a, this is exactly.
Yeah. um And so what problems are we, are we both solving and creating by using systemd? Well, so you write your bash script, it deploys your thing. You shut your laptop and then you wait five minutes, you open it back up and then you have [a check?] and it's still running. And you're like right, I think I maybe believe that this is going to work. And you go home and the next day you come in and still running. Cool. And then three days later it crashes. And you're like, what would have been super cool is instead of me getting a phone call in the middle of the night because it crashed, if it had just restarted.
...a set of of essentially shell scripts that get run to fire things up in the right order. Again, I'm probably a bit... missing loads of bits of context here, but it's a sort of a more principled approach where you have units that are like, I would like this thing to run, please. I would like this to be true under these circumstances. And it depends on these other things that also need to be either running or at least have started before me. And so instead of having essentially numbered directories with, you know, 40.do-this , 41.do...,
And systemd does that kind of the right way by actually tracking dependencies, which again was expensive and caused me problems in our last conversation, but is is the right approach and the correct thing to do. And so that's what systemd is. It's like the overarching orchestrator of a computer and all of the processes that are running on it.
Which... is I think for most people, when Linux systems particularly moved from init to systemd, the biggest frying pan to the side of the head was, where are all my chuffing logs? They used be /var/log/whatever, and that's burnt into my mind. They are text files and are in /var/log/blah, and systemd stopped that. And now there are a few logs in /var/log, but nowadays you have to interact with it through, and it has a binary log file format, as I understand it, behind the hood. And you have to learn journalctl, which I still haven't learned, and I still Google the same thing over and over and over again and type in the thing that it tells me to do, which...
is ...note to self. Don't, don't do taking a note here. Don't do that. Make a cheat sheet for it and stick it to my monitor. Like all the other cheat sheets I have. Yeah. So that was, but that was like the, but that broke most people, I think, because I didn't have to interact with adding and removing daemons from my system. That's what, you know, my package management system did. But whenever something went wrong, I'm like, where the hell's the log file? Anyway, so journalctl.
this needs This script needs to run after I run. There's a few, like, customization points you've got like that. And you can say what you're wanted by as well. So in this instance, you probably say I'm wanted by multi-user.target, which is like a magical sort of target that says, hey, when it becomes a multi-user system, the fifth, whatever, um run level five, then this is, I am saying that I am wanted by it, which is a way of you kind of going the other way around from the usual dependency saying it depends on me.
Right. So what's next is that you discover that the thing just crashes every four or five days ah because it's running out of memory because it needs to run on more than one computer. It is too big. So you have to now run it on multiple computers and you have to distribute whatever work it's doing.
That is probably what I would do. And then I would have the thing where I would try to deploy it and there'd be some package or some configure. Oh, we got to increase the size of the maximum size of the receive buffers on the network. And so now I've got to like go and change that configuration. I gotta change it. And I've already scaled this out to like 10 computers now, like every month for the last, you know, 10 months, I've been just adding another computer to my to the list of hosts.
And if you think of, you know, the system, ah sorry, the systemd configuration unit file, whatever we just said, as being part of this machine configuration, then it does make sense to talk about some of the other things that you might need that machine to have set up like packages. And as you say, system settings. So yeah let's segue into that. Let's do it.
Yeah. Yeah, OK. So you've decided that now, okay I need to retire this bash script. It's served me well, but it's time to move on to something a little bit where I don't have to like build all this stuff myself and make sure that it works and troubleshoot it all. So I'm going to try to use Ansible. Let's just say.
So Ansible is, uh, honestly a tool that I have only used sometimes. It is not, I sort of like wind up making the jump from like, the shell script to like terraform. That's usually what I do is I'm like, all right, I'm going to go and I'm going to have something like nomad manage these, or I'm going to manage them in the cloud, just making Docker containers.
Yes. But, but real, but real high level Ansible is you write a playbook. And I think that playbook is pretty much in YAML and it's got like the steps that you want to perform. And there's like a lot of sort of baked in things of like, "Oh, I need to copy this artifact from this place to this place". Cool. I need to create a, configuration file here. Cool. I need to restart systemd. Cool. It can do all those things for you. And there's lots of baked-in tools in Ansible to sort of do the typical system management things: You can install packages. You can create users. You can.. you know, because it's like hopefully, like you said, we weren't running this thing as a root. So we had a dedicated user for it. I need when I'm setting up a new machine, I need to make that user. I need to make sure they don't have a password, that they have the right SSH keys, you know, all those kinds of wonderful things. So you have some, you know, script or some playbook that you run, you know, as root because it needs to be able to do all these things. But then it sort of sets up the environment and then like subsequent deploys and things can, you know, kind of make it that the program can run as a user and it doesn't need to root.
And you kind of roll your eyes and do them. And it's like, well, let's automate this. And it does it in a principled way using, with a bunch of support files that help you, ah make sort of support functionality that lets you do like add user rather than having to go whatever steps you actually have to take to add the user, which I forget these days.
And Unix systems are so, so complicated. I can't actually imagine how you could write a more general purpose like make my system look this way thing except for at least one listener somebody is currently shouting "Nix" into the void as they're walking along and I know that Nix solves this in a very cool way and I'm very excited by it but I don't have any personal experience with it other than someone demoing to me and me going wow that is super cool.
And then you're like, that seems great. And then you just log back onto the machine and just go "sudo apt install bob". And you're like, there we are. We're done. Anyway, back to, oops, I've just banged my, yeah but sorry, editing Matt. You just, I've just whacked the microphone stand. [that's ok, I didn't edit it out -editing Matt] Where were we? So I was sort of saying that there's this sort of difference between sort of prescriptive run these things in order and maybe they're idempotent or maybe they can adapt and say like, well, if's if there's a user already there, don't re-add it, that kind of feeling. Versus the Terraform thing where you just say I should like this to be the end state. Here is a list of users the machine has to have with the properties that users have.
Because...very much part of the, the last week's conversation. The reason I was looking into that was because I was avoiding writing several conference talks that I have to give in about a month's time. And a week has passed since we last spoke; now I'm giving away all of our secrets. Although much longer will have passed in real time. And I've probably given the conference talk by the time I've released this. um So listen, you can be the judge of whether it was any good or not. But yeah, I have done no work on it at all. So... ..oops. But yeah, this is a rough draft of a conference talk on...
How are you going to do it? And I feel like the punchline of this is like, okay, and now we're migrating this all to the cloud and we're going to use Terraform. We're going to use GCP or maybe you have like, you know, ah a lot of companies I feel like these days have like and essentially like an internal cloud. Like they're still using Terraform, but they're using tools like Nomad and they have their own, you know, physical servers and they have an infrastructure team that's managing it all. And this maybe leads us back. This is how you get this. Okay. This is the whole ... This is how you get into the state where you're just like, yeah, I just like changed one function with some unit tests and pushed to PR and I have no idea where goes.
Which in this case, you know maybe your level of abstraction is those cloud tools that we've just been talking about and the services that run. But knowing enough about the level beneath you to say like, okay, I do know that there are processes that run and that something is taking care of the input and output for those processes and making sure the right signals get to them at the right time and not the wrong things like me logging out.
But I don't know that it exists and maybe I could sketch something, but I don't necessarily know off the top my head. And then you should know beneath that what... that something exists, right? Beneath that layer, we know that there is a systemd and I don't know how that works, but it's always good to have a decent understanding of the level beneath where you're working and then be aware of the layer below that.
