Search Authority

Run Bash Script in Background: Ultimate Guide

Running a bash script in background lets you start long tasks and keep using the terminal. This approach is common for automation, monitoring, and maintenance workflows where yo...

Mara Ellison
Run Bash Script in Background: Ultimate Guide

Running a bash script in background lets you start long tasks and keep using the terminal. This approach is common for automation, monitoring, and maintenance workflows where you do not want to wait for the process to finish.

It is also useful when you need a command prompt available for other work. With the right options, a background job can continue running after you log out or close the session.

Method Terminal remains open Survives logout Use case
nohup Yes Yes Long-running scripts and services
& Yes No Quick background tasks in current session
screen or tmux Yes Yes Interactive sessions with detach/reattach
systemd service No Yes System-level daemons and scheduled automation

Background Execution with & and disown

Start and detach with &

Using & at the end of a command line puts the process into background immediately. The shell assigns a job number and process ID so you can manage it with jobs and kill commands.

Protect against SIGHUP with disown

By default, bash sends SIGHUP to jobs when you exit, which terminates them. Running disown removes the job from the shell table so that logout does not kill the process, while keeping it running in the background.

Reliable Background Execution with nohup

Ignore hangup signals

nohup shields the script from hangup signals, allowing it to continue after you log out. Output is redirected to nohup.out unless you specify another file, helping you capture logs for later review.

Combine with & for full protection

Using nohup together with & gives a robust pattern: nohup script.sh &. This starts the process in background, ignores hangups, and keeps the terminal free for other commands.

Managing Background Jobs and Output

Track jobs with jobs and fg

Use jobs to list active background tasks for the current shell. Bring a task back to foreground with fg %jobnumber to interact with it or stop it gracefully.

Control output to avoid blocking

Redirect stdout and stderr to a file or to /dev/null when you do not need console output. Proper redirection prevents the process from stopping due to full buffers and makes log analysis easier.

Advanced Scheduling with at and systemd timers

Run once at a specific time using at

The at command schedules a bash script to run once in the future. It is ideal for one-off background tasks, and you can manage the queue with atq and remove jobs with atrm.

Persistent scheduling with systemd timers

For recurring automation, a systemd timer can start your script in background at set intervals. This method integrates with system supervision, provides logging, and survives reboots reliably.

Best Practices for Background Bash Scripts

  • Always redirect output to a log file for debugging and auditing
  • Use nohup or a terminal multiplexer for long tasks that must survive logout
  • Test scripts in foreground first to catch errors before running in background
  • Log timestamps and key events so you can trace execution later
  • Monitor resource usage and set timeouts to avoid runaway processes

FAQ

Reader questions

How can I start a script in background and ensure it keeps running after I log out?

Use nohup combined with &, like nohup ./script.sh &, which ignores hangups and detaches the process from your terminal session.

What should I do if a background script stops when I close the terminal?

Run the script with nohup, or use screen or tmux to create a persistent session that you can reattach later without interrupting the process.

How do I check the status of a script running in background?

Use jobs to view background tasks in the current shell, or ps with grep to find the process ID and inspect its state across sessions.

Can I run multiple scripts in background at the same time?

Yes, you can start several scripts with & or inside screen or tmux windows, and manage each one using separate job numbers or process IDs.

Related Reading

More pages in this topic cluster.

Who Designed the Nike Logo? The Story Behind the Swoosh

The Nike swoosh is one of the most recognizable symbols in the world, but few people know the story behind its creation. This piece explores who designed the Nike logo, why it h...

Read next
What is the World's Hottest Pepper? 🌶️🔥

When people ask about the world's hottest pepper, they usually mean the variety that currently holds the Guinness World Record and pushes the boundaries of capsaicin heat. Peppe...

Read next
Jon Huertas in This Is Us:角色, 出演时期与剧情影响详解

Jon Huertas 在《这就是我们》中饰演成年 Kevin Pearson,这一角色从2016年首播持续至2022年最终季,构成了剧集核心家庭叙事的重要组成部�...

Read next