Skip to main content
54 votes
Accepted

Getting a pid for an ssh process that backgrounded itself

Finding the pid by grepping might be error prone. Alternative option would be to use ControlPath and ControlMaster options of SSH. This way you will be able to have your ssh command listen on a ...
Rafał Krypa's user avatar
53 votes
Accepted

Why does "yes&" crash my Bash session?

Background processes which write to the terminal are only suspended if the TOSTOP output mode is set, which isn’t the case by default. Try stty tostop yes & and you’ll see that yes is suspended. ...
Stephen Kitt's user avatar
32 votes
Accepted

Why process killed with nohup

As far as I know, there are two situations that can cause a process to be killed after being protected by nohup, and each situation has a different workaround. One possibility, which does not appear ...
Fox's user avatar
  • 8,383
31 votes

In a shell script, how can I (1) start a command in the background (2) wait x seconds (3) run a second command while that command is running?

Unless I'm misunderstanding your question, it can simply be achieved with this short script: #!/bin/bash process_a & sleep x process_b (and add an extra wait at the end if you want your script ...
dr_'s user avatar
  • 32.4k
29 votes
Accepted

When were background processes invented?

The first system to support multiple concurrently-executing processes, or at least to simulate the concurrent execution of multiple processes, was the Atlas system developed at Manchester University ...
Stephen Kitt's user avatar
28 votes

Run multiple commands and kill them as one in bash

I'm surprised this isn't here yet, but my favorite way of doing this is to use subshells with background commands. Usage: (command1 & command2 & command3) Output is visible from both, all ...
jv-dev's user avatar
  • 381
28 votes
Accepted

How do I foreground a job in a script?

You need to just enable job control in the shell with set -m #!/bin/bash set -m sleep 3 & # test sleep 1 # wait some sleep 4 & # run program under test jobs fg %1 quoting from bash ...
Andy K's user avatar
  • 511
27 votes

Run two scripts after each other in the background? && and & don't work?

That's one of the known deviations¹ of zsh compared to sh (or csh for that matters). In sh: A && B & is short for: (A && B) & That is, it runs a subshell that runs that and-...
Stéphane Chazelas's user avatar
23 votes

How to kill SSH session that was started with the -f option (run in background)

As answered by others here, pkill ssh kills it. To create a tunnel that could be brought back, I start it in screen without the -f option, and then detach the screen with Ctrl-A D. To bring back the ...
Haotian Yang's user avatar
23 votes
Accepted

How does anacron work if it's not a daemon?

It uses a variety of methods to run: if the system is running systemd, it uses a systemd timer (in the Debian package, you’ll see it in /lib/systemd/system/anacron.timer); if the system isn’t running ...
Stephen Kitt's user avatar
23 votes

How do I foreground a job in a script?

#!/bin/bash sleep 3 & sleep_pid=$! sleep 1 sleep 4 & wait "$sleep_pid" Putting that sleep 3 in the foreground would be equivalent to waiting for it to finish. I'm assuming this is ...
Kusalananda's user avatar
  • 356k
21 votes
Accepted

Explain why watch 'jobs' does not work but watch 'ps' work?

jobs is a built-in that reports on the state of the current shell: the commands that were backgrounded with that shell. watch runs a new shell for each execution, and that shell's jobs has no way of ...
muru's user avatar
  • 78.1k
20 votes

How do I list all background processes?

The jobs command will show any background jobs started within the current shell, usually by starting a background task with the & operator or ^Z bg (e.g. sleep 10 &). If you want to see all of ...
Jeremy Meadows's user avatar
19 votes

Keep processes running after SSH session disconnects

Tmux is a good option available to run your long-running processes in the background. I have to keep running the long-running processes on a google cloud platform's VM instance/server (with OS: ...
Yogesh Awdhut Gadade's user avatar
19 votes
Accepted

Why can't I see the "wget" job when I execute it in the background?

When using wget with -b or --background it puts itself into the background by disassociating from the current shell (by forking off a child process and terminating the parent). Since it's not the ...
Kusalananda's user avatar
  • 356k
19 votes
Accepted

SSH Tunnel (Port forwarding) in background

You mention ssh -f, which is correct, but you missed -N, which is the remaining piece of the puzzle: -N Do not execute a remote command. This is useful for just forwarding ports. [...] So close! Try ...
Chris Davies's user avatar
17 votes

Why would a backgrounded job with `| less` be stopped, while the other one without it is running?

Let's look more closely at what's happening to less: $ pdfgrep -R -i spark . | strace less & [...] open("/dev/tty", O_RDONLY|O_LARGEFILE) = 3 ioctl(3, TCGETS, {B38400 opost isig -icanon ...
Mark Plotnick's user avatar
17 votes
Accepted

How to run multiple background jobs in linux?

You can use the & to start multiple background jobs. Example to run sequentially: (command1 ; command2) & Or run multiple jobs in parallel command1 & command2 & This will start ...
alpha's user avatar
  • 2,034
17 votes
Accepted

What happens if I start too many background jobs?

Could all 700 instances possibly run concurrently? That depends on what you mean by concurrently. If we're being picky, then no, they can't unless you have 700 threads of execution on your system you ...
Austin Hemmelgarn's user avatar
17 votes
Accepted

Bash wait for all subprocesses of script

Yes, it's enough to use a single wait with no arguments at the end to wait for all background jobs to terminate. Note that background jobs started in a subshell would need to be waited for in the same ...
Kusalananda's user avatar
  • 356k
15 votes

bg command not sending process to background

From your description, the bg command did work; otherwise Ctrl+Z and Ctrl+C would still work. Just because a process is in the background doesn't mean it can't still send you output. If you don't want ...
user10489's user avatar
  • 10.9k
14 votes
Accepted

Why jobs lost when after reconnect ssh?

Shell jobs don't belong directly to the user. I mean there is no global list of jobs for the user. A job can be a process that belongs to the user and you can find all processes belonging to the user. ...
Kamil Maciorowski's user avatar
13 votes
Accepted

Why nohup background process is getting killed?

Your Python program undoes nohup. nohup ignores the hangup signal with SIG_IGN and then chain loads your program in the same process. Your Python program promptly resets the signal handling for the ...
JdeBP's user avatar
  • 72k
12 votes
Accepted

Does backgrounded child process die with parent?

There are a lot of factors to consider here. Please take this answer with a slight grain of salt as I'm writing all these details from memory. (And please correct me via edits or comments if you ...
Wildcard's user avatar
  • 37.5k
12 votes

What happens if I start too many background jobs?

It's hard to say specifically how many instances could be run as background jobs in the manner you describe. But a normal server can certainly maintain 700 concurrent connections as long as you do it ...
laenkeio's user avatar
  • 621
12 votes
Accepted

What exactly does it mean to run a process in the "background"?

The Unix concept of a background process is part of job control. Job control is the organization of processes around a terminal session. Daemons normally don't run in a terminal session, therefore ...
Kaz's user avatar
  • 8,897
12 votes
Accepted

Open pdf files in the background with fzf

You can’t background the entire pipeline because fzf needs to interact with the terminal. So the shell doesn't wait for the termination of the viewer, you can run xargs in a subshell as an ...
Stephen Kitt's user avatar
11 votes

"Variabilize" the ampersand (background a process)

You can flip things and variabilise “foregrounding”: FOREGROUND=fg sleep 5 & ${FOREGROUND} Set FOREGROUND to true or empty to run the process in the background. (Setting FOREGROUND to true to run ...
Stephen Kitt's user avatar
11 votes
Accepted

"Variabilize" the ampersand (background a process)

It's not possible to use a variable to background the call because variable expansion happens after the command-line is parsed for control operators (such as && and &). Yet another option ...
Jeff Schaller's user avatar
  • 68.8k
11 votes

What happens if I start too many background jobs?

Using & for parallel processing is fine when doing a few, and when you monitor progress. But if you are running in a corporate production environment you need something that gives you better ...
Ole Tange's user avatar
  • 37.5k

Only top scored, non community-wiki answers of a minimum length are eligible