The following function allows me to remotely execute Wolfram code on another machine that has wolframscript installed:

Attributes[remoteExecute] = {HoldAllComplete};
remoteExecute[(* remote_, *) expr_] := 
 Module[{compressed, ubuntuBox = RemoteConnect["", "username", "password"]}, 
  compressed = ExportString[ToString[Hold[expr], InputForm], "Base64"];
    "wolframscript -format InputForm -code 'ReleaseHold[ImportString[\
\"" <> compressed <> "\",\"Base64\"]]'", "StandardOutput"]]]

For example:

(* Return True: *)
FileExistsQ["~/only_on_my_remote_machine"] // remoteExecute

Problem: This function doesn't work on every function, however, and it is mysterious why:

How to fix?

  • $\begingroup$ What's returned by the function? If it's a String that means the encoding didn't quite go right. By the way this would be easier to write as Compress[Unevaluated[expr] and then you would execute by calling Uncompress[compressed]. I think you might be happier with that. $\endgroup$ – b3m2a1 Aug 23 at 15:49
  • $\begingroup$ b3m2a1: Is your proposed function then this one? $\endgroup$ – George Aug 23 at 17:41
  • $\begingroup$ No. Don't use ExportString and don't use ImportString or ReleaseHold. Just do something like RemoteRunProcess[ubuntuBox, TemplateApply["wolframscript -format InputForm -code 'Uncompress[\"`\`\"]'", Compress[Unevaluated[expr]]] $\endgroup$ – b3m2a1 Aug 23 at 17:43
  • $\begingroup$ I'm getting compression error output like Uncompress::corrupt: Compressed data "ïÂÂ" is corrupt and does \ not represent an expression.` $\endgroup$ – George Aug 23 at 17:49
  • 1
    $\begingroup$ b3m2a1: Removing the backslashes worked! Thanks for your help. I think you accidentally included backslashes in your formatted markdown above^. $\endgroup$ – George Aug 23 at 18:09

The following

Attributes[remoteExecute] = {HoldAllComplete};
remoteExecute[expr_] := 
   ubuntuBox = RemoteConnect[ip, "username", "password"]}, 
  compressed = Compress[Unevaluated[expr]]; 
     "wolframscript -format InputForm -code 'Uncompress[\"``\"]'", 
     compressed], "StandardOutput"]]]

is able to execute functions remotely, so long as your function isn't locally defined.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.