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 ...
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.
...
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 ...
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 ...
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 ...
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 ...
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 ...
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-...
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 ...
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 ...
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 ...
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 ...
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 ...
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: ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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. ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
background-process × 646bash × 214
shell × 129
process × 109
shell-script × 97
linux × 55
jobs × 48
nohup × 47
command-line × 41
job-control × 39
kill × 38
ssh × 37
terminal × 36
process-management × 28
signals × 26
io-redirection × 21
zsh × 20
scripting × 18
daemon × 15
pipe × 14
ps × 14
python × 13
disown × 13
parallelism × 12
ubuntu × 11