Background processes get upgraded to parent process of (on Linux usually) init, PID 1.
obviously the & is for run it on background. The question is why it keep running if it was launched by root but root is no longer active?
root is active as long as your system is up running. (root as in the superuser.)
Anyhow, it doesn't have anything to do with the root user per se. As
the process you started is not dependent on the terminal, (or the like),
ending the parent process would not terminate the child. It usually become orphan for a short while then adopted by init.
A lot of your processes are run under other accounts. Try e.g.:
ps aux | awk 'NR>1{print $1}' | sort -u
To illustrate one can instead use another account, e.g. testuser.
sleeplong:
#!/bin/sh
sleep 9999
Save and cmod +x sleeplong. Run under testuser:
user@host $ su testuser
testuser@host $ ./sleeplong &
[1] 9692
Open top with PIDs:
user@host $ pids="$(pstree -cpsa 9692 | \
sed 's/ *[^,]*,\([0-9]*\).*/\1/' | tr '\n' ',')"; \
top -w 90 -p ${pids}1
Enter V to get tree
PID USER TIME+ COMMAND
1 root 0:01.03 init
19787 user 95:30.58 `- terminal
8835 user 0:00.16 `- bash
9634 testuser 0:00.04 `- su
9642 testuser 0:00.09 `- bash
9692 testuser 0:00.00 `- sleeplong
9693 testuser 0:00.00 `- sleep
Exit:
testuser@host $ exit
Run top routine again:
PID USER TIME+ COMMAND
1 root 0:01.03 init
9692 testuser 0:00.00 `- sleeplong
9693 testuser 0:00.00 `- sleep
You can visualize this further by doing something like this:
Expand the script to:
#!/bin/sh
sleep 8888 &
sleep 9999
ecode=$?
printf "Bye\n"
exit $ecode
Run it ./sleeplong2 & (su or not).
- Start
top with same routine as above and enter c to show arguments.
In other terminal:
kill NNN # Where NNN=PID of sleep 8888
kill NNN # Where NNN=PID of sleep 9999
Exit code from last kill should normally be 143. That is
128 + 15 = 143
As kill defaults to 15, or SIGTERM.
Another thing to try out could be to kill bash (or the like) where sleep
reside.
Also not that you can do e.g.:
$ su testuser -c './sleeplong &'
Hope it became a bit clearer.