compsci-missing_semester_2020/_subsections/lesson-05.org
2025-08-13 19:12:46 +03:00

9 KiB
Executable file

Lesson 05 | Command line Environment

notes

topics

  • job control
  • terminal multiplexers
  • dot files / how to configure
  • work with remote machines

job control

sleep

  • puts process to sleep for a specified amount of time
  • exit by typing Ctrl-C

Ctrl-C

  • sends SIGINT - signal interrupt
  • man signal will show the list of signals

signals

SIGQUIT
signal sent when we want to quit
SIGTERM
similar to quit but not in term
SIGHUP
when we have processes running but still want to close

pausing

SIGSTOP
pause execution of program
SIGCONT
continue program

python library

  • use import signal
import signal
def handler(signum, frame):
    printf("signal received")

signal.signal(signal.SIGINT, handler)
  • the above would catch a Ctrl-C and run it through handler
  • you use this by catching a Ctrl-C and running any saves or cleanup before exiting

nohup and jobs

  • allows for a process to continue livign when the terminal is killed

example

  • sleep put the process to sleep
  • '&' puts the process in the background

    • wont take over the process
nohup sleep 2000 & python somprog.py

jobs

  • jobs

will show the suspended and running processes in background

  • how to restart a suspended process

    • type 'jobs' to see what there is in this terminal
    • the find the jobs number, ie [1] or [2]
    • use the 'bg' command

      bg %1

kill command

  • allows you to send any kind of signal to a job
  • e.g. "stop" will suspend but not close the job

some commands

HUP
Hang Up
STOP
suspend
KILL
kill it no matter what

Terminal Multiplexer

overview

  • tmux
  • lets you create workspaces that you can work in
  • rearrange the environment
  • have different settings

core concepts

  • hierarchy

    • sessions
    • windows
    • panes

dot files

alias

  • remap a source series of characters to a longer series
  • alias takes a single argument, ie, do not use spaces
  • to show what an alias refers to pass the alias to the alias command

    • e.g.
~/somedirectory >>> alias myaliasedcommand
myaliasedcommand='ls -lah'
examples
  • make an alias "ll" for a list command with flags
alias ll="ls -lah"
  • make an alias for git status
alias gs="git status"
  • alias to prompt for an override
alias mv="mv -i"

text based configuration 'dot files'

  • called 'dot files' because they start with a dot

    • .bashrc

      • must be in the home directory
    • PS1

      • prompt variable for your prompt
      • you can set it in the .bashrc file
      • PS1=" >> "
    • .vimrc

      • vim configuration file

tmux

  • run a session, detach and get back to it later

prefix key

Ctl-B

userful commands

detach / attach
  • Ctl-B , D -> detach
  • tmux a -> reattach
reattach to most recent session
  • tmux a
attach to a particular session
  • list the tmux session
  • note the index number or name
  • attach to that index -t (target) ~tmux a -t [INDEX]
kill
kill a session

tmux kill-session -t [INDEX]

kill session from list windows
  • go into list windows C-b w
  • highlight the one you want dead
  • C-b x, it will ask for confirmation
kill ALL sessions

tmux kill-server

kill a pane

C-b x

kill an entire window

C-b &

tmux names session
  • tmux new -s name
list session
  • tmux ls

3 layers

layer 1 -> sessions
  • when you hit tmux you start a new tmux session
layer 2 -> window
  • when you create a new session, you automatically create an initial window
  • C-b c :: create a new window in addition to the existing ones

    • all windows will be listed in the display line at the bottom
    • the * indicates the current
  • C-b n :: move through your windows
  • C-b < :: rename window
  • C-b w :: list all sessions and windows

    • you can walk through the list using arrow keys
    • enter to switch
kill session
  • go into list windows C-b w
  • highlight the one you want dead
  • C-b x, it will ask for confirmation
layer 3 -> panes
  • panes exist in windows
  • add horizontal pane :: C-b %
  • add vertical pane :: C-b "
  • switch panes :: C-b [arrow]
  • show pane index :: C-b q
  • switch to pane index :: C-b q [index]
  • change pane size :: C-b [hold Ctl] [arrow]
preselected pay layout
  • C-b Alt-1-5

*

terminal emulator - alacritty

  • very fast
  • useful for heavy displays like neoviim
  • vm mode is good for reloading

hint system

  • pattern recognition of terminal output based on regex
  • execute commands on those patterns
e.g.
  • url will be opened in browser
custom actions
  • shows hints overlay over something recongized
  • for files or other customized action

themes

  • black theme

copy mode / vim

  • treat terminal output as a vim buffer
  • doesn't support all, but a lot

aliases

you can find good alias lists online

common folder structures

  • use symlinks to store all your dotfiles in one directory
  • ln -s [path-to-orig] [path-to-link-location]
~
├─ .bashrc
├─ .vimrc
└─ dotfiles
   ├─ bashrc
   └─ vimrc

ssh

  • working with remote machines
  • it is good for opening connections
  • can execute commands remotely, and piping back the output to the calling shell ssh username@some-ip.com ls -la

ssh keys

  • typing passwords is inconvenient
  • use public key encryption

    • public, private
    • give public key to the server
    • whenever you try to authenticate use your private key

create a key

ssh-keygen -o -a -t ed25519

  • passphrase gives extra protection bc a user of the private key has to give a password
  • creates

    • private key: id_ed25518
    • public key: id_ed25519.pub

move the public key to the server

cat ~/.ssh/id_ed25519.pub | ssh username@ip-address.com tee .ssh/authorized_keys
  • at that point ssh will ask you for the password for the server and then it will put the key where you sent it
  • 'tee' sends standard input both to the screen and to a file (so you can see what is being saved)
ssh-copy-id
  • does the above but in one command

ssh-copy-id username@ip-address

copy files remotely

scp

scp filename.ext username@ip-address:/path/to/new-filename.ext

rsync

  • copy a LOT of files remotely
  • rsync can continue from interrupts

rsync -avP /folder/to/copy/from username@ip-address:/folder/to/copy/to

ssh config

Host hostname User username HostName ip-address IdentityFile ~/.ssh/id_ed25519 RemoteForward 9999 localhost:8888