A super basic guide to "nohup"May 20, 2023 | Development
hope you are doing well today. 😊
Well, this wasn't the first time I used this little command but I simply used it again today and even learned something new in this domain. Therefore I thought to myself... Let's write a guide for today, especially since my "personal method" took a bit of research to "discover".
Okay. Not exactly like that, and also in German, but you get the idea. So, here we go.
First of all, what are we talking about? What is nohup?
Personally, I think the first few sentences of the corresponding Wikipedia article are a very good way to explain it. Honestly, I couldn't do it better with my very limited or rather non-existent writing skills.
So let's just use that...
nohup is a POSIX command which means "no hang up". Its purpose is to execute a command such that it ignores the HUP (hangup) signal and therefore does not stop when the user logs out.
Output that would normally go to the terminal goes to a file called nohup.out, if it has not already been redirected.
To say it in even simpler terms. It's a command which runs another command even after you exit your terminal session.
Normally, this is part of a bigger application that you want to run continuously in the background. In this case, a Docker container would be the better solution since it offers high levels of isolation, transportability, and customizability.
Sometimes you want to run something small though, and in this case, a Docker container is way too overkill. That's where nohup comes into play.
Let's say you want to run a simple jar file in that manner. Usually, that would be run by executing "java -jar myprogram.jar". Now we just add the "nohup" as a prefix like this. "nohup java -jar myprogram.jar".
Ta-Da... It runs. We can even add the "&" suffix to run all of that in the background, like this. "nohup java -jar myprogram.jar &".
Okay. We can run small something in a very simple manner, but then we leave. Well... How do we stop this then?
I mean. The process ID gets printed out right after starting it up. If we're honest with ourselves for one second here... Do you know where you can find that note someday in the future? I at least couldn't. So to get this, let's run "ps -ef" to retrieve all process IDs with their corresponding program as well as their executable path. With that, we can easily identify the correct one. 😉
Now that we got the process ID we can kill the corresponding process by executing "kill -9 <PROCESS ID>" and it's gone. Insert your favorite reference here...
So yeah... There's a lot more going on in that domain. But I wanted to keep it simple and easily usable/understandable.
I hope I somewhat succeeded in this regard. 😅