Skip to main content
RapidDev - Software Development Agency
replit-tutorial

How to automate scripts in Replit

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.

What you'll learn

  • Write and execute shell scripts from the Replit terminal
  • Configure onBoot and run commands in the .replit file
  • Set up Scheduled Deployments for recurring automated tasks
  • Chain multiple commands for build and deployment automation
Book a free consultation
4.9Clutch rating
600+Happy partners
17+Countries served
190+Team members
Beginner8 min read15 minutesAll Replit plans (Scheduled Deployments require Core or Pro)March 2026RapidDev Engineering Team
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.

Prerequisites

  • 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.

typescript
1#!/bin/bash
2# scripts/setup.sh - Project setup automation
3
4echo "Starting project setup..."
5
6# Install Node.js dependencies
7npm install
8
9# Run database migrations
10npm run migrate
11
12# Seed the database with test data
13npm run seed
14
15# Build CSS if using Tailwind
16npm run build:css
17
18echo "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.

typescript
1# .replit
2entrypoint = "src/server.js"
3onBoot = "npm install && npm run migrate"
4run = "node src/server.js"
5
6[nix]
7channel = "stable-24_05"
8packages = ["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.

typescript
1# Run backend and frontend dev servers simultaneously
2run = "node server.js & npm run dev:css & wait"
3
4# Or use an array for a single command
5run = ["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.

typescript
1// scripts/daily-backup.js
2// Run as a Scheduled Deployment daily
3
4import pg from 'pg';
5import fs from 'fs';
6
7const pool = new pg.Pool({
8 connectionString: process.env.DATABASE_URL,
9});
10
11async function backup() {
12 console.log(`Backup started at ${new Date().toISOString()}`);
13
14 const tables = ['users', 'products', 'orders'];
15
16 for (const table of tables) {
17 const result = await pool.query(`SELECT * FROM ${table}`);
18 console.log(`${table}: ${result.rows.length} rows exported`);
19 // Process or send data to external storage
20 }
21
22 console.log('Backup completed successfully');
23 process.exit(0); // Exit so the scheduled job completes
24}
25
26backup().catch((err) => {
27 console.error('Backup failed:', err.message);
28 process.exit(1);
29});

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.

typescript
1#!/bin/bash
2# scripts/build.sh - Production build automation
3
4set -e # Exit on any error
5
6echo "Installing production dependencies..."
7npm ci --production
8
9echo "Building TypeScript..."
10npx tsc
11
12echo "Building CSS..."
13npm run build:css
14
15echo "Running database migrations..."
16npm run migrate
17
18echo "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.

typescript
1#!/bin/bash
2# scripts/init.sh - Environment-aware initialization
3
4if [ "$REPLIT_DEPLOYMENT" = "1" ]; then
5 echo "Production environment detected"
6 npm ci --production
7 npm run migrate
8else
9 echo "Development environment detected"
10 npm install
11 npm run migrate
12 npm run seed
13 echo "Test data seeded"
14fi

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

Complete working example

.replit
1# .replit - Complete automation configuration
2
3entrypoint = "src/server.js"
4
5# Run setup tasks when the workspace boots
6onBoot = "bash scripts/init.sh"
7
8# Development: run server and CSS watcher together
9run = "node src/server.js & npm run dev:css & wait"
10
11hidden = [".config", "node_modules", "package-lock.json", "dist"]
12
13[nix]
14channel = "stable-24_05"
15packages = ["nodejs-20_x"]
16
17[[ports]]
18localPort = 3000
19externalPort = 80
20
21# Production deployment configuration
22[deployment]
23build = ["bash", "scripts/build.sh"]
24run = ["node", "dist/server.js"]
25deploymentTarget = "cloudrun"
26
27# Environment variables for development
28[run.env]
29NODE_ENV = "development"

Common mistakes when automating scripts in Replit

Why it's a problem: Forgetting to make shell scripts executable with chmod +x

How to avoid: 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.

Why it's a problem: Using ; instead of && to chain commands

How to avoid: 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.

Why it's a problem: Not calling process.exit() in Scheduled Deployment scripts

How to avoid: 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.

Why it's a problem: Expecting the filesystem to persist between scheduled runs

How to avoid: 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

Still stuck?

Copy one of these prompts to get a personalized, step-by-step explanation.

ChatGPT Prompt

I want to automate my Replit project setup with a shell script that installs dependencies, runs database migrations, and seeds test data. Also show me how to set up a Scheduled Deployment for daily database backups.

Replit Prompt

Create automation scripts for my project. Add an onBoot command in .replit that runs npm install and database migrations. Create a build script for production deployment. Set up a scheduled task script for daily database backups that exits cleanly after completion.

Frequently asked questions

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.

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.

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.

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.

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.

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.

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.

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.

RapidDev

Talk to an Expert

Our team has built 600+ apps. Get personalized help with your project.

Book a free consultation

Need help with your project?

Our experts have built 600+ apps and can accelerate your development. Book a free consultation — no strings attached.

Book a free consultation

We put the rapid in RapidDev

Need a dedicated strategic tech and growth partner? Discover what RapidDev can do for your business! Book a call with our team to schedule a free, no-obligation consultation. We'll discuss your project and provide a custom quote at no cost.