Click here to Skip to main content
13,198,322 members (65,467 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I am running an R batch command from a website ( 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 7:04am

1 solution

Rate this: bad
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.

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
Sergey Alexandrovich Kryukov 12-Sep-12 13:15pm
Very good.
You are very welcome.
Good luck, call again.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.171020.1 | Last Updated 11 Sep 2012
Copyright © CodeProject, 1999-2017
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