Wilhel

Wilhel

Command Line Environment

Command Line Control#

Ending Processes#

  • SIGINT: Ctrl-C
  • SIGQUIT: Ctrl-\
  • SIGTERM: kill -TERM <PID>
#!/usr/bin/env python
import signal, time

def handler(signum, time):
    print("\nI got a SIGINT, but I am not stopping")

signal.signal(signal.SIGINT, handler)
i = 0
while True:
    time.sleep(.1)
    print("\r{}".format(i), end="")
    i += 1

Pausing and Running Processes in the Background#

  • SIGSTOP: Ctrl-Z
  • fg: continue running in the foreground
  • bg: continue running in the background
  • jobs: list all unfinished tasks in the current terminal session
    • pid references the task, pgrep finds the pid
    • %+job number selects the task, jobs will print the job number
    • $! the most recent job
  • &: command suffix to run directly in the background
  • nohup: a wrapper that ignores SIGHUP, closes the terminal, and allows the program to continue running in the background
  • disown: for already running programs, allows them to continue running even after closing the terminal

Terminal Multiplexing#

tmux#

Based on panels and tabs, it splits the terminal window into multiple sections, allowing interaction with multiple shell sessions simultaneously, and can detach the current terminal session and reconnect in the future.

Session-Workspace, containing one or more windows#

  • tmux start a new session
  • tmux new -s NAME start a new session with a specific name
  • tmux ls list all current sessions
  • <C-b> d detach the current session
  • tmux a reconnect to the last session
    • -t specify a specific session

Window-Tab, visually dividing the session into multiple parts#

  • <C-b> c create a new window, <C-d> close
  • <C-b> N jump to the Nth window
  • <C-b> p switch to the previous window
  • <C-b> n switch to the next window
  • <C-b> , rename the current window
  • <C-b> w list all current windows

Panel-Like splitting in vim, panels allow us to display multiple shells in one screen#

  • <C-b> " split horizontally
  • <C-b> % split vertically
  • <C-b> <direction> switch to the specified direction of the panel
  • <C-b> z toggle zoom for the current panel
  • <C-b> [ start scrolling the screen back. Press space to start selecting, and press enter to copy the selected part
  • <C-b> <space> switch between different panel layouts

Further Reading#

Here is a quick start guide to tmux, and this article is more detailed, including the screen command. You may also want to learn the screen command, as it is installed by default on most UNIX systems.

Exercises#

Task Control#

  1. We can use commands like ps aux | grep to get the pid of a task, and then you can end these processes based on the pid. But we actually have a better way to do this. Execute the command sleep 10000 in the terminal. Then use Ctrl-Z to switch it to the background and use bg to continue allowing it to run. Now, use pgrep to find the pid and use pkill to end the process without manually entering the pid. (Hint: use the -af flag).

    sleep 10000 &
    pgrep sleep 
    pkill -f sleep
    
  2. If you want to start another process after a certain process ends, how would you do it? In this exercise, we use sleep 60 & as the first program to execute. One way is to use the wait command. Try starting this sleep command and then wait for it to finish before executing the ls command.

    sleep 60 &
    pgrep sleep | wait; ls
    

    However, if we operate in different bash sessions, the above method will not work. Because wait only works for child processes. One feature we haven't mentioned before is that when the kill command exits successfully, its exit status is 0, and other statuses are non-zero. kill -0 does not send a signal, but returns a non-zero status code when the process does not exist. Please write a bash function pidwait that takes a pid as an input parameter and waits until the process ends. You need to use sleep to avoid wasting CPU performance.

    pidwait()
    {
        while kill -0 $1 # loop until the process ends
        do
    	    sleep 1
        done
        ls
    }
    

Terminal Multiplexing#

  1. Please complete this tmux tutorial and refer to these steps to learn how to customize tmux.

tmux source-file ~/.tmux.conf to make the configuration file take effect

Aliases#

  1. Create an alias dc that allows the cd command to be executed correctly even if it is mistakenly entered as dc.
  2. Execute history | awk '{$1="";print substr($0,2)}' | sort | uniq -c | sort -n | tail -n 10 to get your top ten most used commands and try creating aliases for them.
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.