The screen command can be useful if you have a long running R script.

As explained here, you can run an R script in a bash shell like this:

Rscript some_script.R |& tee -a some_script.log

With the screen command however, it is possible to start a long running R script in an interactive R session, detach the session, log out from your computer, log in later, re-attach the R session and continue working.

Here is how you can do it:

# start a named screen session
screen -S long_running_r_script

#-------------------------------------------------------------------------------
# in another terminal: list screen sessions
screen -ls
#There is a screen on:
#  10159.long_running_r_script	(21.02.2022 22:26:12)	(Attached)

# rename an existing screen session:
screen -S  10159.long_running_r_script -X sessionname not_so_long_running_r_script
# check that the renaming worked:
screen -ls
#There is a screen on:
#  10159.not_so_long_running_r_script	(21.02.2022 22:26:12)	(Attached)

# kill ALL screen sessions:
screen -X quit
# check that there is no screen session left:
screen -ls
# No Sockets found in /run/screen/S-aa.

#-------------------------------------------------------------------------------
# start a named screen session again
screen -S long_running_r_script

# start a long running R script
R
Sys.sleep(1000)

# detach this screen session: CTRL+a d (the R processes continues running)

# To create a new screen window, press CTRL+a followed by c
# To list all open windows, press CTRL+a followed by “
# To rename the current windows, press CTRL+a followed by A
# To split the screen horizontally, press CTRL+a followed by S
# To toggle between the split windows, press CTRL+a followed by tab.
# To split the screen vertically, press CTRL+a followed by |

# To quit the current screen session: CTRL+a followed by k
# To detach from the screen session, press CTRL+a followed by d
# To resume a specific screen session: screen -r <the name>

# If you have opened one too many regions, you can close just one region and 
# leave the others as is. Switch to the region you want to close with Ctrl-a TAB 
# and then hit Ctrl-a X (upper case X).

 

If you can't resume screen because it's already attached, you can use this:

screen -rd <session name>

where 'd' stands for 'detach', i.e. first detach the session from every terminal it was attached to then resume it.


When screen is invoked, it executes initialization commands that are contained in  two files "/etc/screenrc"  and then ".screenrc" in the user's home directory

Here is a simple .screenrc example:

# override the default 100 lines of scrollback
defscrollback 5000

# enable logging (take care, files might become huge)
deflog on
logfile /tmp/testing_screen.log

 

https://xkcd.com/627/

xkcd-tech-support-cheat-sheet

 

Make a promise. Show up. Do the work. Repeat.