Scrcpy works when using the terminal, but not in a shell script

Hi everyone,
I try to mirror my phone to the desktop via scrcpy. This works quite fine, when entering the commands in terminal. After preparing adb for a tcpip connection (adb tcpip 5555, then adb connect 192.168.1.14:5555), I start scrcpy with the following command via terminal:

scrcpy -e -b 20M -S

And everything works just fine. BUT if I put this command in an executable shell script:

#!/bin/bash

echo "Opening TCP IP Port for adb. Device must be attached via USB for first time."
adb tcpip 5555
echo "Rooting adb to Android Device via WLAN - make sure, MAC and IP is static!"
adb connect 192.168.0.14:5555
echo "Starting scrcpy"
scrcpy -e -b 20M -S
sleep 30

The scrcpy command replies “ERROR: Server connection failed”

Anyone knows, why this happens? I am a bit confused. The script invokes literally the exact commands, I enter manually…

Hi @Knecker,

Try changing the script’s perspective and telling it what display to use, don’t let it auto-detect, by changing the executable to:

DISPLAY=:0 scrcpy -e -b 20M -S

I don’t know if this’ll work, but it’s worth a shot.

1 Like

You can try this:

#!/bin/bash

echo "Opening TCP IP Port for adb. Device must be attached via USB for first time."
adb start-server
adb -s DEVICE_ID tcpip 5555
echo "Rooting adb to Android Device via WLAN - make sure, MAC and IP is static!"
adb connect DEVICE_IP:5555
echo "Starting scrcpy"
scrcpy -s DEVICE_IP:5555 -e -b 20M -S
sleep 30

Also see scrcpy_script/Wireless Mirror.bat at master · Blakenidby/scrcpy_script · GitHub

1 Like

Thanks, just tried it out, still the same error unfortunately. Was worth a shot.

This results in

ERROR: At most one device selector option may be passed

I do believe, it might has something to do with this: When entering the command manually, this is the output:

scrcpy 1.23 <https://github.com/Genymobile/scrcpy>
/usr/share/scrcpy/scrcpy-server: 1 file pushed, 0 skipped. 336.6 MB/s (41123 bytes in 0.000s)
[server] INFO: Device: motorola moto g 5G plus (Android 11)
INFO: Renderer: opengl
INFO: OpenGL version: 4.6.0 NVIDIA 510.73.05
INFO: Trilinear filtering enabled
INFO: Initial texture: 1080x2520
[server] INFO: Device screen turned off

Might it be, that the script somehow does not know the path to “/usr/share/scrcpy/scrcpy-server”? I hope I make myself understandable…