I have this function which is trying to restart a genymotion device in python:
def restart_device(d, stop_timeout=40, start_timeout=30):
"""Restarts a specific Genymotion device with timeouts and logs the process."""
try:
device_name = get_device_name_by_ip(d.serial)
logging.info(f"Stopping device: {device_name}")
subprocess.run([gmtoolPath, "admin", "stop", device_name], check=True, timeout=stop_timeout)
time.sleep(5) # Wait a bit before restarting
logging.info(f"Starting device: {device_name}")
subprocess.run([gmtoolPath, "admin", "start", device_name], check=True, timeout=start_timeout)
# Wait for the device to boot up
logging.info(f"Waiting for device {device_name} to boot...")
time.sleep(30) # Adjust the wait time as needed
logging.info(f"Device {device_name} restarted successfully.")
except subprocess.CalledProcessError as e:
logging.error(f"Error restarting device {device_name}: {e.stderr}")
except subprocess.TimeoutExpired as e:
logging.error(f"Timeout expired while restarting device {device_name}: {e}")
When I run the it throw the code its returning timeout but when I run the stop and start commands manually with the termianl its working perfectly.
Also an important note, I have another computer and when I run the exact same code its working fine on it.
Any help will be helpfull :)
I have this function which is trying to restart a genymotion device in python:
def restart_device(d, stop_timeout=40, start_timeout=30):
"""Restarts a specific Genymotion device with timeouts and logs the process."""
try:
device_name = get_device_name_by_ip(d.serial)
logging.info(f"Stopping device: {device_name}")
subprocess.run([gmtoolPath, "admin", "stop", device_name], check=True, timeout=stop_timeout)
time.sleep(5) # Wait a bit before restarting
logging.info(f"Starting device: {device_name}")
subprocess.run([gmtoolPath, "admin", "start", device_name], check=True, timeout=start_timeout)
# Wait for the device to boot up
logging.info(f"Waiting for device {device_name} to boot...")
time.sleep(30) # Adjust the wait time as needed
logging.info(f"Device {device_name} restarted successfully.")
except subprocess.CalledProcessError as e:
logging.error(f"Error restarting device {device_name}: {e.stderr}")
except subprocess.TimeoutExpired as e:
logging.error(f"Timeout expired while restarting device {device_name}: {e}")
When I run the it throw the code its returning timeout but when I run the stop and start commands manually with the termianl its working perfectly.
Also an important note, I have another computer and when I run the exact same code its working fine on it.
Any help will be helpfull :)
Share Improve this question asked Feb 16 at 18:23 Achiya Ben NatanAchiya Ben Natan 133 bronze badges1 Answer
Reset to default 0Try adding shell=True
to the arguments in your subprocess.run()
calls:
subprocess.run([gmtoolPath, "admin", "stop", device_name], check=True, timeout=stop_timeout, shell=True)
and
subprocess.run([gmtoolPath, "admin", "start", device_name], check=True, timeout=start_timeout, shell=True)
By default, subprocess basically uses its own shell to run commands, but sometimes that means it works a bit differently than the one your computer uses normally. Adding shell=True
has it use the OS's normal shell instead, and since the commands work normally when you use the normal shell that should fix it.