당신이 처리 할 수없는 일반적인 경우보다 더 복잡한 경우가있는 경우 ``
, 다음 체크 아웃 Kernel.spawn()
. 이것은 루비가 외부 명령을 실행하기 위해 제공하는 가장 일반적이고 완전한 기능인 것 같습니다.
당신은 그것을 사용할 수 있습니다 :
- 프로세스 그룹을 작성하십시오 (Windows).
- 파일 / 각 오류로 리디렉션, 출력, 오류.
- env vars, umask를 설정하십시오.
- 명령을 실행하기 전에 디렉토리를 변경하십시오.
- CPU / data / etc에 대한 리소스 제한을 설정합니다.
- 다른 답변에서 다른 옵션으로 수행 할 수있는 모든 작업을 수행하지만 더 많은 코드를 사용하십시오.
루비 문서는 충분한 예제가 있습니다 :
env: hash
name => val : set the environment variable
name => nil : unset the environment variable
command...:
commandline : command line string which is passed to the standard shell
cmdname, arg1, ... : command name and one or more arguments (no shell)
[cmdname, argv0], arg1, ... : command name, argv[0] and zero or more arguments (no shell)
options: hash
clearing environment variables:
:unsetenv_others => true : clear environment variables except specified by env
:unsetenv_others => false : dont clear (default)
process group:
:pgroup => true or 0 : make a new process group
:pgroup => pgid : join to specified process group
:pgroup => nil : dont change the process group (default)
create new process group: Windows only
:new_pgroup => true : the new process is the root process of a new process group
:new_pgroup => false : dont create a new process group (default)
resource limit: resourcename is core, cpu, data, etc. See Process.setrlimit.
:rlimit_resourcename => limit
:rlimit_resourcename => [cur_limit, max_limit]
current directory:
:chdir => str
umask:
:umask => int
redirection:
key:
FD : single file descriptor in child process
[FD, FD, ...] : multiple file descriptor in child process
value:
FD : redirect to the file descriptor in parent process
string : redirect to file with open(string, "r" or "w")
[string] : redirect to file with open(string, File::RDONLY)
[string, open_mode] : redirect to file with open(string, open_mode, 0644)
[string, open_mode, perm] : redirect to file with open(string, open_mode, perm)
[:child, FD] : redirect to the redirected file descriptor
:close : close the file descriptor in child process
FD is one of follows
:in : the file descriptor 0 which is the standard input
:out : the file descriptor 1 which is the standard output
:err : the file descriptor 2 which is the standard error
integer : the file descriptor of specified the integer
io : the file descriptor specified as io.fileno
file descriptor inheritance: close non-redirected non-standard fds (3, 4, 5, ...) or not
:close_others => false : inherit fds (default for system and exec)
:close_others => true : dont inherit (default for spawn and IO.popen)