Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Mono C#
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 11-Sep-12 8:04am
R.N.E.204

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
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
  Permalink  
Comments
R.N.E. at 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 at 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)

  Print Answers RSS
0 Mathew Soji 334
1 Maciej Los 295
2 BillWoodruff 290
3 Afzaal Ahmad Zeeshan 257
4 DamithSL 225
0 OriginalGriff 6,369
1 Sergey Alexandrovich Kryukov 5,973
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,045


Advertise | Privacy | Mobile
Web03 | 2.8.1411019.1 | Last Updated 11 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100