Home  /  Resources & support  /  FAQs  /  Running Stata for Unix in batch mode

How do I run Stata in batch mode?

Title   Running Stata for Unix in batch mode
Author Alan Riley, StataCorp

First, you should become familiar with do-files. Read [U] 16 Do-files and [R] do.

Now that you know what do-files are and how to run them interactively from within Stata, you can also use them to run background Stata batch jobs in Unix. There are two ways to do this:

        stata -b do filename &

               or

        stata < filename.do > filename.log &

The first tells Stata to run in batch mode. Stata will execute the commands in filename.do and will automatically save the output in filename.log.

The second uses the Unix input redirection symbol to send each line in filename.do to Stata, just as if you typed that line interactively. Any output is redirected to filename.log.

The “&” at the end of each of the above examples tells Unix to run the command in the background, freeing up your Unix prompt.

Warning: In the second method, Stata does not know the commands it receives come from a file. As far as Stata is concerned, you are typing every command in filename.do on the Stata command line. If your do-file contains either the #delimit command or the comment characters (/* at the end of one line and */ at the beginning of the next), the second method will not work. We recommend that you use the first method: stata -b do filename &.

Finally, you may wish to start a batch Stata job, log off from your terminal, and log back in later to retrieve the output. To do this, you should preface either of the above methods with nohup

        nohup stata -b do filename & 

This command will prevent your Stata job from being killed when you log off. Type man nohup at your Unix prompt for more information.