Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Silent or minimized version of shell and winexec

    Dear all,

    I find myself running a rather long loop which also calls shell multiple times in Stata 15.1 on Windows. Consequently, I have blue cmd windows popping up regularly, "stealing" the focus of the system, stopping me from doing other things (at least without disturbance). I am aware of the difference between shell and winexec (stata waits for the shelled command to complete or not), but that's not really useful here as they both "steal" the systems focus.

    Is there a way to call shell and winexec in a minimized or silent manner so that those tasks are carried out in the background (but still complete prior to returning to Stata in the case of shell)? I have googled around but not been able to find anything.

    An additional, possibly relevant point is that sometimes in my scripts the shell prompt should complete prior to stata continuing (i.e. shell is required and not just winexec, although a solution for both would be ideal), although not always (so sometimes I use shell and other times winexec).

    Any help would be much appreciated!

    Alan

  • #2
    I think good advice requires a better idea of what you're trying to do.

    For example, I find that many users are unaware that official commands like copy erase and mkdir are available to do things through Stata that otherwise the OS might be fired up to do directly.

    Another territory includes the scope to write Stata commands for what you're doing or to link with external programs. A very simple example is that I have a Stata command that calls up my favourite text editor. Within the program it is shelling out and firing up another executable but doing so is essentially painless for the user. It certainly beats my typing out the full path of the executable (not that there aren't other ways to do it).

    Very likely, neither of those is what you're doing at all but the point remains that there's unlikely to be a solution in abstraction.

    Comment


    • #3
      Thanks for replying so quickly Nick. My use case doesn't fall into either of the examples you make unfortunately.

      Briefly, I am calling R via shell to run a script that plots some maps. I'm aware that there is some basic map plotting capabilities in Stata, but the flexibility of R for this is too much to give up.

      Any thoughts? I guess your comment about "unlikely to be a solution" is probably the (disappointing) answer...

      Comment


      • #4
        Hi Alan. There are some user-written commands/programs for calling R from within Stata. Perhaps one of them would work for you. See this old thread: And try these two commands:
        Code:
        findit rsource
        findit rcall
        HTH.
        --
        Bruce Weaver
        Email: bweaver@lakeheadu.ca
        Web: http://sites.google.com/a/lakeheadu.ca/bweaver/
        Stata version: 15.1 IC (Windows)

        Comment


        • #5
          Alan - When I'm running shell scripts on Windows (usually for typesetting LaTeX docs in a loop) I write my shell commands to a .bat text file with some options to minimize the cmd windows to prevent it from popping up. A downside is that you wont see when there are errors, so I turn this off until I know everything is running smoothly.
          Here's an example of how I typeset that you could adapt/improve :

          Code:
               loc f  "myfile"
                  cap rm `"`f'.pdf"
                  !taskkill /f /im "Acrobat.exe"   /t
            * old version:
              *    !pdflatex  --shell-escape -no-file-line-error "`results_tex'/`f'.tex"
              *    !pdflatex   --shell-escape  -no-file-line-error "`results_tex'/`f'.tex"
                   
                   
                   *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
                    **write batch file command w/  /min  opt and then run it:
                    cap file close compilelatex
                    file open compilelatex using "`results_tex'/compilelatex_`f'.cmd" , write replace text
                    file write compilelatex `" if not DEFINED IS_MINIMIZED set IS_MINIMIZED=1 && start "" /min "%~dpnx0" %* && exit "' _n
                              file write compilelatex `"pdflatex   --shell-escape  -no-file-line-error "`results_tex'/`f'.tex""' _n
                    file write compilelatex `"pdflatex   --shell-escape  -no-file-line-error "`results_tex'/`f'.tex""' _n
                    file write compilelatex "exit" _n
                    file close compilelatex
                    shell start /min  `results_tex'/compilelatex_`f'.cmd ^& exit
                    shell start /min  `results_tex'/compilelatex_`f'.cmd ^& exit
                    *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
                                                      
                  di as smcl `"Click to open directory: {browse `"`results_tex'"'}"'
                    di as smcl `"CLICK TO OPEN FILE: {browse `"`results_tex'/`f'.pdf"'}"'
                  
                       if   `"`open'"' == "open"   {
                          shell start  "Acrobat.exe" "`results_tex'/`f'.pdf"
                          }
          Eric A. Booth
          Senior Research Scientist, Gibson Consulting Group

          eric.a.booth@gmail.com | http://www.eric-a-booth.com

          Comment


          • #6
            Thanks for the tip Bruce, both rcall and rsource are wrappers for shell though, so they still open the cmd window and take the system focus. I also came across another in the runmlwin package (runmlwin_qshell), but in this case it behaves the same as shell (it's undocumented, but when inspecting the ado file, i notice that it reverts to shell if there is an error when a plugin runs, so maybe the syntax isn't quite correct - I pass three or four options to R, maybe it only handles one...)

            Thanks for your code too Eric... it looks like it might be adaptable... I had briefly experimented with the /min flag but that seemed to terminate the process without doing anything... I tend to use relative paths in my R scripts, do you have any idea whether the batch file still executes in the same directory as stata's current working directory?

            Comment


            • #7
              Hi Alan.
              To my understanding, what you mention is more an issue of the Windows default shell (cmd.exe) than of Stata itself; or, maybe, an issue of Stata's interaction with the operating system's default shell. Under Linux and macOS, when using Stata's shell command with any arguments, no extra window appears. Instead, the results of all executed shell commands are redirected to Stata's results window.

              Windows' default shell, though, does not support this. When auto-creating LaTeX files from within Stata, I ended up to use an approach quite similar to eric_a_booth's.

              However, I wanted to add something: You can tell Stata to use Windows Powershell instead of the old cmd.exe. Despite the fact that PowerShell is more elaborate and, pun intended, powerful, it has a command line switch that gives you control over the window to be used: It can be minimized from the start, or completely "hidden".

              In Stata, use
              Code:
              global S_SHELL "PowerShell.exe -WindowStyle Minimized"
              to let the shell command use a minimized PowerShell window. This, in my setup, minimized the time slot where the focus-stealing Window appears to a split second. I guess this is the best you can get in Windows.

              You can also try "Hidden" instead of "Minimized", but be warned: This will break things when you invoke shell without arguments, as the (hidden) PowerShell process won't stop by itself, but Stata is waiting for it.

              Regards, and I hope this helps a little
              Bela

              PS: Maybe PowerShell supports output redirection in the same way as Linux/macOS shell programs do. If so, integrating PowerShell as the default shell under Windows systems where it is available (i.e. all Windows versions since Vista) could be a point for the Wishlist for Stata 16, don't you all think?
              Last edited by Daniel Bela; 12 Oct 2018, 05:49.

              Comment


              • #8
                Originally posted by Daniel Bela View Post
                To my understanding, what you mention is more an issue of the Windows default shell (cmd.exe) than of Stata itself; or, maybe, an issue of Stata's interaction with the operating system's default shell. Under Linux and macOS, when using Stata's shell command with any arguments, no extra window appears. Instead, the results of all executed shell commands are redirected to Stata's results window.

                Windows' default shell, though, does not support this. When auto-creating LaTeX files from within Stata, I ended up to use an approach quite similar to eric_a_booth's.

                However, I wanted to add something: You can tell Stata to use Windows Powershell instead of the old cmd.exe. Despite the fact that PowerShell is more elaborate and, pun intended, powerful, it has a command line switch that gives you control over the window to be used: It can be minimized from the start, or completely "hidden".

                In Stata, use
                Code:
                global S_SHELL "PowerShell.exe -WindowStyle Minimized"
                to let the shell command use a minimized PowerShell window. This, in my setup, minimized the time slot where the focus-stealing Window appears to a split second. I guess this is the best you can get in Windows.

                You can also try "Hidden" instead of "Minimized", but be warned: This will break things when you invoke shell without arguments, as the (hidden) PowerShell process won't stop by itself, but Stata is waiting for it.

                Indeed, this is the Windows issue and not Stata. As far as I know, your solution - changing the S_SHELL to powershell - will still brings about a flashing powershell window (instead of the cmd.exe window). There is even a thread about it in the powershell repository on GitHub. If you have managed to call powershell without the flashing window, please kindly provide a complete example.
                ——————————————
                E. F. Haghish, IMBI, University of Freiburg
                haghish@imbi.uni-freiburg.de
                http://www.haghish.com/

                Comment

                Working...
                X