Skip to main content

Running the website instance in the background using tmux

note

In this section, we assume you have set up a website which is running on a localhost port

You might notice that if you were to close the terminal window which has the Docusaurus/Flask process running, you will no longer be able to access the website. In fact, if you were to run

curl 127.0.0.1

in a terminal window which has a SSH-connection to the VM, you will get a response from Apache telling you that this website is no longer available:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>503 Service Unavailable</title>
</head><body>
<h1>Service Unavailable</h1>
<p>The server is temporarily unable to service your
request due to maintenance downtime or capacity
problems. Please try again later.</p>
<hr>
<address>Apache/2.4.58 (Ubuntu) Server at 127.0.0.1 Port 80</address>
</body></html>

Obviously, we cannot rely on having the terminal process constantly open to ensure that our website is up and running. The solution to this is to use a program like tmux to run the program in the background. To install tmux, run:

sudo apt install tmux

or consult the official documentation. If tmux is installed on your VM instance, run

tmux

You might now notice that the terminal window has slightly changed. On my computer, a green band appears along the bottom of the window, like this: alt text Ensuring we are now in the right folder (e.g. "my-website"), you can now run

npx docusaurus start

and you will see that the process is starting again. Now, if you were to close the terminal window and start a new window with a SSH-connection to your VM instance, running

curl 127.0.0.1

will give you expected response (only the first lines are shown here):

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="generator" content="Docusaurus">
<title>My Site</title>
<link rel="alternate" type="application/rss+xml" href="/blog/rss.xml" title="My Site RSS Feed">
<link rel="alternate" type="application/atom+xml" href="/blog/atom.xml" title="My Site Atom Feed">
<script defer src="/runtime~main.js"></script><script defer src="/main.js"></script><link href="/styles.css" rel="stylesheet">
</head>
<body>
<script>

to reconnect to your previous tmux session, you can run:

tmux run a #

and to show all of your current running tmux sessions, you can run:

tmux ls

You might notice that you get the response no server running on /tmp/tmux-1000/default from the above command, even though you are sure that you have a session running succesfully. The explanation to this is that tmux is user specific, and as such, you should make sure that you currently are logged into the correct user on your VM.

If you wish to end a tmux session, you can run

exit

if you wish to exit a tmux session without ending it, you can press Ctrl+B followed by D

https://www.ionos.com/digitalguide/server/know-how/tmux-terminal-multiplexer/