📗
Essential Python For Genome Science
  • Before Start
  • Chapter Contents
  • Prerequisites
    • About the UNIX system
    • About python
  • UNDERSTAND RAW DATA
    • Stages of Genome Data Generation
    • From Bulk To Single Cell
    • Introduction To the Datasets
      • bulk RNA-seq
      • single-cell data
  • Work Environment
    • Chapter Ensemble
    • All About Installations
    • Keep Running
    • Coding Environment
    • Git and Github
    • Other Tips
  • Python and UNIX System
    • Run Python
    • File I/O
    • Run Shell Command In Python - I
    • 🎉Case Study: Mapping bulk RNA-seq reads with salmon
  • Data Cleaning
    • 🎉Key Concept of Pandas
    • 🎉Case Study: Aggregate Salmon Quant
    • Case Study: Exploring The Dataset 🚩
    • The "copy" and "inplace" Parameter 🚩
    • Case Study: Extract and Reformat GTF file 🚩
    • the correct vs. the wrong way of using pandas 🚩
    • Case Study: Bulk Sample PCA 🚩
  • PYTHON BASICS
    • Python can be lightning-fast ⚡️ 🚩
    • Run Shell Command In Python - II 🚩
    • Pointers In Python 🚩
    • Everything is an object 🚩
    • Thread and Process 🚩
    • Resource For Intermediate Python Knowledge 🚩
    • Python magic method 🚩
  • Genome Science Data
    • NGS Data Formats and Tools 🚩
      • SAM/BAM 🚩
      • BED 🚩
      • GTF 🚩
      • Bigwig / Bigbed 🚩
      • VCF / BCF 🚩
    • The Python Packages 🚩
  • Data visualization
    • Matplotlib Basics 🚩
    • Seaborn Basics 🚩
    • Interactive Data Visualization 🚩
  • Use R in Python
    • Why? 🚩
    • rpy2 🚩
  • Gotchas
    • Check whether package X is installed
    • BAM to FASTQ
    • Genomic Websites
Powered by GitBook
On this page
  • SIGHUP
  • Run with nohup and &
  • Screen

Was this helpful?

  1. Work Environment

Keep Running

How to keep your command running?

PreviousAll About InstallationsNextCoding Environment

Last updated 5 years ago

Was this helpful?

Here I introduce two ways to keep your command running in the background. You need this when:

  • Run a command (e.g., downloading a large file) that takes a long time to finish.

  • Run a command in the background for a long time, for example, a .

SIGHUP

When you execute a command in the terminal, it will block other commands until it finishes. If you close the terminal during the execution, the command will terminate also. Because a will be sent to that process (the executing program), and terminate it. Below prevent this from happening even you close the terminal.

Run with nohup and &

nohup is a simple way to keep your command running, it means "no SIGHUP", so if you use nohup in front of a command, closing terminal will not terminate the command.

"&" means put the process will be put in the background, so it will not block terminal, and you can execute other commands in this same terminal.

Check out fg, bg , and jobs command again .

# Use the sleep command to mimic a long execution time.
$ sleep 100
# This will block terminal for 100s. 
# Close terminal also terminates this.

$ nohup sleep 100
# This will still block terminal for 100s. 
# But close terminal will not terminate this.

$ nohup sleep 100 &
# This will not block terminal. 
# Close terminal will not terminate this.

$ jobs
[1]  + running    nohup sleep 100
# Use jobs command, you can see this job right now run in the background

$ fg
[1]  + 5741 running    nohup sleep 100
# Use fg, we move the command back to the foreground
# It blocks the terminal again
# If I press Ctrl+Z in here:
^Z
[2]  + 5741 suspended  nohup sleep 100

# it says this job is suspended
# use bg now can put it back to the background and start running
$ bg
[2]  - 5741 continued  nohup sleep 100

Screen

screen is an even better way. It can create named terminal screens, which will keep running independently to your terminal. Here are a few examples

# Create a screen called jupyter
$ screen -R jupyter

# when executed above command, the terminal seems refreshed, 
# this is because you entered a new screen

# now I start a jupyter notebook server (explained in another page)
$ jupyter notebook --ip=localhost --port=8888
(skip output)
# this will keep running to provide me the jupyter notebook service

# now let's exit screen
# press ctrl+A first, then press d
# ctrl+A enters screen control, d means detach
^Z
d

# now terminal change back, and I saw this:
$ screen -R jupyter
[detached]

# Jupyter is still running!

# we can check all running screens
# we can open many screens
$ screen -ls
There is a screen on:
	5892.jupyter	(Detached)

# we can go back to screen by name or id at any time
$ screen -r jupyter

# now terminal change to the jupyter one

I will leave other usages of screen as homework, you can quickly learn this from google and youtube since you now know what to search 😏.

  • How to terminate a screen?

  • What's other options to control a screen when you press Ctrl+A in a screen?

jupyter notebook server
SIGHUP
here