Running Scripts: A Linux Cheatsheet

Card image cap

Running Scripts: A Linux Cheatsheet

Fri Feb 22, 2019

When running scripts on Linux, there are a few standard procedures you will likely want to use. I’ve outlined a quick cheat sheet here of some of the standard commands you may want to use.

Script output to files

Using the example command node myscript.js and the output file ~/myscript.log here are some handy commands.

Appending stdout to a file (not to the screen)

node myscript.js >> ~/myscript.log

Appending stdout & stderr to a file (not to the screen)

node myscript.js &>> ~/myscript.log

Appending stdout to a file and the screen

node myscript.js >&1 | tee -a ~/myscript.log

Appending stdout & stderr to a file and the screen

node myscript.js 2>&1 | tee -a ~/myscript.log

Writing stdout to a new file (not to the screen)

node myscript.js > ~/myscript.log

Writing stdout & stderr to a new file (not to the screen)

node myscript.js &> ~/myscript.log

Writing stdout to a new file and the screen

node myscript.js >&1 | tee ~/myscript.log

Writing stdout & stderr to a new file and the screen

node myscript.js 2>&1 | tee ~/myscript.log

Screen

If you’re expecting a script to run for a while, you will want to run it in screen in cases of session disconnections or terminal window closures.

Create a new screen session

screen

List all running screen sessions

screen -ls

Reattaching to your last screen

screen -r

Reattaching to a specific screen

screen -r <session-name>

Detaching from a screen

# from within a screen session

"Ctrl-a” + “d”

Killing a screen session

# from within a screen session

“Ctrl-a” + “k"

Please report any errors or requests for improvements by creating a PR on GitHub at: running-scripts-cheatsheet or by hitting me up on Twitter at @tlef.