Android stops/pauses execution of my background script?
Android turns off some of CPUs and/or don't let apps use them when it's dozing. It's achieved through Linux kernel's Control Groups. One of the cgroups is cpuset that controls which CPU is assigned to which processes. Android creates multiple descendant cgroups in cpuset e.g. background, foreground, system-background, top-apps etc.

Apps are normally in background category which can use least no. of CPUs - usually one or two. Core native services like vold, zygote (and system_server), telephony services, and apps which run a foreground service (e.g. notification) or those exempted from Battery Optimization or those having privileges like allow-in-power-save are put in foreground group which can use CPUs even when device is in sleep mode.

So you need to add the script to foreground cgroup in order to keep it alive. From within script:

~# echo -n $$ &gt;/dev/cpuset/foreground/tasks


Going even more aggressive, you can hold a wake lock so that device doesn't suspend:

~# echo -n my_script &gt;/sys/power/wake_lock


Don't forget to release it (echo -n my_script &gt;/sys/power/wake_unlock) after stopping the script.

Please note that both of these measures can adversely affect your battery performance, particularly the latter. Android's warning:


  Creating and holding wake locks can have a dramatic impact on the host device's battery life