2024 Stata Biostatistics and Epidemiology Virtual Symposium · 22 February
|Running Stata for Unix in batch mode
|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.
Classroom and web training
Teaching with Stata
Statalist: The Stata Forum
Last updated: 16 November 2022
StataCorp LLC (StataCorp) strives to provide our users with exceptional products and services. To do so, we must collect personal information from you. This information is necessary to conduct business with our existing and potential customers. We collect and use this information only where we may legally do so. This policy explains what personal information we collect, how we use it, and what rights you have to that information.
These cookies are essential for our website to function and do not store any personally identifiable information. These cookies cannot be disabled.
Please note: Clearing your browser cookies at any time will undo preferences saved here. The option selected here will apply only to the device you are currently using.