Source From Here
Question
I'm perfectly happy with the IP range that docker is giving me by default (176.17.x.x), so I don't need to create a new bridge, I just want to give my containers a static address within that range so I can point client browsers to it directly.
How-To
I used the method written here in the official Docker documentation and I confirm it's working:
Supplement
* [ 常見問題 ] Add link between container and bridge
This is a blog to track what I had learned and share knowledge with all who can take advantage of them
標籤
- [ 英文學習 ]
- [ 計算機概論 ]
- [ 深入雲計算 ]
- [ 雜七雜八 ]
- [ Algorithm in Java ]
- [ Data Structures with Java ]
- [ IR Class ]
- [ Java 文章收集 ]
- [ Java 代碼範本 ]
- [ Java 套件 ]
- [ JVM 應用 ]
- [ LFD Note ]
- [ MangoDB ]
- [ Math CC ]
- [ MongoDB ]
- [ MySQL 小學堂 ]
- [ Python 考題 ]
- [ Python 常見問題 ]
- [ Python 範例代碼 ]
- [心得扎記]
- [網路教學]
- [C 常見考題]
- [C 範例代碼]
- [C/C++ 範例代碼]
- [Intro Alg]
- [Java 代碼範本]
- [Java 套件]
- [Linux 小技巧]
- [Linux 小學堂]
- [Linux 命令]
- [ML In Action]
- [ML]
- [MLP]
- [Postgres]
- [Python 學習筆記]
- [Quick Python]
- [Software Engineering]
- [The python tutorial]
- 工具收集
- 設計模式
- 資料結構
- ActiveMQ In Action
- AI
- Algorithm
- Android
- Ansible
- AWS
- Big Data 研究
- C/C++
- C++
- CCDH
- CI/CD
- Coursera
- Database
- DB
- Design Pattern
- Device Driver Programming
- Docker
- Docker 工具
- Docker Practice
- Eclipse
- English Writing
- ExtJS 3.x
- FP
- Fraud Prevention
- FreeBSD
- GCC
- Git
- Git Pro
- GNU
- Golang
- Gradle
- Groovy
- Hadoop
- Hadoop. Hadoop Ecosystem
- Java
- Java Framework
- Java UI
- JavaIDE
- JavaScript
- Jenkins
- JFreeChart
- Kaggle
- Kali/Metasploit
- Keras
- KVM
- Learn Spark
- LeetCode
- Linux
- Lucene
- Math
- ML
- ML Udemy
- Mockito
- MPI
- Nachos
- Network
- NLP
- node js
- OO
- OpenCL
- OpenMP
- OSC
- OSGi
- Pandas
- Perl
- PostgreSQL
- Py DS
- Python
- Python 自製工具
- Python Std Library
- Python tools
- QEMU
- R
- Real Python
- RIA
- RTC
- Ruby
- Ruby Packages
- Scala
- ScalaIA
- SQLAlchemy
- TensorFlow
- Tools
- UML
- Unix
- Verilog
- Vmware
- Windows 技巧
- wxPython
2016年5月31日 星期二
2016年5月30日 星期一
[Linux 常見問題] Bash: How to read one line at a time from output of a command?
Source From Here
Question
I am trying to read the output of a command in bash using a while loop:
- test.sh
The output I got:
After this I tried:
- test2.sh
but it generated an error "Error: Must provide process id to debug" So, how do I read it line by line because I think currently it is slurping the entire line at once.
How-To
There's a mistake, you need < <(command) not <<<$(command). < <( ) is a Process Substitution, $() is a command substitution and <<< is a here-string. So you can do it this way:
- test3.sh
The execution result:
- test4.sh
Some advantages of process substitution:
Question
I am trying to read the output of a command in bash using a while loop:
- test.sh
- #!/bin/bash
- while read -r line
- do
- echo "$line"
- done <<< $(find . -type f)
# ./test.sh
./DebugPy.py ./DebugPy.pyc ./zombe.py ./test.sh
After this I tried:
- test2.sh
- #!/bin/bash
- $(find . -type f) |
- while read -r line
- do
- echo "$line"
- done
How-To
There's a mistake, you need < <(command) not <<<$(command). < <( ) is a Process Substitution, $() is a command substitution and <<< is a here-string. So you can do it this way:
- test3.sh
- #!/bin/bash
- find . -type f |
- while read -r line; do
- echo "$line"
- done
Another approach using Process Substitution:# ./test3.sh
./DebugPy.py
./DebugPy.pyc
./test3.sh
./test2.sh
./zombe.py
./test.sh
- test4.sh
- #!/bin/bash
- while read -r line; do
- echo "$line"
- done < <(find . -type f)
* No need to save temporary files.
* Better performance. Reading from another process often faster than writing to disk, then read back in.
* Save time to computation since when it is performed simultaneously with parameter and variable expansion, command substitution, and arithmetic expansion
[ 英語發燒句 ] Part-51
274. "不用捶胸頓足"
275. "別搞砸了"
276. "他被當成耳邊風"
277. "別拿我出氣"
Note. 類似說法: vent sth on sb
278. "非常時期需要非常手段"
279. "大做文章"
Note. field day 表示 "重要活動的日子; 大好時機; 令人相當愉快的時間", have a field day 可用來指某人可以盡情, 肆意地做事, 而所做的事可能會傷害其他人.
A. I can't believe how terrible my presentation to the class turned out to be.
B. Stop kicking yourself. It's not the end of the world.
275. "別搞砸了"
A. I need to buy the plane tickets for our vacation soon.
B. Don't drop the ball on this one. It is high season, and the cheap tickets will sell out fast.
276. "他被當成耳邊風"
A. Did you tell your husband that it would be a risky bet to invest in that company?
B. I did, but it fell on deaf ears. The stocks we own are now almost worthless.
277. "別拿我出氣"
A. Mom yelled at me for not cleaning the kitchen. You used the kitchen too, but she said nothing to you.
B. Don't take it out on me. I didn't make a mess like you did.
Note. 類似說法: vent sth on sb
278. "非常時期需要非常手段"
A. I heard that Matt is selling his car.
B. Yeah, he just lost his job. Drastic times call for drastic measures.
279. "大做文章"
A. Did you hear about the latest scandal that politician is involved in?
B. Of course! The press is having a field day with it.
Note. field day 表示 "重要活動的日子; 大好時機; 令人相當愉快的時間", have a field day 可用來指某人可以盡情, 肆意地做事, 而所做的事可能會傷害其他人.
2016年5月25日 星期三
[ Python 常見問題 ] Is it possible to attach a console into a running process
Source From Here
Question
I just want to see the state of the process, is it possible to attach a console into the process, so I can invoke functions inside the process and see some of the global variables.
How-To
If you have access to the program's source-code, you can add this functionality relatively easily. See Recipe 576515: Debugging a running python process by interrupting and providing an interactive prompt (Python):
A simple user case can be referred as below. Firstly, our fake main program zombe.py:
- zombe.py
Which will use while loop to keep running as zombie. Then is the module which will help us intercept the running Python process for debugging:
- DebugPy.py
Then you can use it this way:
Another implementation of roughly the same concept is provided by rconsole. From the documentation:
Question
I just want to see the state of the process, is it possible to attach a console into the process, so I can invoke functions inside the process and see some of the global variables.
How-To
If you have access to the program's source-code, you can add this functionality relatively easily. See Recipe 576515: Debugging a running python process by interrupting and providing an interactive prompt (Python):
This provides code to allow any python program which uses it to be interrupted at the current point, and communicated with via a normal python interactive console. This allows the locals, globals and associated program state to be investigated, as well as calling arbitrary functions and classes. To use, a process should import the module, and call listen() at any point during startup. To interrupt this process, the script can be run directly, giving the process Id of the process to debug as the parameter.
A simple user case can be referred as below. Firstly, our fake main program zombe.py:
- zombe.py
- #!/usr/bin/env python
- import time
- import DebugPy
- import pdb
- import sys
- cnt=0
- DebugPy.listen() # This is key point for the main program being able to be intercepted by Linux signal
- def test():
- print("This is testing function in zombe")
- while True:
- cnt = cnt + 1
- print("Count %d...(sleep 10 min)" % (cnt))
- time.sleep(1*60)
- DebugPy.py
- #!/usr/bin/env python
- try: import readline # For readline input support
- except: pass
- import sys, os, traceback, signal, codeop, cStringIO, cPickle, tempfile
- def pipename(pid):
- """Return name of pipe to use"""
- return os.path.join(tempfile.gettempdir(), 'debug-%d' % pid)
- class NamedPipe(object):
- def __init__(self, name, end=0, mode=0666):
- """Open a pair of pipes, name.in and name.out for communication
- with another process. One process should pass 1 for end, and the
- other 0. Data is marshalled with pickle."""
- self.in_name, self.out_name = name +'.in', name +'.out',
- try: os.mkfifo(self.in_name,mode)
- except OSError: pass
- try: os.mkfifo(self.out_name,mode)
- except OSError: pass
- # NOTE: The order the ends are opened in is important - both ends
- # of pipe 1 must be opened before the second pipe can be opened.
- if end:
- self.inp = open(self.out_name,'r')
- self.out = open(self.in_name,'w')
- else:
- self.out = open(self.out_name,'w')
- self.inp = open(self.in_name,'r')
- self._open = True
- def is_open(self):
- return not (self.inp.closed or self.out.closed)
- def put(self,msg):
- if self.is_open():
- data = cPickle.dumps(msg,1)
- self.out.write("%d\n" % len(data))
- self.out.write(data)
- self.out.flush()
- else:
- raise Exception("Pipe closed")
- def get(self):
- txt=self.inp.readline()
- if not txt:
- self.inp.close()
- else:
- l = int(txt)
- data=self.inp.read(l)
- if len(data) < l: self.inp.close()
- return cPickle.loads(data) # Convert back to python object.
- def close(self):
- self.inp.close()
- self.out.close()
- try: os.remove(self.in_name)
- except OSError: pass
- try: os.remove(self.out_name)
- except OSError: pass
- def __del__(self):
- self.close()
- def remote_debug(sig,frame):
- """Handler to allow process to be remotely debugged."""
- def _raiseEx(ex):
- """Raise specified exception in the remote process"""
- _raiseEx.ex = ex
- _raiseEx.ex = None
- try:
- # Provide some useful functions.
- locs = {'_raiseEx' : _raiseEx}
- locs.update(frame.f_locals) # Unless shadowed.
- globs = frame.f_globals
- pid = os.getpid() # Use pipe name based on pid
- pipe = NamedPipe(pipename(pid))
- old_stdout, old_stderr = sys.stdout, sys.stderr
- txt = ''
- pipe.put("Interrupting process at following point:\n" +
- ''.join(traceback.format_stack(frame)) + ">>> ")
- try:
- while pipe.is_open() and _raiseEx.ex is None:
- line = pipe.get()
- if line is None: continue # EOF
- txt += line
- try:
- code = codeop.compile_command(txt)
- if code:
- sys.stdout = cStringIO.StringIO()
- sys.stderr = sys.stdout
- exec code in globs,locs
- txt = ''
- pipe.put(sys.stdout.getvalue() + '>>> ')
- else:
- pipe.put('... ')
- except:
- txt='' # May be syntax err.
- sys.stdout = cStringIO.StringIO()
- sys.stderr = sys.stdout
- traceback.print_exc()
- pipe.put(sys.stdout.getvalue() + '>>> ')
- finally:
- sys.stdout = old_stdout # Restore redirected output.
- sys.stderr = old_stderr
- pipe.close()
- except Exception: # Don't allow debug exceptions to propogate to real program.
- traceback.print_exc()
- if _raiseEx.ex is not None: raise _raiseEx.ex
- def debug_process(pid):
- """Interrupt a running process and debug it."""
- os.kill(pid, signal.SIGUSR1) # Signal process.
- pipe = NamedPipe(pipename(pid), 1)
- try:
- while pipe.is_open():
- txt=raw_input(pipe.get()) + '\n'
- pipe.put(txt)
- except EOFError:
- pass # Exit.
- pipe.close()
- def listen():
- signal.signal(signal.SIGUSR1, remote_debug) # Register for remote debugging.
- if __name__=='__main__':
- if len(sys.argv) != 2:
- print "Error: Must provide process id to debug"
- else:
- pid = int(sys.argv[1])
- debug_process(pid)
# ./zombe.py & // Launch zombe in background
[1] 22266
# ./DebugPy.py 22266 // Attach into zombe process with PID
Interrupting process at following point:
File "./zombe.py", line 21, in
time.sleep(1*60)
>>> cnt // Check the variable cnt in zombe process
1
>>> test() // Call the function available in zombe process
This is testing function in zombe
Another implementation of roughly the same concept is provided by rconsole. From the documentation:
rconsole is a remote Python console with auto completion, which can be used to inspect and modify the namespace of a running script. To invoke in a script do:To attach from a shell do:
- from rfoo.utils import rconsole
- rconsole.spawn_server()
$ rconsole
Security note: The rconsole listener started with spawn_server() will accept any local connection and may therefore be insecure to use in shared hosting or similar environments!
訂閱:
文章 (Atom)
[Git 常見問題] error: The following untracked working tree files would be overwritten by merge
Source From Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 # git clean -d -fx 方案2: 今天在服务器上 gi...
-
前言 : 為什麼程序管理這麼重要呢?這是因為: * 首先,本章一開始就談到的,我們在操作系統時的各項工作其實都是經過某個 PID 來達成的 (包括你的 bash 環境), 因此,能不能進行某項工作,就與該程序的權限有關了。 * 再來,如果您的 Linux 系統是個...
-
屬性 : 系統相關 - 檔案與目錄 語法 : du [參數] [檔案] 參數 | 功能 -a | 顯示目錄中個別檔案的大小 -b | 以bytes為單位顯示 -c | 顯示個別檔案大小與總和 -D | 顯示符號鏈結的來源檔大小 -h | Hum...
-
來源自 這裡 說明 : split 是 Perl 中非常有用的函式之一,它可以將一個字串分割並將之置於陣列中。若無特別的指定,該函式亦使用 RE 與 $_ 變數 語法 : * split /PATTERN/,EXPR,LIMIT * split /...