I'm running some R code with an R session.

I begin by opening the shell, typing in screen to begin a screen session, and then emacs -nw

Let's say I begin R with R and then run some code, e.g. > source("script.R")

How do I simply exit this session and keep this R code running? If I'm using screen and I kill the buffer with C-x k, do I kill the session?


4 Answers 4


If you close emacs, the R process will be killed. To keep it running, detach your screen session without closing emacs. I think this is bound to C-a d by default.

In your example, you start by opening screen in a terminal:


Then start emacs, using -nw to run it in text-mode:

emacs -nw

If you have ESS installed, start R via M-x R. If you're just running it in a shell (why? use ESS!) do that instead.

Now start your analysis. For example:

for(i in 1:100){

Now detach the session with C-a d. You can now close your terminal, and even log off your computer - but don't turn it off! When you want to see how your job is doing, open another terminal, call screen with the -r flag:

screen -r

And you'll see your R session, inside Emacs, is still working away:

> Sys.sleep(4)
> for(i in 1:100){
+ Sys.sleep(5)
+ print(i)
+ }
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8

As long as you leave emacs and R running, and detach (but don't close) screen, and don't turn your computer off, that's all you need.

  • How do you recommend allowing R to continue? I suppose you could continue to run emacs as a server: emacs --nw --daemon Sep 15, 2016 at 3:37
  • 1
    I don't understand. All running processes in a screen session continue when you detach it. You don't need to do anything more.
    – Tyler
    Sep 15, 2016 at 10:01

Screen is a terminal multiplexer then has a server/client model. When you start screen, it forks itself in the background to start the server and then the foreground client start communicating to it. What this does is allows it to keep running in the background no matter what happens to your client (whether it be killed, you lose connection if it's running over ssh or anything else).

To detach the screen client from the server by pressing crtl+a followed by d. You can then use screen -ls to list all running screen sessions and screen -r to attach to the last one.

However, if you are running R inside emacs inside screen, then it is up to emacs when to close the R program, screen has no say in it.

Screen also allows you to split the window and have multiple windows. You can use this instead of launching the application with emacs to keep it running even when you exit emacs.


If that is the only window you have, killing that window will also kill screen. As long as you have a window open, screen should still be running. For instance, you might be running R in another window.

But usually when you kill a window, what's in it stops also, because screen sends it a signal:

Kill current window. If there is an exec command running then it is killed. Otherwise the process (shell) running in the window receives a HANGUP condition, the window structure is removed and screen (your display) switches to another window. When the last window is destroyed, screen exits. After a kill screen switches to the previously displayed window. Note: Emacs users should keep this command in mind, when killing a line. It is recommended not to use "C-a" as the screen escape key or to rebind kill to "C-a K".

If you are running R in an Emacs buffer, then (having taken no provision for putting it into the background, and/or not using nohup) then killing the Emacs window would take R along with it.

You can of course detach from a screen session (that is different from killing a window): control+Ad

But it helps to read the manual page


Are you running a shell in Emacs? If so, you can use Unix job control to put the R script in the background: Once your R script is running, type Ctrl-C Ctrl-Z to return to the shell prompt. This will suspend your script. Then type bg to restart the job you just suspended in the background. You can see that the job is running in the background using the shell command jobs.

Another way to accomplish what you want is by starting your R script in the background from the beginning. See this link for doing it in R, or this link to do it directly from the command line.

  • 1
    Running a program in the background isn't the same as running it in a screen session. Screen sessions persist even after you log out, where background processes are killed on logout.
    – Tyler
    Sep 15, 2016 at 0:49
  • Yeah, I assumed OP wanted to stay in Emacs (my favorite pastime) as C-x k is an Emacs command -- I should have asked/waited for clarification.
    – GLRoman
    Sep 16, 2016 at 15:12

Not the answer you're looking for? Browse other questions tagged .