All Posts Tagged “linux”

Redirect stderr to stdout or a file

0: stdin
1: stdout
2: stderr

# redirect stdout to a file
$ strace uptime > test.log
# equals to
$ strace uptime 1> test.log

# redirect stderr to a file
$ strace uptime 2> test.log

# redirect stderr to stdout
# if you write `strace uptime 2> 1`, it means that you redirect stderr to a file with the name 1
$ strace uptime 2>&1

# redirect stderr and stdout to a file
$ strace uptime &> test.log

# run a command in backgroud
$ long-running-command &

ref:
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html
https://www.cyberciti.biz/faq/redirecting-stderr-to-stdout/

at: Run a one-time command at a specific time

"at" executes commands at a specified time. You may need to install the "at" package manually.

# install
$ sudo apt-get install at

# start
$ sudo atd

$ at 00:05
at> echo "123" > /tmp/test.txt

$ at 00:05 31.12.2016
at> DPS_ENV=production /home/ubuntu/.virtualenvs/dps-py3/bin/python /home/ubuntu/dps/manage.py send_withdraw_emails 2016 12 > /tmp/send_withdraw_emails.log

ref:
https://www.lifewire.com/linux-command-at-4091646
http://tecadmin.net/one-time-task-scheduling-using-at-commad-in-linux/

tmux: a terminal multiplexer

tmux is a command-line tool that can create multiple virtual terminals in one terminal.

ref:
https://tmux.github.io/

Install

# on Ubuntu
$ sudo apt-get install tmux

# on macOS
$ brew install tmux

$ tmux -V
tmux 2.3

Configuration

in ~/.tmux.conf

# prefix: Control + a
set -g prefix C-a

# kill-session: Control + a >> X
bind X confirm-before "kill-session -t ''"

# select-pane: Option + arrow key
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

# Start numbering at 1
set -g base-index 1
set-window-option -g pane-base-index 1

# Allows for faster key repetition
set -s escape-time 0

# Rather than constraining window size to the maximum size of any client
# connected to the *session*, constrain window size to the maximum size of any
# client connected to *that window*. Much more reasonable.
setw -g aggressive-resize on

set -g status-left-length 40

set -g mouse on

# List of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'

# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run '~/.tmux/plugins/tpm/tpm'

After adding new plugins, you need to press Control + A >> I to fetch and install them.

ref:
https://github.com/tony/tmux-config/blob/master/.tmux.conf

Usage

$ tmux
$ tmux new -s your_project

$ tmux ls

$ tmux attach
$ tmux attach -t your_project

# list all windows with their layouts
$ tmux list-windows

$ tmux kill-session -t your_project

# kill all sessions
$ tmux kill-server

# reload settings
$ tmux source ~/.tmux.conf
Control + a >> Shift + ?
list all shortcut keys

Control + a >> Shift + %
split window vertically

Control + a >> Shift + ""
split window horizontally

Control + a >> Arrow keys
Control + a >> q
move the cursor between panels

Control + a >> d
dettach

Control + a >> c
create a new window

Control + a >> p
switch to the previous window

Control + a >> n
switch to the next window

Control + a >> w
Control + a >> 0-9
switch to window 0-9

Control + a >> [
enable copy mode, you can use Option + Up or Option + Down to scroll

Control + a >> Shift + &
kill the current window

Control + a >> X
kill the current session

ref:
http://manpages.ubuntu.com/manpages/zesty/en/man1/tmux.1.html

Plugins

tmuxinator

in ~/.tmuxinator/your_project.yaml

An example config:

# ~/.tmuxinator/dps.yml

name: dps
root: ~/Projects/dps

startup_window: django
startup_pane: shell

windows:
  - docker:
      layout: main-horizontal
      panes:
        - up:
          - make up
  - django:
      layout: tiled
      panes:
        - run:
          - make run
        - run_worker:
          - make run_worker
        - run_node:
          - make run_node
        - attach:
          - make attach
          - clear
$ tmuxinator start your_project

ref:
https://github.com/tmuxinator/tmuxinator

tmux-resurrect

Control + a >> Control + s
save tmux env

Control + a >> Control + r
restore tmux env

ref:
https://github.com/tmux-plugins/tmux-resurrect
https://github.com/tmux-plugins/tmux-continuum