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
Write a shell script in the Replit editor
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.
1#!/bin/bash2# scripts/setup.sh - Project setup automation34echo "Starting project setup..."56# Install Node.js dependencies7npm install89# Run database migrations10npm run migrate1112# Seed the database with test data13npm run seed1415# Build CSS if using Tailwind16npm run build:css1718echo "Setup complete!"Expected result: The script file is saved and executable. Running it from Shell performs all the setup steps in sequence.
Configure the onBoot command in .replit
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.
1# .replit2entrypoint = "src/server.js"3onBoot = "npm install && npm run migrate"4run = "node src/server.js"56[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.
Run multiple processes with the run command
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.
1# Run backend and frontend dev servers simultaneously2run = "node server.js & npm run dev:css & wait"34# Or use an array for a single command5run = ["node", "src/server.js"]Expected result: Both your backend server and frontend build process start when you click the Run button.
Set up a Scheduled Deployment for recurring tasks
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.
1// scripts/daily-backup.js2// Run as a Scheduled Deployment daily34import pg from 'pg';5import fs from 'fs';67const pool = new pg.Pool({8 connectionString: process.env.DATABASE_URL,9});1011async 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 storage20 }21 22 console.log('Backup completed successfully');23 process.exit(0); // Exit so the scheduled job completes24}2526backup().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.
Create a deployment build script for production
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.
1#!/bin/bash2# scripts/build.sh - Production build automation34set -e # Exit on any error56echo "Installing production dependencies..."7npm ci --production89echo "Building TypeScript..."10npx tsc1112echo "Building CSS..."13npm run build:css1415echo "Running database migrations..."16npm run migrate1718echo "Build complete!"Expected result: The build script runs all production preparation steps in order and stops on any error.
Add environment-specific logic to automation scripts
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.
1#!/bin/bash2# scripts/init.sh - Environment-aware initialization34if [ "$REPLIT_DEPLOYMENT" = "1" ]; then5 echo "Production environment detected"6 npm ci --production7 npm run migrate8else9 echo "Development environment detected"10 npm install11 npm run migrate12 npm run seed13 echo "Test data seeded"14fiExpected result: The script detects the environment and runs the appropriate commands for development or production.
Complete working example
1# .replit - Complete automation configuration23entrypoint = "src/server.js"45# Run setup tasks when the workspace boots6onBoot = "bash scripts/init.sh"78# Development: run server and CSS watcher together9run = "node src/server.js & npm run dev:css & wait"1011hidden = [".config", "node_modules", "package-lock.json", "dist"]1213[nix]14channel = "stable-24_05"15packages = ["nodejs-20_x"]1617[[ports]]18localPort = 300019externalPort = 802021# Production deployment configuration22[deployment]23build = ["bash", "scripts/build.sh"]24run = ["node", "dist/server.js"]25deploymentTarget = "cloudrun"2627# Environment variables for development28[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.
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.
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.
Talk to an Expert
Our team has built 600+ apps. Get personalized help with your project.
Book a free consultation