Why does my Execute hang?

Noob here…

Trying to run the below code, and it starts the process but then the app hangs until i terminate it via a kill command in the console. What am I doing wrong?

If TargetLinux Then
     Var sh as New Shell
     sh.ExecuteMode = Shell.ExecuteModes.Synchronous
     sh.Execute("/usr/bin/udiskie", " &")
End If

The ampersand parameter tells bash to run the command in the background and continue. I have tried it as part of the command line, and also as a parameter above. No change.

The code compiles, runs, then hangs. I kill the process ID for udiskie and my app comes back to life.

Synchronous
(Default) The shell executes its command and returns the result in the Result property when the script has finished running. Synchronous shells block the main UI thread, even when they are in a thread themselves. For long-running shell processes, use one of the other shell modes instead.

I agree that the " &" should background the udiskie task, and the shell should end. Maybe there’s a bug on Linux? Perhaps you should try Asynchronous instead?

Edit to add:
Could the “sh” object going out of scope be an issue? Maybe try making the “sh” object a global variable and see if that helps?

1 Like

Oh wait, I think this is the problem:
sh.Execute("/usr/bin/udiskie", " &")

You are executing the command:
"/usr/bin/udiskie"
with one parameter
" &"

Try this instead:
sh.Execute("/usr/bin/udiskie &")

thanks.

This change made no difference. I’ll try a global variable

Shell using Synchronous mode keeps waiting for the process to end. Since the process is still running, it hasn’t ended and Xojo still waits. You can find details on the specific mode behaviors in the Xojo documentation: Shell — Xojo documentation

Agree, but the ampersand should solve this. It is the only way to run the command and return control to the console, otherwise it sits and waits.

The Xojo Shell object is not your command line access environment. It behaves differently than Konsole or Terminal.app. Synchronous mode is waiting for the process to end.

I urge you to investigate one of the other Shell mode options, because you are trying to solve this the wrong way.

4 Likes

So, I don’t have Linux to test with, but I tried using “&” on macOS, and it doesn’t work as I expected, and likely won’t work on Linux either.

I think the answer really is to use Asynchronous or Interactive modes.

thanks. I’ve been playing around with the 3 modes and the only one that actually launches the external process is Synchronous, but then it locks the UI.

After a rethink, I came up with this as the command line: /usr/bin/udiskie &> /dev/null & disown"

and this works as expected and with Synchronous mode as well.

thanks for all the help

Clever! I’m not sure what speceific event triggers the Xojo shell to know “this shell has terminated” but sounds like this hack does the goal.

That sounds to me like the interactive or asynchronous Shell instance is going out of scope and being destroyed? In that case, your command probably is being invoked, but is killed off.

command stays running in the background even after program exit

Wouldn’t be

sh.Execute("nohup /usr/bin/udiskie &")

A better way?

see above, @Tim_Parnell message gave me the clue that this won’t work. I was treating Execute as a full konsole session in my head, but it isn’t.

I’ve tried. You are correct.