# Run a Python script 24/7

> A script runs around the clock when three things hold: the machine stays on, the process survives your logout, and something restarts it after a crash. An EnvHaven workspace covers the first two by itself; here is the setup, and the other places it can run.

_Updated November 23, 2026_

A scraper, a price tracker, a trading bot, a job that sends a morning digest: each is a Python script that has to keep running when you are not looking. Three conditions decide whether it does.

1. **The machine stays on.** A laptop that sleeps stops the script, and so does any hosted environment that shuts down when idle.
2. **The process survives your logout.** Closing a terminal or an SSH session ends the processes started in it, unless they run under tmux, nohup or a service.
3. **Something restarts it.** Network errors and exceptions happen; a supervisor or a retry loop brings the script back.

## On a workspace

An EnvHaven workspace meets the first two by itself: the machine never sleeps, and its terminal is a tmux session that keeps running when you close the tab. Python 3.12 and `uv` are ready to use.

1. Put any keys the script needs in the workspace's **Vars** pane; they stay write-only and reach every new shell.
2. Open a new terminal window and start the script, or ask an agent from the **Launch** menu to start it in a window of its own.
3. For the third condition, ask the agent: "Wrap this script so it restarts on any exception and logs to a file."
4. Close the tab. The script keeps running while you are away.

A process started in the terminal does not come back by itself after a workspace restart or update, so start the script again after one. If the script needs to be reachable (a webhook receiver, a small dashboard), bind it to `0.0.0.0:3000` and it is live at the workspace's preview address with HTTPS.

## Where it can run

| Where | Machine stays on | Survives logout | Notes |
| --- | --- | --- | --- |
| An EnvHaven workspace | Yes | Yes | Python 3.12, `uv` and built-in agent support |
| A VPS you manage | Yes | With tmux or systemd | You set up everything |
| A sleeping free tier | No | Yes | Stops when idle |
| Your laptop | No | With tmux | Fine for testing |

## Scheduled jobs

Some jobs only need to run at set times. On a workspace, keep one small loop running that waits for the next run and then calls the script. An agent can write it for you ("run digest.py every day at 07:00 and sleep in between"), and it lives in a terminal window like any other process.

On a server you manage yourself, a cron entry handles it with nothing running in between:

```bash
crontab -e
# every day at 07:00
0 7 * * * cd ~/digest && uv run digest.py
```

## Keep reading

- [Keep a script running after closing SSH](https://envhaven.com/guides/keep-a-script-running-after-closing-ssh)
- [Host a Telegram bot 24/7](https://envhaven.com/guides/host-a-telegram-bot)
- [Idle shutdown](https://envhaven.com/glossary/idle-shutdown)
- [VPS](https://envhaven.com/glossary/vps)

Source: https://envhaven.com/guides/run-a-python-script-24-7
