Skip to content

Commit

Permalink
Start cleanup process with setsid or nohup
Browse files Browse the repository at this point in the history
If available, start the cleanup process in a new session to reduce the
likelihood of it being terminated along with the scrcpy server process
on some devices.

The binaries setsid and nohup are often available, but it is not
guaranteed.
  • Loading branch information
rom1v committed Dec 5, 2024
1 parent a50564f commit cfb6683
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions server/src/main/java/com/genymobile/scrcpy/CleanUp.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
* Handle the cleanup of scrcpy, even if the main process is killed.
Expand Down Expand Up @@ -107,16 +109,22 @@ private void runCleanUp(Options options) {

private void run(int displayId, int restoreStayOn, boolean disableShowTouches, boolean powerOffScreen, int restoreScreenOffTimeout)
throws IOException {
String[] cmd = {
"app_process",
"/",
CleanUp.class.getName(),
String.valueOf(displayId),
String.valueOf(restoreStayOn),
String.valueOf(disableShowTouches),
String.valueOf(powerOffScreen),
String.valueOf(restoreScreenOffTimeout),
};

List<String> cmd = new ArrayList<>();
if (new File("/system/bin/setsid").exists()) {
cmd.add("/system/bin/setsid");
} else if (new File("/system/bin/nohup").exists()) {
cmd.add("/system/bin/nohup");
}

cmd.add("app_process");
cmd.add("/");
cmd.add(CleanUp.class.getName());
cmd.add(String.valueOf(displayId));
cmd.add(String.valueOf(restoreStayOn));
cmd.add(String.valueOf(disableShowTouches));
cmd.add(String.valueOf(powerOffScreen));
cmd.add(String.valueOf(restoreScreenOffTimeout));

ProcessBuilder builder = new ProcessBuilder(cmd);
builder.environment().put("CLASSPATH", Server.SERVER_PATH);
Expand Down

0 comments on commit cfb6683

Please sign in to comment.