atx_sche.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import subprocess
  2. from tools import loggerKit
  3. # 定时启动ATX-SERVER服务
  4. def start_atx_server(device_list):
  5. loggerKit.info("atx-server Starting atx-server service...")
  6. for device in device_list.keys():
  7. loggerKit.info("Currently linked device: {0}", device)
  8. try:
  9. subprocess.run(['python3', '-m', 'uiautomator2', 'init'], check=True)
  10. except subprocess.CalledProcessError:
  11. # Error occurred while running the command
  12. loggerKit.info("Error occurred while checking ATX server status")
  13. # @scheduler.task('interval', id='check_atx_server_status', seconds=30, max_instances=1, misfire_grace_time=60)
  14. def check_atx_server_status(device_list):
  15. loggerKit.info("atx-server 服务状态检测 ... ")
  16. for device in device_list.keys():
  17. loggerKit.info("current linked for device:{0}", device)
  18. try:
  19. # Run the command "ps -ef | grep atx-agent server -d" and capture the output
  20. cmd_ps = f"adb -s {device} shell ps -ef | grep 'atx'"
  21. ps_output = subprocess.check_output(cmd_ps, shell=True)
  22. print(f'{ps_output}')
  23. # Find the process ID (PID) from the output
  24. pid = None
  25. for line in ps_output.splitlines():
  26. if b'atx' in line:
  27. pid = int(line.split(None, 1)[1].split()[0])
  28. print(f'pid:{pid}')
  29. break
  30. # If a PID is found, kill the process using "kill -9"
  31. cmd_kill = f"adb -s {device} shell kill -9 {pid}"
  32. if pid:
  33. subprocess.run(cmd_kill, shell=True)
  34. # Run the command "/data/local/tmp/atx-agent server -d"
  35. cmd_start = f'adb -s {device} shell /data/local/tmp/atx-agent server -d'
  36. subprocess.run(cmd_start, shell=True)
  37. except subprocess.CalledProcessError:
  38. # Error occurred while running the command
  39. loggerKit.info("Error occurred while checking ATX server status")
  40. def start_atx_agent_all(device):
  41. try:
  42. # Run the command to change the permissions of the ATX agent file
  43. command1 = f'adb -s {device} shell chmod 775 /data/local/tmp/atx-agent'
  44. subprocess.run(command1, shell=True)
  45. # Run the command to start the ATX agent service in the foreground
  46. command2 = f'adb -s {device} shell /data/local/tmp/atx-agent server -d'
  47. subprocess.run(command2, shell=True)
  48. loggerKit.info("ATX agent service started successfully")
  49. except subprocess.CalledProcessError as e:
  50. loggerKit.info(f"{device} Error occurred while starting ATX agent service: {e}")
  51. # init atx-agent
  52. def init_axt_agent():
  53. command = "python3 -m uiautomator2 init"
  54. result = subprocess.run(command, shell=True, capture_output=True, text=True)
  55. # Access the output and error messages
  56. output = result.stdout
  57. error = result.stderr
  58. print("Output:", output)
  59. print("Error:", error)