Skip to content

Instantly share code, notes, and snippets.

@hakopako
Last active March 11, 2018 04:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hakopako/31d304ce9491709a40c1 to your computer and use it in GitHub Desktop.
Save hakopako/31d304ce9491709a40c1 to your computer and use it in GitHub Desktop.
ほんとうによくわすれる

linuxcmd

status

  • loadavg $ sar -q

connection

  • tcpのみ1秒ごとに表示 $ netstat -nt 1
  • watch コマンドとあわせると便利 $ watch -n 1 netstat -tanpx
  • tcp通信dump $ tcpdump -X -s0 -A src port 80

cron

  • cron実行ログを眺める # tail -f /var/log/cron
  • cron実行ログユーザ別 # tail -f /var/spool/mail/[user]
  • cron設定ユーザ別 # ll /var/spool/cron/

サーバスペック系

  • OS種類 $ cat /etc/redhat-release
  • サーババージョン $ uname -a or $ cat /proc/version
  • サーバスペック $ cat /proc/cpuinfo とか $ cat /proc/meminfo

ほか

  • 文字列を含むファイル検索 $ find /home/hoge/ -type f -print | xargs grep **HOGE** /dev/null
  • 文字列を含むファイル検索 $ grep [keyword] [filepath]
  • フォルダ使用量 $ du -sh /home/*
# output
[2017/07/09 16:19:32] aaaaaaaaaaaaaaaa

Bash

### ログ出力
function logger() {
    echo "[`date "+%Y/%m/%d %H:%M:%S"`] $1"
}

logger "baaaaaaaaaaarr"

Python3

from datetime import datetime

def logger(self, text):
    print('[' + datetime.now().strftime('%Y/%m/%d %H:%M:%S') + '] ' + text)
    
self.logger('aaaaaaaa')

PHP

function logger($text){
    echo('['.date("Y-m-d H:i:s").'] '.$text);
}

logger('aaaaaaaa');

Ruby

pandas系

read

# データの読み込み csv/array とかいろいろ
import pandas as pd
df = pd.read_csv("probe_20160105.csv", header=None)
df = pd.read_csv("probe_20160105.csv", names=('IMEI', 'dt', 'lat', 'lng', 'status'))
# hogeがtrueのものをフィルタリング
df = df[df['hoge'] == true]]  
# DataFrameをループ
for index, row in df.iterrows():
    print row['c1'], row['c2']

Postgresql系

スキーマ一覧

select schema_name from information_schema.schemata;

スキーマ内のテーブル一覧

SELECT
  table_catalog
  , table_schema
  , table_name
  , table_type 
FROM
  information_schema.tables 
WHERE
  table_schema = '[schema_name]'; 

変数!

=> \set SCHEMA_NAME 'bar'
=> CREATE SCHEMA :SCHEMA_NAME;
=> CREATE TABLE :SCHEMA_NAME.hoge (
    uid     character varying(80) NOT NULL,
    data    text,
    PRIMARY KEY (uid)
);
=> \unset SCHEMA_NAME

vacuumされてる?

select schemaname, relname, last_vacuum, last_autovacuum, last_analyze, last_autoanalyze from pg_stat_all_tables;

  relid  |     schemaname     |         relname         | seq_scan | seq_tup_read | idx_scan | idx_tup_fetch | n_tup_ins | n_tup_upd | n_tup_del | n_tup_hot_upd | n_live_tup | n_dead_tup | last_vacuum |      last_autovacuum        | last_analyze |       last_autoanalyze        | vacuum_count | autovacuum_count | analyze_count | autoanalyze_count

postgresqlの容量系

SELECT relname, reltuples, (relpages / 128) as mbytes, (relpages * 8192.0 / (reltuples + 1e-10)) as average_row_size FROM pg_class ORDER BY mbytes DESC;

memo

  • $ docker images ... イメージ一覧
  • $ docker ps ... 起動中コンテナ
  • $ docker ps -a ... 全てのコンテナ
  • $ docker stop <コンテナID or NAME> ... コンテナの停止
  • $ docker start <コンテナID or NAME> ... コンテナの起動
  • $ docker rm [コンテナID] ... コンテナの削除
  • $ docker run [オプション] [--name {コンテナー名}] {イメージ名}[:{タグ名}] [コンテナーで実行するコマンド] $ docker run -d -it -p 80:80 --hostname hogehoge --name hogehoge imagename:6.6 /bin/bash
  • $ docker exec -it [name] /bin/bash ... コンテナの中に入る
a = input() # read line one by one.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment