Skip to content

Instantly share code, notes, and snippets.

View piaoger's full-sized avatar

Piaoger piaoger

View GitHub Profile
@piaoger
piaoger / resource-usage.sh
Last active October 26, 2016 08:48
query cpu/ram/hdd usage
#!/bin/bash
# from http://unix.stackexchange.com/questions/69167/bash-script-that-print-cpu-usage-diskusage-ram-usage
FREE_DATA=`free -m | grep Mem`
CURRENT=`echo $FREE_DATA | cut -f3 -d' '`
TOTAL=`echo $FREE_DATA | cut -f2 -d' '`
echo CPU: `top -b -n1 | grep "Cpu(s)" | awk '{print $2 + $4}'`%
echo RAM: $(echo "scale = 2; $CURRENT/$TOTAL*100" | bc)%
@piaoger
piaoger / get-public-ipv4.sh
Created September 12, 2016 06:20
get-public-ipv4.sh
# get public ipv4
# http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html
# http://stackoverflow.com/questions/14594151/methods-to-detect-public-ip-address-in-bash
AMI_ID=$(curl --connect-timeout 5 "http://169.254.169.254/latest/meta-data/ami-id")
if [[ $AMI_ID == ami-* ]] ;
then
IPV4=$(curl --connect-timeout 5 "http://169.254.169.254/latest/meta-data/public-ipv4")
else
@piaoger
piaoger / gen-ghpages.sh
Last active September 28, 2016 08:51
generate gh-pages
gitconfig() {
git config user.name "piaoger"
git config user.email "[email protected]"
}
rev=$(git rev-parse --short HEAD)
depot=https://github.com/piaoger/playground-rust.git
@piaoger
piaoger / open-current-dir.sh
Created September 29, 2016 03:37
open file manager of current directory in the terminal
# open file manager of current directory in the terminal
# http://askubuntu.com/questions/31069/how-to-open-a-file-manager-of-the-current-directory-in-the-terminal
xdg-open .
# mac
# open .
@piaoger
piaoger / rust-intro-helloworld.rs
Last active October 18, 2016 06:50
rust-into: hello
// https://play.rust-lang.org/
// https://is.gd/VHQlvO
// Line comments which go to the end of the line.
/* Block comments which go to the closing delimiter. */
fn main () {
@piaoger
piaoger / rust-intro-borrow.rs
Last active October 17, 2016 09:53
rust-intro: borrow
// http://rustbyexample.com/scope/borrow.html
// https://is.gd/E0jeLL
fn simple_borrow() {
let mut v = vec!["A"];
{
// immutable borrow
@piaoger
piaoger / memory-unsafty.c
Last active October 18, 2016 03:58
memory unsafe in c
// https://en.wikipedia.org/wiki/Dangling_pointer
void f() {
int *x = malloc(sizeof(int));
*x = 1024;
printf("%d\n", *x);
@piaoger
piaoger / raii-resource-management.txt
Last active October 18, 2016 03:48
raii style resource management
# raii (c++)
before:
void UseFile(char const* fn)
{
FILE* f = fopen(fn, "r"); // 获取资源
// 使用资源
try {
if (!g()) { fclose(f); return; }
// ...
@piaoger
piaoger / linux-date-handling.sh
Last active November 13, 2016 13:13
handling date in linux bash
# I want compress yesterday's log, so I have to learn how to get yesterday with bash
# yesterday=$(date --date='-1 day' +%Y-%m-%d)
# today=$(date +%Y-%m-%d)
# yesterday_log=service_log_${yesterday}.log
# zip ${yesterday_log}.zip ${yesterday_log}
## today
today=$(date +%Y-%m-%d)
@piaoger
piaoger / kill-processes.sh
Last active November 23, 2016 07:02
kill processes by name
#!/bin/bash
appname=python
getpids() {
local pids=$(ps -ef | grep $1 | grep -v grep| awk '{print $2}')
echo $pids
}
pids=`getpids $appname`