Putting subshell in background vs putting command in background
There is already an answer which gives an improved code snippet to the task the original poster questions was related to, while it might not yet have more directly responded to the question.
The question is about differences of
- A) Backgrounding a "command" directly, vs
- B) Putting a subshell into the background (i.e with a similar task)
Lets check about those differences running 2 tests
# A) Backgrounding a command directly
sleep 2 & ps
outputs
[1] 4228
PID TTY TIME CMD
4216 pts/8 00:00:00 sh
4228 pts/8 00:00:00 sleep
while
# A) backgrounding a subhell (with similar tas)
( sleep 2; ) & ps
outputs something like:
[1] 3252
PID TTY TIME CMD
3216 pts/8 00:00:00 sh
3252 pts/8 00:00:00 sh
3253 pts/8 00:00:00 ps
3254 pts/8 00:00:00 sleep
** Test results:**
In this test (which run only a sleep 2
) the subshell version indeed differs, as it would use 2 child processes (i.e. two fork()
/exec
operations and PID) and hence more than the direct backgrounding of the command.
In the script 1
of the question however the command was not a single sleep 2s
but instead it was a pipe
of 4 commands, which if we test in an additional case
- C) Backgrounding a pipe with 4 commands
# C) Backgrounding a pipe with 4 commands
sleep 2s | sleep 2s | sleep 2s | sleep 2s & ps
yields this
[2] 3265
PID TTY TIME CMD
3216 pts/8 00:00:00 bash
3262 pts/8 00:00:00 sleep
3263 pts/8 00:00:00 sleep
3264 pts/8 00:00:00 sleep
3265 pts/8 00:00:00 sleep
3266 pts/8 00:00:00 ps
and shows that indeed the script 1
would be a much higher strain in terms of PIDs
and fork()
s.
As a rough estimate the script one would have used about 254 * 4 ~= 1000 PIDs and hence even more than the script 2
with 254 * 2 ~= 500 PIDs. Any problem occurring because of PIDs resouce depletion seems yet unlikely since at most Linux boxes
$ cat /proc/sys/kernel/pid_max
32768
gives you 32x times the PIDs needed even for case script 1
and the processes/programs involved (i.e. sed
, ping
, etc) also seem unlikely to cause the inconstant results.
As mentioned by user @derobert the real issue behind the scripts
failing was that the missing of the wait
command, which means that after backgrounding the commands in the loop the end of the script and hence the shell caused all the child processes to be terminated.
This will do what you are expecting for:
#!/bin/bash
function_ping(){
if ping -c 1 -w 5 $1 &>/dev/null; then
echo "UP: $1"
else
echo "DOWN $1"
fi
}
for ip in {1..254}; do
function_ping 192.168.1.$ip &
done
wait
Save it as parallelping, and execute it.
It helps you? It can be converted into a big function, that can be used in a fast while loop too so you can use your imagination programming with it.
Note: You must use bash.