It's called a fork bomb. It defines a function with the name : that takes no parameters () (not that you can pass parameters to a bash function like this but anyway). The body of the function {} contains a call to itself : and the output of itself is piped | into another call to itself :, both of which are started as a background process &. The ; terminates the statement and the final : calls the function, executing it. The function will keep multiplying exponentially until your PC cannot handle it anymore.
It's what causes it to grow so rapidly. This way the function body is to call itself piping its output to itself, so for every function call it calls 2 more of this function. Also, by using pipe, the OS will have to call the function ready to receive the output of the previous function causing parallel processing to take place.
The pipe makes it so that the : on the right consumes the output of the : on the left. It’s what makes the growth exponential by forcing both copies to be alive at the same time
When I first saw this, I was confused by how the pipe would ever resolve, since the first function call never returns. The answer that the & operator applies to the entire function (not just the part after the pipe) and essentially means “immediately return exit code 0, and run the function as a background process”. Thus, each pipe resolves in a single cycle.
Also, the reason this is likely to bring down a system (as well as why it is so hard to clean up) is not that it overloads the memory or CPU, but rather that due to the PID structure, the OS has a hard limit of 65536 processes that can be running at any given time. So, once this process table is filled, it becomes impossible to start a new process. Moreover, even if you can kill one instance, the PID slot will immediately be refilled by another copy.
If you have a working shell, you may be able to run kill -9 -1 to kill all processes owned by the user (as long as kill is a builtin for the running shell). If not, then a reboot is likely the only solution.
2.5k
u/sanchez2673 Aug 01 '22 edited Aug 02 '22
It's called a fork bomb. It defines a function with the name
:that takes no parameters()(not that you can pass parameters to a bash function like this but anyway). The body of the function{}contains a call to itself:and the output of itself is piped|into another call to itself:, both of which are started as a background process&. The;terminates the statement and the final:calls the function, executing it. The function will keep multiplying exponentially until your PC cannot handle it anymore.