您好,登錄后才能下訂單哦!
[TOC]
代碼示例支持 |
---|
平臺(tái): Centos 6.3 |
Python: 2.7.14 |
Github: https://github.com/baidu/CUP |
歡迎關(guān)注公眾號(hào)進(jìn)行技術(shù)互動(dòng)和討論:
今天的坑不僅包括裸用os.system還包括裸用相關(guān)的家族:
這些坑是新同學(xué)非常容易踩,而且 code review 過(guò)程中容易漏掉:
[1] 長(zhǎng)期運(yùn)行 Service 中裸用以函數(shù)家族
[2] 自動(dòng)化測(cè)試中裸用以上函數(shù)家族而不加以保護(hù)
提供一個(gè)作者的代碼參考: 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)
見(jiàn)ShellExec類(lèi)的run函數(shù)
建議看下第二章節(jié)關(guān)于進(jìn)程和子進(jìn)程繼承類(lèi)信息,script使用上述家族進(jìn)行執(zhí)行時(shí),采用了啟動(dòng)一個(gè)子進(jìn)程的方式
Shell執(zhí)行是個(gè)非常常見(jiàn)的操作,所以很多同學(xué)特別是新同學(xué),在使用過(guò)程中經(jīng)常不注意而隨意使用。 裸用一時(shí)爽,進(jìn)程死亡火葬場(chǎng)
參考資料來(lái)源:
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)).
在說(shuō)繼承、拷貝父進(jìn)程的
此外
在 Linux 系統(tǒng)中, 一般系統(tǒng)會(huì)自動(dòng)替程序選擇端口連接到用戶指定的目的端口, 而這個(gè)端口范圍是提前設(shè)定好的, 比如作者的 centos:
$ cat /proc/sys/net/ipv4/ip_local_port_range
10000 60000
歡迎關(guān)注公眾號(hào)進(jìn)行技術(shù)互動(dòng)和討論:
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。