# How to automate scripts in Replit

- Tool: Replit
- Difficulty: Beginner
- Time required: 15 minutes
- Compatibility: All Replit plans (Scheduled Deployments require Core or Pro)
- Last updated: March 2026

## TL;DR

Replit supports automation through Shell scripts, the .replit file's run and onBoot commands, and Scheduled Deployments for recurring tasks. Write shell scripts in the Shell terminal, configure onBoot in .replit to run setup commands every time the app starts, and use Scheduled Deployments to execute scripts on a timed basis like daily backups or weekly reports. These approaches replace the need for external CI/CD tools for many common automation tasks.

## Automating Scripts and Tasks in Replit with Shell, onBoot, and Scheduled Deployments

Replit provides several ways to automate repetitive tasks without leaving the browser. You can write shell scripts and run them from the terminal, configure the .replit file to execute commands on boot or as part of the run process, and set up Scheduled Deployments that run scripts on a recurring basis. This tutorial covers each approach with practical examples for common automation scenarios like database backups, dependency updates, and periodic data processing.

## Before you start

- A Replit account (Core or Pro recommended for Scheduled Deployments)
- Basic familiarity with shell commands like cd, ls, and chmod
- An existing project where you want to add automation

## Step-by-step guide

### 1. Write a shell script in the Replit editor

Create a new file in your project with a .sh extension, such as scripts/setup.sh. Write your commands in the file using standard bash syntax. Start the file with the shebang line #!/bin/bash to specify the shell interpreter. After saving the file, open the Shell terminal from the Tools dock and make the script executable with chmod +x scripts/setup.sh. You can then run it with ./scripts/setup.sh. Shell scripts are useful for grouping multiple commands that you run together regularly.

```
#!/bin/bash
# scripts/setup.sh - Project setup automation

echo "Starting project setup..."

# Install Node.js dependencies
npm install

# Run database migrations
npm run migrate

# Seed the database with test data
npm run seed

# Build CSS if using Tailwind
npm run build:css

echo "Setup complete!"
```

**Expected result:** The script file is saved and executable. Running it from Shell performs all the setup steps in sequence.

### 2. Configure the onBoot command in .replit

The onBoot key in the .replit file runs a command every time the Replit environment starts. This is useful for setup tasks that need to happen before your app runs, like installing dependencies, running migrations, or setting up the environment. Open the .replit file (enable Show hidden files in the file tree menu if you do not see it) and add the onBoot key at the top level. The command runs once when the workspace boots, not on every Run click. If you need multiple commands, chain them with && or point to a shell script.

```
# .replit
entrypoint = "src/server.js"
onBoot = "npm install && npm run migrate"
run = "node src/server.js"

[nix]
channel = "stable-24_05"
packages = ["nodejs-20_x"]
```

**Expected result:** When the Replit workspace boots, npm install and migrations run automatically before you interact with the project.

### 3. Run multiple processes with the run command

The run key in .replit supports running multiple processes simultaneously using the ampersand operator and the wait command. This is useful when your project has both a backend server and a frontend build process that need to run together. Each process runs in the background, and wait keeps the Run button active until all processes complete. You can also use this to run a file watcher alongside your main server.

```
# Run backend and frontend dev servers simultaneously
run = "node server.js & npm run dev:css & wait"

# Or use an array for a single command
run = ["node", "src/server.js"]
```

**Expected result:** Both your backend server and frontend build process start when you click the Run button.

### 4. Set up a Scheduled Deployment for recurring tasks

Scheduled Deployments run your app on a defined schedule, making them ideal for tasks like daily database backups, weekly report generation, or periodic API data syncing. Open the Deployments pane from the Tools dock and select Scheduled as the deployment type. Enter a schedule using natural language like 'Every day at 3 AM' or 'Every Monday at 10 AM.' Add the build and run commands that execute your task. The deployment runs, completes the task, and shuts down. Billing is $1/month base plus compute time at $0.000061 per second.

```
// scripts/daily-backup.js
// Run as a Scheduled Deployment daily

import pg from 'pg';
import fs from 'fs';

const pool = new pg.Pool({
  connectionString: process.env.DATABASE_URL,
});

async function backup() {
  console.log(`Backup started at ${new Date().toISOString()}`);
  
  const tables = ['users', 'products', 'orders'];
  
  for (const table of tables) {
    const result = await pool.query(`SELECT * FROM ${table}`);
    console.log(`${table}: ${result.rows.length} rows exported`);
    // Process or send data to external storage
  }
  
  console.log('Backup completed successfully');
  process.exit(0); // Exit so the scheduled job completes
}

backup().catch((err) => {
  console.error('Backup failed:', err.message);
  process.exit(1);
});
```

**Expected result:** The scheduled deployment runs your backup script at the configured time and shuts down after completion.

### 5. Create a deployment build script for production

The [deployment] section in .replit has separate build and run commands for production. The build command runs once during deployment and is where you install production dependencies, compile TypeScript, build CSS, and run any preparation steps. The run command starts the production server after the build completes. Organize complex build steps into a dedicated shell script to keep the .replit file clean.

```
#!/bin/bash
# scripts/build.sh - Production build automation

set -e  # Exit on any error

echo "Installing production dependencies..."
npm ci --production

echo "Building TypeScript..."
npx tsc

echo "Building CSS..."
npm run build:css

echo "Running database migrations..."
npm run migrate

echo "Build complete!"
```

**Expected result:** The build script runs all production preparation steps in order and stops on any error.

### 6. Add environment-specific logic to automation scripts

Your scripts may need to behave differently in development versus production. Use the REPLIT_DEPLOYMENT environment variable to detect the environment. In shell scripts, check with an if statement. In Node.js, read process.env.REPLIT_DEPLOYMENT. This lets you skip database seeding in production, use different API endpoints, or adjust logging verbosity based on the environment.

```
#!/bin/bash
# scripts/init.sh - Environment-aware initialization

if [ "$REPLIT_DEPLOYMENT" = "1" ]; then
  echo "Production environment detected"
  npm ci --production
  npm run migrate
else
  echo "Development environment detected"
  npm install
  npm run migrate
  npm run seed
  echo "Test data seeded"
fi
```

**Expected result:** The script detects the environment and runs the appropriate commands for development or production.

## Complete code example

File: `.replit`

```toml
# .replit - Complete automation configuration

entrypoint = "src/server.js"

# Run setup tasks when the workspace boots
onBoot = "bash scripts/init.sh"

# Development: run server and CSS watcher together
run = "node src/server.js & npm run dev:css & wait"

hidden = [".config", "node_modules", "package-lock.json", "dist"]

[nix]
channel = "stable-24_05"
packages = ["nodejs-20_x"]

[[ports]]
localPort = 3000
externalPort = 80

# Production deployment configuration
[deployment]
build = ["bash", "scripts/build.sh"]
run = ["node", "dist/server.js"]
deploymentTarget = "cloudrun"

# Environment variables for development
[run.env]
NODE_ENV = "development"
```

## Common mistakes

- **Forgetting to make shell scripts executable with chmod +x** — undefined Fix: Run chmod +x scripts/yourscript.sh in the Shell before trying to execute the script. Without the execute permission, you get a 'Permission denied' error.
- **Using ; instead of && to chain commands** — undefined Fix: The ; operator runs the next command even if the previous one failed. Use && so the chain stops on failure, preventing broken states from incomplete runs.
- **Not calling process.exit() in Scheduled Deployment scripts** — undefined Fix: Without process.exit(), the Node.js process keeps running after the task completes, and you pay for idle compute time. Always exit with code 0 for success or 1 for failure.
- **Expecting the filesystem to persist between scheduled runs** — undefined Fix: The deployment filesystem resets each time. Store any output data in the PostgreSQL database or Object Storage, not local files.

## Best practices

- Use the onBoot command for setup tasks that need to run every time the workspace starts
- Chain commands with && so the sequence stops on the first failure instead of continuing
- Always add set -e at the top of shell scripts to exit on any error
- Use npm ci instead of npm install in production build scripts for faster, more reliable builds
- Call process.exit(0) at the end of scheduled scripts to avoid paying for idle compute time
- Keep automation scripts in a dedicated scripts/ directory with descriptive file names
- Test all scripts in the development Shell before using them in deployments
- Use REPLIT_DEPLOYMENT to detect production and adjust script behavior accordingly

## Frequently asked questions

### What is the difference between run and onBoot in the .replit file?

The onBoot command runs once when the Replit workspace starts. The run command executes every time you click the Run button. Use onBoot for setup tasks like installing dependencies and run for starting your application.

### Can I schedule a task to run every hour?

Yes. Scheduled Deployments accept natural language schedules like 'Every hour' or 'Every 2 hours.' The deployment boots, runs your script, and shuts down until the next scheduled time.

### How much does a Scheduled Deployment cost?

Scheduled Deployments cost $1/month base fee plus $0.000061 per second of compute time. Build time is free. A script that runs for 30 seconds daily costs roughly $1.05/month total.

### Can I use cron syntax for scheduling?

Replit Scheduled Deployments use natural language input rather than cron syntax. Enter schedules like 'Every day at 3 AM' or 'Every Monday at 10 AM' and Replit interprets them.

### Do shell scripts have access to my Secrets?

Yes, in the development workspace after a reboot (run kill 1 first). In Scheduled Deployments, add secrets in the deployment configuration. They are available as environment variables in your scripts.

### Can I run background processes that persist after I close the browser?

In the development workspace, processes stop when you close the tab. For persistent processes, deploy your app using a Reserved VM deployment, which keeps the instance running continuously.

### How do I debug a failing automation script?

Run the script manually from Shell to see error output. Add echo statements or console.log calls at key points. Check the Console tab for output from scripts triggered by the Run button or onBoot.

### Can I trigger a script from an external webhook?

Yes. Set up an HTTP endpoint in your app that runs the script when called. Deploy the app with Autoscale so it responds to incoming webhook requests and executes the automation logic.

---

Source: https://www.rapidevelopers.com/replit-tutorial/how-to-create-automated-deployment-scripts-using-replit-s-shell-access
© RapidDev — https://www.rapidevelopers.com/replit-tutorial/how-to-create-automated-deployment-scripts-using-replit-s-shell-access
