您好,登錄后才能下訂單哦!
[TOC]
代碼示例支持 |
---|
平臺: Centos 6.3 |
Python: 2.7.14 |
Github: https://github.com/baidu/CUP |
歡迎關注公眾號進行技術互動和討論:
今天的坑不僅包括裸用os.system還包括裸用相關的家族:
這些坑是新同學非常容易踩,而且 code review 過程中容易漏掉:
[1] 長期運行 Service 中裸用以函數家族
[2] 自動化測試中裸用以上函數家族而不加以保護
提供一個作者的代碼參考: https://github.com/baidu/CUP/blob/master/cup/shell/oper.py
from cup import shell
shellexec = shell.ShellExec()
# timeout=None will block the execution until it finishes
shellexec.run('/bin/ls', timeout=None)
# timeout>=0 will open non-blocking mode
# The process will be killed if the cmd timeouts
shellexec.run(cmd='/bin/ls', timeout=100)
見ShellExec類的run函數
建議看下第二章節關于進程和子進程繼承類信息,script使用上述家族進行執行時,采用了啟動一個子進程的方式
Shell執行是個非常常見的操作,所以很多同學特別是新同學,在使用過程中經常不注意而隨意使用。 裸用一時爽,進程死亡火葬場
參考資料來源:
man fork
)
fork() creates a new process by duplicating the calling process. The new process, referred to as the child, is an exact duplicate of the calling process, referred to as the parent, except for the follow-
ing points:
* The child has its own unique process ID, and this PID does not match the ID of any existing process group (setpgid(2)).
* The child's parent process ID is the same as the parent's process ID.
* The child does not inherit its parent's memory locks (mlock(2), mlockall(2)).
* Process resource utilizations (getrusage(2)) and CPU time counters (times(2)) are reset to zero in the child.
* The child's set of pending signals is initially empty (sigpending(2)).
* The child does not inherit semaphore adjustments from its parent (semop(2)).
* The child does not inherit record locks from its parent (fcntl(2)).
* The child does not inherit timers from its parent (setitimer(2), alarm(2), timer_create(2)).
* The child does not inherit outstanding asynchronous I/O operations from its parent (aio_read(3), aio_write(3)), nor does it inherit any asynchronous I/O contexts from its parent (seeio_setup(2)).
The process attributes in the preceding list are all specified in POSIX.1-2001. The parent and child also differ with respect to the following Linux-specific process attributes:
* The child does not inherit directory change notifications (dnotify) from its parent (see the description of F_NOTIFY in fcntl(2)).
* The prctl(2) PR_SET_PDEATHSIG setting is reset so that the child does not receive a signal when its parent terminates.
* Memory mappings that have been marked with the madvise(2) MADV_DONTFORK flag are not inherited across a fork().
* The termination signal of the child is always SIGCHLD (see clone(2)).
在說繼承、拷貝父進程的
此外
在 Linux 系統中, 一般系統會自動替程序選擇端口連接到用戶指定的目的端口, 而這個端口范圍是提前設定好的, 比如作者的 centos:
$ cat /proc/sys/net/ipv4/ip_local_port_range
10000 60000
歡迎關注公眾號進行技術互動和討論:
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。