2019年10月23日 星期三

[TIPS] 檢查CPU溫度 Raspberry PI


命令

 /opt/vc/bin/vcgencmd

CPU溫度


pi@raspberrypi:~ $ /opt/vc/bin/vcgencmd version

Jul  9 2019 14:40:53

Copyright (c) 2012 Broadcom

version 6c3fe3f096a93de3b34252ad98cdccadeb534be2 (clean) (release) (start)

pi@raspberrypi:~ $ uname -a

Linux raspberrypi 4.19.57-v7+ #1244 SMP Thu Jul 4 18:45:25 BST 2019 armv7l GNU/Linux

pi@raspberrypi:~ $ /opt/vc/bin/vcgencmd measure_temp

temp=45.1'C

2019年9月23日 星期一

阿班 Bash 常用指令範例

Last Modified Date: 2025-03-28

mkdir

Date: 08-27 v1
建立新目錄

語法


mkdir  [-p]  <目錄名>


OPTION

無參數: 如果該目錄不存在,才建立它。若該目錄在則顯示目錄已存在錯誤訊息,並且回傳值為1 
-p: 如果該目錄不存在,才建立它。若該目錄在則略過,並且回傳值為 0

範例


$ mkdir new_directory
$ mkdir new_directory
mkdir: can't create directory 'new_directory': File exists
$ echo $?
1
$ mkdir -p new_directory
$ echo $?
0




mktemp


Date: 05-14 v1
產生暫存檔案或目錄

語法


mktemp [OPTION] ... [TEMPLATE]

OPTION

-u: --dry-run 不產生任何檔案或目錄,只顯示名稱 (不安全, 僅供測試, 因為有可能重複產生)

範例

$ mktemp -u /you_path/path2/your_prefix.XXXXXXXXXX

/you_path/path2/your_prefix.zjvveu0Ncs

# mktemp /tmp/your_prefix.XXXXXXXXXX
/tmp/your_prefix.5eEu7vU0zi
# ls -la /tmp/your_prefix.5eEu7vU0zi

-rw------- 1 test test 0 May 15 00:06 /tmp/your_prefix.5eEu7vU0zi


read

語法

read [argument1] [argument2] ...

範例

$ bash read_example_01.sh
./type_example_01.sh
./read_example_01.sh
$ find . -type f | while read filename ; do echo "$filename" ; done
./type_example_01.sh
./read_example_01.sh


sed

刪除空白行

語法

 sed -e "/^$/d" 

範例


bash-5.0# echo -e "te\n\nst" | sed "/^$/d"
te
st

type

語法

type command1 command2 ...

範例

a() { echo 'a'; }
b() {
  echo 'b'
}

c()
{
  echo 'c'
}

# type is a shell builtin
type a b c


執行結果

$ bash type_example_01.sh
a is a function
a ()
{
    echo 'a'
}
b is a function
b ()
{
    echo 'b'
}
c is a function
c ()
{
    echo 'c'
}


如果 type 配合 grep 指令把 is a function 去掉,則有自動排版函式的效果。

$ bash type_example_01.sh  | grep  -v "is a function"
a ()
{
    echo 'a'
}
b ()
{
    echo 'b'
}
c ()
{
    echo 'c'
}

xargs

語法

xargs [argument1] [argument2] ...

範例

bash-5.0# find . -type f | xargs wc -l | sed -e "s/^[ \t]*//g"
2 ./b.txt
1 ./c.txt
3 ./a.txt
6 total


2019年9月16日 星期一

阿班 Docker 指令備忘範例

啟動 Docker Container


自動啟動 Container

 編修日期: 2019-9-23

使用情境

docker machine 重啟後,依設定規則來自動執行對應的 Docker Containers

參考文件

https://docs.docker.com/config/containers/start-containers-automatically/

語法

docker update --restart always container-id [...]

or
docker update --restart always container-name [...]

範例

docker update --restart always my-testapp my-testapp2

2019年6月29日 星期六

Bash Tip - Why .bashrc (dot bashrc) not loaded after new user created?


Recently, I reinstalled ubuntu 16.04LTS on my desktop as a Linux server.

After a new user created, ssh into the server each time, but .bashrc (dot bashrc) not loaded after new user created? Please try to modify the profile as bellow.

Append some commands into ~/.profile

DOT_BASHRC=~/.bashrc
# -n True if the length of string is non-zero.
# -r True if file exists and is readable.
if [ -n "$BASH" ] && [ -r "$DOT_BASHRC" ]; then
echo "load $DOT_BASHRC by ~/.profile"
source "$DOT_BASHRC"
fi

result of test-cases.sh example

~$ source .profile
load /home/ben/.bashrc by ~/.profile
.bashrc has been executed once

Appendix


2019年5月13日 星期一

開源 RISC OS Pi 系統 - 初探





1987 開始發展的RISC OS, 終於在2018年10月以Apache 2.0 License 開源,允許商業使用。

近來忙裡偷閒,用樹梅派最新版本 Raspberry Pi 3 Model B+ 安裝了RISC OS PI ,號稱是ARM 架構下快速也易於客制化的作業系統 (A fast and easily customized operating system for ARM devices)。

初步試用, 它的視窗環境用起來執行程式反應很快(相比Raspbian  based X-Window 來說)。

並試了一些常用工具都有替代的APP可以使用
Ex:
  • Nettle(ssh/telnet client)
  • LanMan (Samba client)
  • NetSurf (Browser) ...


不過還需要更多開源高手投入時間進一步改善。

安裝方式是以NOOBS來安裝 RISC OS PI




小結

待機耗電量約在 2.93W (4.89V, 0.6A)運行較吃CPU資源大約升高到 3.9W (4.89V, 0.8A),以一個迷你作業系統而言,還算省電。有興趣的朋友可以裝起來試玩看看。






參考資料


延伸閱讀

2019年5月9日 星期四

DevOps Tip - Got error - not found aws-iam-authenticator for kubectl using?


Problem

I've got error to run kubectl command with aws-iam-authenticator not found

(venv) ➜  k8s git:(develop) ✗ kubectl get pod
Unable to connect to the server: getting credentials: exec: exec: "aws-iam-authenticator": executable file not found in $PATH


Solutions

Installation tool on OSX Mojave

brew install aws-iam-authenticator

Example of installation

 (venv) ➜  k8s git:(develop) ✗ brew install aws-iam-authenticator
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 3 taps (homebrew/core, homebrew/cask and caskroom/cask).
==> New Formulae
..
==> Downloading https://homebrew.bintray.com/bottles/
aws-iam-authenticator-0.4.0.mojave.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/52...
######################################################################## 100.0%
==> Pouring aws-iam-authenticator-0.4.0.mojave.bottle.tar.gz
🍺  /usr/local/Cellar/aws-iam-authenticator/0.4.0: 5 files, 31.1MB

References


2019年4月11日 星期四

Python Tip - Examples of initialize variables with sequence number in different ways

Purpose

It is used to collect sample code for python's coding tips of initialization variables.

A. Variables with constants

test_init_vars.py

#!/usr/bin/env python

a=0
b=1
c=2
d=3
e=4
f=5
print(a, b, c, d, e, f)

B. Use range()

test_init_vars_by_range.py

#!/usr/bin/env python

a, b, c, d, e, f = range(6)

print(a, b, c, d, e, f)

C. Use enum since python 3.4 supported

test_init_vars_by_enum.py

#!/usr/bin/env python3.6

from enum import IntEnum,auto

class Vars(IntEnum):
    a=0
    b=auto()
    c=auto()
    d=auto()
    e=auto()
    f=auto()

print("{}, {}, {}, {}, {}, {}".format(Vars.a, Vars.b, Vars.c, Vars.d, Vars.e, Vars.f))

Result

$ python2.7 test_init_vars.py
(0, 1, 2, 3, 4, 5)
$ python3.6 test_init_vars.py
0 1 2 3 4 5

$ python2.7 test_init_vars_by_range.py
(0, 1, 2, 3, 4, 5)
$ python3.6 test_init_vars_by_range.py
0 1 2 3 4 5

$ python2.7 test_init_vars_by_enum.py
Traceback (most recent call last):
  File "test_init_vars_by_enum.py", line 3, in
    from enum import IntEnum,auto
ImportError: cannot import name auto

$ python3.6 test_init_vars_by_enum.py
0, 1, 2, 3, 4, 5

References

2019年3月21日 星期四

在執行期取得目前Makefile腳本檔所在的目錄 Absolute Path of current Makefile 含測試結果


範例程式取得Makefile所在絕對路徑 (Absolute Path of current Makefile)


current_mk_abspathname := $(abspath $(lastword $(MAKEFILE_LIST)))
current_mk_absdir := $(dir $(current_mk_abspathname))
all:
@echo "Makefile absolute pathname: $(current_mk_abspathname)" ; \
echo "Makefile absolute path: $(current_mk_absdir)"

test results on linux


/build$ make -v
GNU Make 4.1
Built for x86_64-pc-linux-gnu
Copyright (C) 1988-2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
/build$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.4 LTS
Release: 16.04
Codename: xenial
/build$ make -f 2019-03-21/01-abspath-mk.mk
Makefile absolute pathname: /build/2019-03-21/01-abspath-mk.mk
Makefile absolute path: /build/2019-03-21/


參考

https://www.systutorials.com/241620/how-to-get-the-full-path-and-directory-of-a-makefile-itself/

2019年3月14日 星期四

Bash Tip - Run Command with multiple arguments with ssh




This is a note for logging my simple way to use ssh and bash with continuous integration on remote server.

You can execute the following test-cases.sh on your host.

It will execute remote script of the test with correct argument contents.

source code of test-cases.sh on host


#/bin/bash
# 2019 Ben Wei <ben@juluos.org>
#
# This is a example script for CI tests for blog
# if not defined in environment variable
# you can use the following command to defined it
#
# export TEST_SSH_HOST="your_ssh_host"
#
function run_cases()
{
local case_name="$1"
local args=""
while [ ! "x$1" = "x" ]; do
printf -v __ %q "$1"
args="$args \"$__\""
shift
done
if [ "x${TEST_SSH_HOST}" = "x" ]; then
TEST_SSH_HOST=test-server
fi
ssh ${TEST_SSH_HOST} "cd test-cases; /bin/bash ./tests.sh $args"
return $?
}
view raw test-cases.sh hosted with ❤ by GitHub



directory layout of tester account on test-server



source code of tests.sh on test-server



#!/bin/bash
i=1
while [ ! "x$1" = "x" ]; do
echo "argv[$i]=[$1]"
shift
i=$((i + 1))
done
view raw tests.sh hosted with ❤ by GitHub

result of test-cases.sh example

source ./test-cases.sh
➜  ~ run_cases "\"1\"\"" "b'\"" "c'#\"" "\"\"" "\'"
ssh test-server cd test-cases; /bin/bash ./tests.sh  "\"1\"\"" "b\'\"" "c\'\#\"" "\"\"" "\\'"
argv[1]=["1""]
argv[2]=[b\'"]
argv[3]=[c\'\#"]
argv[4]=[""]
argv[5]=[\\']

Notes

printf %q  is use to make safe quote the argument if hashtag(#), quote('), double quote(") and so on.

Appendix



2019年2月22日 星期五

Bash Tip - Use bash to implement like C's argc and argv

Test bash script capacity

I want to show how to write the advanced bash script with shift and array.
Feature:

  • filter the arguments with '-' leading character as options
  • the following inputs are the data arguments


Tested environments: - bash 4.3.48 x86_64-pc-linux-gnu ubuntu-16.04.4 LTS

#!/usr/bin/env bash
# Copyright (C) 2019 by Ben Wei <ben@juluos.org>
# Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
# CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
######
# sample code of blog
## tested with bash 4.3.48 x86_64-pc-linux-gnu ubuntu-16.04.4 LTS
verbose=0
clog()
{
if [ $verbose -gt 0 ]; then
echo $@
fi
}
syntax()
{
cat << EOF >/dev/stdout
syntax: [options] <message 1> [message 2] [message 3]
options -v verbose of clog
EOF
exit 1
}
while [ 1 ]; do
if [[ "$1" =~ "-".* ]]; then
if [ "$1" == "-v" ]; then
verbose=1
fi
shift
else
break
fi
done
if [ -z "$1" ]; then
syntax
fi
arguments=("$1" "$2" "$3")
argc=${#arguments[@]}
if [ "$argc" -eq 0 ]; then
syntax
fi
for ((i=0; i<${argc} ; i++)); do
if [ "x${arguments[$i]}" = "x" ]; then continue; fi
clog "input argument[$i]: ${arguments[$i]}"
done
exit 0
# origin result
bash bash_arguments_example.sh -v "a b" "ff 11"
input argument[0]: a b
input argument[1]: ff 11
# with debuging option
$ bash -x bash_arguments_example.sh -v "a b" "ff 11"
+ verbose=0
+ '[' 1 ']'
+ [[ -v =~ -.* ]]
+ '[' -v == -v ']'
+ verbose=1
+ shift
+ '[' 1 ']'
+ [[ a b =~ -.* ]]
+ break
+ '[' -z 'a b' ']'
+ arguments=("$1" "$2" "$3")
+ argc=3
+ '[' 3 -eq 0 ']'
+ (( i=0 ))
+ (( i<3 ))
+ '[' 'xa b' = x ']'
+ clog 'input argument[0]: a b'
+ '[' 1 -gt 0 ']'
+ echo input 'argument[0]:' a b
input argument[0]: a b
+ (( i++ ))
+ (( i<3 ))
+ '[' 'xff 11' = x ']'
+ clog 'input argument[1]: ff 11'
+ '[' 1 -gt 0 ']'
+ echo input 'argument[1]:' ff 11
input argument[1]: ff 11
+ (( i++ ))
+ (( i<3 ))
+ '[' x = x ']'
+ continue
+ (( i++ ))
+ (( i<3 ))
+ exit 0
view raw executed result hosted with ❤ by GitHub

2019年1月30日 星期三

DevOps Tip - How to use terraform apply without prompt and input yes for CI/CD?

Problem

Took a memo about integration of CI/CD applied by terraform without prompt to input 'yes'
It is annoying to input yes every time if you already make sure already by `terraform plan`.
On the other hand, I need to integrate with different system for CI/CD. I need a way to approve it automatically.
By tests with commands official documents, I found the correct command and arguments.

Tested Solutions


    terraform apply -input=false -auto-approve

or as my test, you can shorten to the following command

    terraform apply -auto-approve

Test Environments

  • Ubuntu 16.04.04 LTS
  • OSX 10.14

Reference:




Extended Reading



2019年1月29日 星期二

DevOps Tip - How to use shared terraform plugins by -plugin-dir argument?


Problem


If you executed `terraform init`, you may already have the .terraform in your folder.


Because you may have many terraform repo similar to another, you can use the -plugin-dir to reduce the usage of disk space.

As a shared plugins so that we don’t need many copies of plugins into different locations.

Example

  • Here I used my OSX Macbook as a Demo Environment.
  ====================
  cd /build/setupdev/
    sudo mkdir -p  /opt/terraform/terraform.d
mv .terraform/plugins to /opt/terraform/terraform.d/plugins


  • Please run the following command to apply it.
  ====================
terraform init -input=false \
-plugin-dir=/opt/terraform/terraform.d/plugins/darwin_amd64





Tree of .terraform

Tree of terraform plugins

Reference