Shell.execute with path

I’ve been struggling with this for about 3 hours now, trying various combinations of things to make this work, and it CAN’T be this difficult. I’ve gotta be missing something basic.

In short, what I want to do is shell to “python -m SimpleHTTPServer 8080” and start the Simple HTTP Server on port 8080 in the Resources folder of my application. I’d like to launch this invisibly to the user and then teminate it at the appropriate time.

The first problem is getting it to serve the correct folder. By default it seems to be serving the root of the whole drive, which is a big no-no.

So I’ve tried writing a bash .command file, running a “cd” command to switch to the correct folder, then running the python command. This works if I set the Permissions correctly and .Launch it. But then the server/console window is visible and I don’t have the pid so I can’t kill it either.

Then I tried using a Shell.Execute on the .ShellPath of the Folderitem and that doesn’t seem to work at all.

What am I missing here? Is there a better way?

For the record, I’ve also tried using the open-source XojoHTTPServer class but that seems to fall apart when there are too many simultaneous web requests (which are coming quickly because they’re from localhost and are very large media files). The python server works perfectly, however.

Indeed Shell not only starts at / but stays there. CD has no effect.

You should call Console programs with the full path. Employ a FolderItem to point to the executable, and use URLPath.

I was not awake yet this morning when I posted. Sorry. I meant ShellPath. But I see you say it does not work for you. I have never had any issue of that kind. You should verify the folderItem.Exists ; it may not be what you think it is.

The shell is sometimes a bit different from the Terminal. Have you tried to create a simple Python script with the correct shebang line and the path set and then calling it from Xojo with a shell?

Shell EXECUTE invokes the shell once with whatever command you give
And then it basically terminates
So even if you do

dim s as new shell

s.execute “cd whatever”
s.execute “python whatever”

The effect is as if in terminal you executed the command, closed it, then started a new one after each of those executes

However you can use different mode that let you write a series of commands
OR you write one really long ugly command with ; for separators and issue it once

The other thing is that all your PATHS (basically the .bashrc is NOT run)
So use full paths to executables like python

Thanks for the responses everyone - The problem is that the “python -m SimpleHTTPServer” command only serves the folder from which it was invoked. So if indeed .Execute starts from the root, that’s where it will serve from regardless of how I call it.

THIS works, but it doesn’t run invisibly and I can’t kill it when it’s done:

[code]dim fol as FolderItem
fol=app.ExecutableFile.parent.parent.child(“Resources”).child(“exec.command”)
dim ScriptS as string= “#!/bin/bash” + EndOfLine+ _
"cd " + Chr(34) + fol.parent.shellpath + Chr(34) + EndOfLine _
+ "python -m SimpleHTTPServer "+ EndOfLine

WriteToFile fol, ScriptS 'This is a function that dumps the string ScriptS to file fol
fol.permissions=&o777
fol.launch
[/code]

This next bit does not. Can anyone enlighten me as to why? It seems as if nothing executes even though PsySh.IsRunning returns true.

[code]dim fol as FolderItem, PySh as Shell
fol=app.ExecutableFile.parent.parent.child(“Resources”).child(“exec.command”)
dim ScriptS as string= “#!/bin/bash” + EndOfLine+ _
"cd " + Chr(34) + fol.parent.shellpath + Chr(34) + EndOfLine _
+ "python -m SimpleHTTPServer "+ EndOfLine

WriteToFile fol, ScriptS 'This is a function that dumps the string ScriptS to file fol
fol.permissions=&o777
PsySh.Execute fol.Shellpath
[/code]

[quote=224008:@Peter Michael]dim fol as FolderItem
fol=app.ExecutableFile.parent.parent.child(“Resources”).child(“exec.command”)
dim ScriptS as string= “#!/bin/bash” + EndOfLine+ _
"cd " + Chr(34) + fol.parent.shellpath + Chr(34) + EndOfLine _
+ "python -m SimpleHTTPServer "+ EndOfLine

WriteToFile fol, ScriptS 'This is a function that dumps the string ScriptS to file fol
fol.permissions=&o777
fol.launch[/quote]

Why don’t you execute fol in shell instead of launching it ?

or a one liner

dim ScriptS as string= “cd “”” + fol.parent.shellpath + “”"" + ; python -m SimpleHTTPServer "

Hi all,

I solved my problem with Shell.Execute after a lot of hair-pulling.

Turns out that the mode 1 python process was being killed as soon as the event handler ended because I instanced the shell class in the scope of the event handler. Adding it as an app-wide property fixed it. Ugh. Basic mistake. Thanks for the help!