Environment: Android version Android8
Usually let the program in the background is the command with the word & can be, but was found on Android 8, the program is indeed back, but then unplug the USB cable connected to the discovery process has ended. Early versions of Android uncertain whether this problem exists.
Some reference online Linux methods, such as adding nohup does not work, or will end. It seems Android adb shell with Linux shell still some differences.
Later found on the Internet at the end of the process is the reason that using exit to exit will not lead to the end of the background. So we try to use the exit to exit, and then unplug the USB. And found that indeed effective. But still you have to use nohup command.
And the difference between the measured setsid nohup: setsid when the current user logs out, the process is over, nohup will not.
The amazing thing, a beginning has been to use nohup does not work, and later can, surprisingly, by trial and error, which is probabilistic, and sometimes will not end sometimes.
Expansion: Several signals the end of the process leading to: SIGHUP 1,2,3,9,15's, SIGINT, SIGQUIT, SIGKILL, SIGTERM
SIGHUP is sent when the terminal is disconnected, plus nohup is to solve the terminal disconnection process leading to the end. Before using proven not added nohup kill -1 process ID, you can kill the process, after the increase in the use of nohup kill -1 process ID can not end the process. Description unplug the USB end of the probability of the process will be caused by other signals.
SIGINT CTRL + C is generally produced,
SIGQUIT when users press the Escape key on the terminal (generally use Ctrl +), interrupt-driven program generates the signal, and sent to all processes in the foreground process group.
SIGKILL can not be ignored, and terminates the process
SIGTERM kill
command sends the default termination signal
To find out which signals lead to disconnect the USB end of the process, specifically a serial terminal connection and USB adb terminal
USB terminal to perform in a background process, performed a serial terminal strace -p process ID of the monitoring process, and then disconnect the USB discovery process is due to receive SIGKILL exit.
nanosleep({tv_sec=5, tv_nsec=1048576}, <unfinished ...>) = ?
+++ killed by SIGKILL +++
Further experiments: After the background process, use the first exit to exit adb, to see whether it has received SIGKILL signal process. Experiments show that the simple use of exit and exit adb will not issue SIGKILL signal, enter adb shell again, a background process still exists. Adb and then exit, unplug the USB receiver to the SIGKILL signal, and thus the description, the SIGKILL signal is issued when disconnecting USB, exit adb not issued.
This signal is then who sent it? adb.exe?
That we continue to open USB, only kill adb.exe. Execution adb kill-server and does not trigger the background process killed. Description should not end computer adb issued.
First, note that at the end of the process adbd received signal 9, then kill off all of the group adbd
init: processing action (sys.usb.config=none && sys.usb.configfs=1) from (init.rk30board.usb.rc:31)
[80345.876778] init: processing action (sys.usb.config=none && sys.usb.configfs=1) from (/init.usb.configfs.rc:1)
[80345.879852] android_work: did not send uevent (0 0 (null))
[80345.880065] configfs-gadget gadget: unbind function 'Function FS Gadget'/ffffffc096228e38
[80345.882355] init: Sending signal 9 to service 'adbd' (pid 6433) process group...
<unfinished ...>) = ?
View the process relationship:
IS2320:/ # ps -e | grep adbd
shell 6501 1 16160 1392 poll_schedule_timeout 4c3aec S adbd
1|IS2320:/ # nohup ./data/stressapptest -s 720000 -i 4 -C 4 -W --stop_on_errors -M 800 > /data/stress.log 2>&1 &
[1] 6526
IS2320:/ # ps -e | grep 6501
shell 6501 1 16160 1420 poll_schedule_timeout 4c3aec S adbd
shell 6515 6501 8932 3072 sigsuspend 79c9b8f634 S sh
IS2320:/ # ps -e | grep 6526
root 6526 6519 938996 821656 hrtimer_nanosleep ef2601e4 S stressapptest
IS2320:/ # ps -e | grep 6519
root 6519 6515 8932 2916 sigsuspend 75eb0f5634 S sh
root 6526 6519 938996 821656 hrtimer_nanosleep ef2601e4 S stressapptest
root 6556 6519 10508 3300 0 723b1f9f94 R ps
root 6557 6519 8696 2576 pipe_wait 7d5d16ef94 S grep
Process tree relationship: adbd-> sh-> sh-> stressapptest
Namely: the user's background process is adbd sub-sub-process, when the end of the adbd will kill off all the underlying processes
Found here, we can solve this problem in two ways
(1) modify adbd adbd do not kill the child process when it exits
(2) identify the causes adbd quit when disconnecting USB, disconnect the USB does not end when adbd
According to the second method investigation
log: When you unplug the USB log
[82733.413003] init: processing action (sys.usb.config=none && sys.usb.configfs=1) from (init.rk30board.usb.rc:31)
[82733.413748] init: processing action (sys.usb.config=none && sys.usb.configfs=1) from (/init.usb.configfs.rc:1)
[82733.414065] configfs-gadget gadget: unbind function 'Function FS Gadget'/ffffffc0ca654038
[82733.415158] android_work: did not send uevent (0 0 (null))
[82733.415385] init: Sending signal 9 to service 'adbd' (pid 6684) process group...
[82733.429269] init: Successfully killed process cgroup uid 0 pid 6684 in 13ms
[82733.430603] init: Service 'adbd' (pid 6684) killed by signal 9
[82733.433194] init: processing action (init.svc.adbd=stopped) from (init.rk30board.usb.rc:35)
[82733.433277] init: processing action (init.svc.adbd=stopped) from (/init.usb.configfs.rc:15)
[82733.464425] init: processing action (sys.usb.config=adb && sys.usb.configfs=1) from (init.rk30board.usb.rc:64)
[82733.464591] init: processing action (sys.usb.config=adb && sys.usb.configfs=1) from (/init.usb.configfs.rc:18)
[82733.464670] init: starting service 'adbd'...
[82733.466184] init: property_set("ro.boottime.adbd", "82733471037723") failed: property already set
[82733.468090] init: Created socket '/dev/socket/adbd', mode 660, user 1000, group 1000
[82733.476293] read descriptors
[82733.476379] read strings
[82733.477581] init: processing action (sys.usb.config=adb && sys.usb.configfs=1 && sys.usb.ffs.ready=1) from (/init.usb.configfs.rc:21)
log can be seen will trigger unplug the USB (sys.usb.config = none && sys.usb.configfs = 1) from (init.rk30board.usb.rc: 31) and sys.usb.config = none && sys. usb.configfs = 1) from (/init.usb.configfs.rc:1)
然后又触发(sys.usb.config=adb && sys.usb.configfs=1) from (init.rk30board.usb.rc:64) 和(sys.usb.config=adb && sys.usb.configfs=1) from (/init.usb.configfs.rc:18)
Leading to the first stop adbd then start adbd
Init rc can be modified to remove sys.usb.config = none && sys.usb.configfs = 1) from (init.rk30board.usb.rc: 31) and sys.usb.config = none && sys.usb.configfs = 1) from (/init.usb.configfs.rc:1) of the operation can stop adbd
Reference Review: init.usb.configfs.rc commented in sys.usb.config = none && sys.usb.configfs = action to 1; init.rk30board.usb.rc the process may not be.
Description, comments can not alone stop adbd can cause unable to connect adb
#on property:sys.usb.config=none && property:sys.usb.configfs=1
# write /config/usb_gadget/g1/UDC "none"
# stop adbd
# setprop sys.usb.ffs.ready 0
# setprop sys.usb.ffs.mtp.ready 0
# write /config/usb_gadget/g1/bDeviceClass 0
# write /config/usb_gadget/g1/bDeviceSubClass 0
# write /config/usb_gadget/g1/bDeviceProtocol 0
# rm /config/usb_gadget/g1/configs/b.1/f1
# rm /config/usb_gadget/g1/configs/b.1/f2
# rm /config/usb_gadget/g1/configs/b.1/f3
# rmdir /config/usb_gadget/g1/functions/rndis.gs4
# setprop sys.usb.state ${sys.usb.config}
After the repair adb root adb device disappearing
Modifications Modifications of the above
即
on property:sys.usb.config=none && property:sys.usb.configfs=1
write /config/usb_gadget/g1/UDC "none"
stop adbd
setprop sys.usb.ffs.ready 0
setprop sys.usb.ffs.mtp.ready 0
write /config/usb_gadget/g1/bDeviceClass 0
write /config/usb_gadget/g1/bDeviceSubClass 0
write /config/usb_gadget/g1/bDeviceProtocol 0
rm /config/usb_gadget/g1/configs/b.1/f1
rm /config/usb_gadget/g1/configs/b.1/f2
rm /config/usb_gadget/g1/configs/b.1/f3
rmdir /config/usb_gadget/g1/functions/rndis.gs4
setprop sys.usb.state ${sys.usb.config}
改为
on property:service.adb.root=1
write /config/usb_gadget/g1/UDC "none"
stop adbd
setprop sys.usb.ffs.ready 0
setprop sys.usb.ffs.mtp.ready 0
write /config/usb_gadget/g1/bDeviceClass 0
write /config/usb_gadget/g1/bDeviceSubClass 0
write /config/usb_gadget/g1/bDeviceProtocol 0
rm /config/usb_gadget/g1/configs/b.1/f1
rm /config/usb_gadget/g1/configs/b.1/f2
rm /config/usb_gadget/g1/configs/b.1/f3
rmdir /config/usb_gadget/g1/functions/rndis.gs4
setprop sys.usb.state ${sys.usb.config}
Reference: https://www.iteye.com/blog/leehomjan-2146739
https://www.cnblogs.com/dspace/p/9825723.html
https://blog.csdn.net/lanyang123456/article/details/60466120
Monitoring process: http://hongjiang.info/shell-script-background-process-ignore-sigint/