- Shell communicates with processes using signals, and when process receives them, they stop execution and deal with signal
- Signals are called software interrupts for this reason
- Signal examples
ctrl-c=SIGINTsignalctrl-\=SIGQUITsignalctrl-z=SIGSTOPsignalman signalshows all available
kill -SIGNAL %JOB_ID: sends signal to corresponding job/process-TERMsendsSIGTERM, which works well for ending background processes gracefully
&at the end of a command runs it in the background and hands the terminal back overjobs: lists unfinished jobs in terminal session, which can be referred to by their PID or%JOB_ID, latter of which is shown byjobsbg %JOB_ID: continue suspended job in backgroundfg %JOB_ID: bring background job, suspended or running, into current terminal and resumenohup COMMAND: will not end process if sent aSIGHUP, which happens when terminal windows is closed
- Terminal multiplexer: allows you to use multiple terminal windows via tabs/panes, attach/detach, etc.
tmuxis a popular example tmuxis controlled via keyboard commands, which are triggered by a prefix key, which by default isctrl-b, expressed asC-bin their notation- Sessions
tmux= starts a new sessiontmux new -s NAME= new session with specified nameC-b+d= detach from current sessiontmux ls= view active sessionstmux attach -t NUM_OR_NAME= attach to sessiontmux rename -t NUM NAME= rename session of indexNUMtoNAME
- Panes
C-b+%= split left-rightC-b+“= split top-bottomC-b+ARROW= navigate panes
- Windows (aka tabs)
C-b+c= new windowC-b+p= prev windowC-b+n= next windowC-b+NUM= go to window of indexNUM
- https://tmuxcheatsheet.com/
- All commands can be viewed inside
tmuxby pressingC-b+? - Create file
~/.tmux.confto make it more usable/intuitive# remap prefix from 'C-b' to 'C-a' unbind C-b set-option -g prefix C-a bind-key C-a send-prefix # split panes using | and - bind | split-window -h bind - split-window -v unbind '"' unbind % # switch panes using Alt-arrow without prefix bind -n M-Left select-pane -L bind -n M-Right select-pane -R bind -n M-Up select-pane -U bind -n M-Down select-pane -D # enable mouse mode (tmux 2.1 and above) set -g mouse on # don't rename windows automatically set-option -g allow-rename off
- Aliases allow you to assign lengthier commands to shorter strings:
alias ALIAS_NAME="COMMAND_STRING [ARGS] ..."- If a string is assigned as via
alias, its raw string can be accessed by prepending with\or runningunalias ALIAS_NAME alias ALIAS_NAMEreveals definition
- If a string is assigned as via
- Aliases do not persist across sessions, so place in
~/.bashrc
- Files that start with
., hidden bylsby default - Examples
bash:~/.bashrc,~/.bash_profilegit:~/.gitconfigvim:~/.vimrcand the~/.vimdirectoryssh:~/.ssh/configtmux:~/.tmux.conf
- For portability, place them all in a single directory with version control, symlinked to the correct places with a script
- Symlink:
ln -s FILE_OR_DIRECTORY_PATH LINK_PATH
- Symlink:
- If you want different programs to share configurations, put those in their own file, then source the file in each program’s dotfile
- Secure Shell (SSH) is used to access remote machines:
ssh USER@IP_ADDRESS_OR_URL [COMMAND]- add a
COMMANDat the end to just run it directly
- add a
- SSH keys allow you to forego password use each time you log into remote
ssh-keygen -t ed25519 -C "your_email@example.com"- Generated in pairs, public and private, i.e.
id_ed25519andid_ed25519.pub- public is passed to remote, private is kept locally to as a method of authentication - Passphrase ensures that if private key file is obtained by someone else, they can’t use it
- Remote machine
- Copy to remote with
ssh-copy-id -i .ssh/id_ed25519.pub USER@IP_ADDRESS_OR_URL
- Copy to remote with
- GitHub/GitLab
cat ~/.ssh/id_ed25519.pub, then copy output and paste it into text field on website
rsync -avP FILE_OR_DIRECTORY_PATH USER@IP_ADDRESS_OR_URL:DESTINATIONsyncs files and directories between two hosts or machines- List remote devices in
~/.ssh/configand you can connect by simply typingssh HOST_NAMEHost HOST_NAME User USER Hostname IP_ADDRESS_OR_URL IdentityFile ~/.ssh/id_ed25519 LocalFoward 9999 localhost:8888 # see port forwarding below tmuxworks well on SSH connections, because detached sessions will persist even if connection falters, and can be picked back up- Other shell alternatives for accessing remote machines are Mosh and SSHFS
- Computer handles many incoming/outgoing requests by placing them on ports and assigning a port number - software will listen to specific ports on a machine to receive info
- If you want to reach a remote server without directly accessible ports (via network/internet, etc.), you can do local port forwarding, which connects an accessible local port to the remote port via SSH, and so when the local port is accessed, it “redirects” to the remote instead
- If Jupyter Notebook is run on remote server which listens on port
8888, and we want to access it locally on port9999, command is
ssh -L 9999:localhost:8888 USER@IP_ADDRESS_OR_URL
- If Jupyter Notebook is run on remote server which listens on port
- Conversely, if you want a remote port to provide access to a port locally, you can do remote port forwarding
- If remote port
8888needs to reroute to local port9999, command is
ssh -R 8888:localhost:9999 USER@IP_ADDRESS_OR_URL
- If remote port
- Helpful Stack Exchange post