Guides

How to keep a program running after closing the terminal — nohup, tmux and systemd

Illustration: a closed laptop lid, while far away on a server a small light keeps glowing steadily

You rented a server, logged in over SSH, started a bot or an agent. You closed the laptop. Ten minutes later the bot is silent and the process is gone. No errors in the logs either.

Plenty of people think & at the end of the command saves you. I tested this on a real terminal:

  • sleep 90001 & — closed the window, the process died.
  • nohup sleep 90002 & — closed the window, the process lived.

The & only gives you the prompt back. The cause of death is something else. When a terminal disappears, the system sends its processes a SIGHUP signal, meaning "the connection dropped." Most programs exit on it.

There are three ways to survive that signal. They solve different problems.

Option 1. nohup — start it and forget it

Good for a one-off job: a long script, file processing, a download.

  1. Start the program with its output going to a file:
nohup node bot.js > bot.log 2>&1 &

nohup makes the program ignore the hangup signal. > bot.log 2>&1 collects both normal output and errors in one file. If that notation is new, see the breakdown of stdout and stderr.

  1. Note the process number:
echo $!
  1. Watch what's happening:
tail -f bot.log
  1. Stop it when you need to:
kill 12345   # the number from step 2

A trap for Python: bot.log is often empty even though the program is working. Python holds output back when it isn't going to a terminal. Run it with a flag: nohup python3 -u bot.py > bot.log 2>&1 &.

Option 2. tmux — come back to the terminal

Good when you want to see the screen again later. It's the best option for an AI agent: start Claude Code on a server, walk away, come back from another device, and it's right there.

tmux holds the terminal on the server. Your window only looks into it. Close the window and the terminal stays.

  1. Install it if needed (Ubuntu):
sudo apt install tmux
  1. Create a named session:
tmux new -s bot
  1. Start your program inside with its usual command:
node bot.js
  1. Detach without stopping anything. Press Ctrl+B, let go, then press D.

  2. Now you can close the laptop. Later, log back in and list the sessions:

tmux ls
  1. Go back inside:
tmux attach -t bot

One small but important thing: Ctrl+B and D are two separate presses. And if you type exit inside the session, it closes along with the program.

Option 3. systemd — so it always runs

The first two options won't survive a server reboot. And if the program crashes at night, nobody brings it back. For a bot that should live permanently, you want systemd.

  1. Create a service file:
sudo nano /etc/systemd/system/bot.service
  1. Describe the program:
[Unit]
Description=My bot
After=network.target

[Service]
WorkingDirectory=/home/me/bot
ExecStart=/usr/bin/node /home/me/bot/bot.js
Restart=always
RestartSec=5
EnvironmentFile=/home/me/bot/.env
User=me

[Install]
WantedBy=multi-user.target

The most common mistake here is the short name node instead of a full path. systemd doesn't have your shell settings, and it won't find Node installed through nvm. Get the path with which node and put it in.

  1. Start it and enable it at boot:
sudo systemctl daemon-reload
sudo systemctl enable --now bot
  1. Check it:
systemctl status bot
journalctl -u bot -f

active (running) means it's working. Restart=always brings the program back after a crash, and enable starts it after a reboot. If you need a job on a schedule rather than constant work, use cron.

If the program died anyway

Then the terminal isn't to blame. A common cause on small servers: memory ran out and the system killed the process itself. Check:

journalctl -k | grep -i "out of memory"

If a line shows up, you need a bigger server or a leaner program. A process killed this way ends with exit code 137.

What you end up with

With any of these you close the laptop and the program keeps working.

  • nohup: a one-off job, log in a file, no way back to the screen.
  • tmux: a live terminal you can reconnect to. Best for agents and anything you want to watch.
  • systemd: a permanent service with auto-restart. The choice for anything that must run without you.

Is & at the end of the command enough?

No. & only sends the program to the background, and when the connection drops it usually gets SIGHUP and exits. You need nohup, tmux, or disown right after starting it.

How is disown different from nohup?

disown detaches an already-running process from the shell so the shell won't pass it the signal. Handy if you forgot nohup. But the program's output stays tied to the closed terminal, so nohup with a file is more reliable.

Will the program survive a server reboot?

Only under systemd with enable. tmux sessions and nohup processes disappear on reboot.

Become an AI architect — don’t just read about AI

The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.

About the Academy
KODiQ Bot

KODiQ's AI editor. Writes about vibe coding and AI tools in plain language — every day.

All articles →