Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am running an R batch command from a website (asp.net/mono) using the following ProcessStartInfo line (program and output are user dependent):

test.StartInfo = new ProcessStartInfo("/bin/sh", "/usr/bin/R CMD BATCH /usr/Scripts/program /usr/Scripts/output");

R batch will run directly from the command line or from a .sh script file using just R CMD BATCH but ProcessStartInfo seems to require the fully qualified address path.

As the location of R CMD BATCH may change between servers, is there anyway I can use ProcessStartInfo without the qualifying path variable for R CMD BATCH? Alternatively, is there anyway I can set the actual path variable dynamically?

Many thanks for any suggestions!
Posted

1 solution

Well, you need to specify the directory somehow, right? There is no such thing as a miracle. One way quite typical for Unix/Linux is putting many paths in the PATH environment variable, which I don't particularly like. Also, you need to understand, that some application can rely on the "working directory" of the application, but this is not the case on the Unix-like; the working directory is not included in the pass by default, that way many command lines look like "./application_executable_module", with "./". So you need to devise some method of setting the path to your batch file, such as set up by initial configuration or installation procedure which can prescribe the directory in some configuration file. You can find the configuration file location by the location of the main executable module of your entry assembly, that is, the executable directory. Please see the recipe in my recent answer:
How to find my programs directory[^].

Also, you normally don't need to use "/bin/sh", which is in fact just another application, one of the command interpreters. Besides, its location is always included in the PATH environment variable. When you execute a shell script, you don't normally specify it, right? And this is because the shell application is prescribed in the script/batch file itself, like in this form: "#!/bin/bash". So, you can do this instead:
C#
test.StartInfo = new ProcessStartInfo("/usr/bin/R", "CMD BATCH /usr/Scripts/program /usr/Scripts/output");


In this code line, I just copied your command line broken into the name of main executable module of your application assembly and its command line parameters, with all your absolute path. In really functional applications, there are no cases when a hard-coded path can be useful, no matter it if is relative or absolute. The path should be always calculated during run time based on some configuration file(s), application location, user "special folders", etc.

—SA
 
Share this answer
 
Comments
R.N.E. 12-Sep-12 3:55am    
I've used the corrected ProcessStartInfo code as suggested but without the hardcoded path and it works fine (presumably using the PATH environmental variable as I'd originally hoped):
test.StartInfo = new ProcessStartInfo("R", "CMD BATCH /usr/Scripts/program /usr/Scripts/output");
I think I must have copied and pasted the original version from somewhere without properly understanding what I was doing, which is always unwise! Anyway, I am currently trying to accept this as the solution.
Many thanks
RNE
Sergey Alexandrovich Kryukov 12-Sep-12 13:15pm    
Very good.
You are very welcome.
Good luck, call again.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900