본문으로 건너뛰기

실행 작업 만들기 (sys.task)

선언

작업 = sys.task(실행 파일 경로 [, 인수...])

매개변수

  • 실행 파일 경로
    문자열
  • 인수...
    문자열, 선택적 가변 인수. 기본값은 없습니다.

반환값

  • 작업
    실행 작업 객체. 작업을 제어하거나 실행하는 객체를 반환합니다.

작업 객체 메서드 목록

메서드매개변수반환값설명
:launch()작업을 시작하여 비동기로 실행합니다.
:wait_until_exit()작업이 끝날 때까지 동기식으로 기다리며, 작업이 끝나기 전까지 모든 스레드를 차단합니다.
:set_stdin(입력 스트림)파이프 | 문자열작업의 입력 스트림을 설정합니다. 인수로 "std", "/dev/null", 파일 경로 또는 파이프 객체를 사용할 수 있습니다. 작업 시작 후 다시 호출하면 오류가 발생합니다.
:stdin()파이프 | 문자열작업의 입력 스트림을 가져옵니다. "std", "/dev/null", 파일 경로 또는 파이프 객체를 반환할 수 있습니다.
:set_stdout(출력 스트림)파이프 | 문자열작업의 출력 스트림을 설정합니다. 인수로 "std", "/dev/null", 파일 경로 또는 파이프 객체를 사용할 수 있습니다. 작업 시작 후 다시 호출하면 오류가 발생합니다.
:stdout()파이프 | 문자열작업의 출력 스트림을 가져옵니다. "std", "/dev/null", 파일 경로 또는 파이프 객체를 반환할 수 있습니다.
:set_stderr(오류 스트림)파이프 | 문자열작업의 오류 스트림을 설정합니다. 인수로 "std", "/dev/null", 파일 경로 또는 파이프 객체를 사용할 수 있습니다. 작업 시작 후 다시 호출하면 오류가 발생합니다.
:stderr()파이프 | 문자열작업의 오류 스트림을 가져옵니다. "std", "/dev/null", 파일 경로 또는 파이프 객체를 반환할 수 있습니다.
:set_work_dir(디렉터리)문자열작업의 작업 디렉터리를 설정합니다. 작업 시작 후 다시 호출하면 오류가 발생합니다.
:work_dir()문자열작업의 작업 디렉터리를 가져옵니다.
:set_env(환경 변수)테이블작업의 환경 변수 키-값 사전을 설정합니다. 작업 시작 후 다시 호출하면 오류가 발생합니다.
:env()테이블작업의 환경 변수 키-값 사전을 가져옵니다.
:pid()정수작업의 프로세스 ID를 가져옵니다. 작업 시작 전에 가져오면 0을 반환합니다.
:is_running()불리언작업이 실행 중인지 확인합니다. :is_running()이 false를 반환하더라도 좀비 프로세스가 생기지 않도록 작업에 :wait_until_exit()를 한 번 호출해야 합니다.
:interrupt()불리언작업의 주 프로세스에 종료 SIGINT 신호를 보냅니다.
:terminate()불리언작업의 주 프로세스에 종료 SIGTERM 신호를 보냅니다.
:kill()불리언작업의 모든 프로세스에 강제 종료 SIGKILL 신호를 보냅니다. 작업 객체가 회수될 때 kill 작업이 자동으로 실행됩니다.
:termination_status()정수작업의 종료 상태를 가져옵니다. 작업이 아직 끝나지 않았으면 nil, 오류 정보를 반환합니다.
:termination_reason()문자열작업의 종료 원인을 가져옵니다. 작업이 아직 끝나지 않았으면 nil, 오류 정보를 반환합니다.

파이프 객체 메서드 목록

메서드매개변수반환값설명
:read()문자열파이프에서 준비된 데이터를 읽습니다.
:write(데이터)문자열파이프에 데이터를 씁니다.
:wclose()불리언, nil | 문자열파이프의 쓰기 끝을 닫습니다. 성공하면 true, 실패하면 false, 오류 정보를 반환합니다.
:rclose()불리언, nil | 문자열파이프의 읽기 끝을 닫습니다. 성공하면 true, 실패하면 false, 오류 정보를 반환합니다.

설명

실행 작업을 만들고 실행 작업 객체를 반환합니다.
작업이 끝날 때까지 모든 스레드를 차단하지 않으려면 작업 객체의 :is_running()을 반복 호출하여 작업이 계속 실행 중인지 확인할 수 있습니다. 작업이 끝난 뒤 :wait_until_exit()를 한 번 호출하여 종료되었는지 확인하십시오.
이 함수는 20250705 이후 버전에서 사용할 수 있습니다.

예제

-- 다음 Shell 명령 시뮬레이션
-- echo "你好世界" | /usr/bin/gzip -fc | /usr/bin/gzip -dfc

local comp_tsk = sys.task(jbroot('/usr/bin/gzip'), '-fc') -- 압축 작업 만들기
local decomp_tsk = sys.task(jbroot('/usr/bin/gzip'), '-dfc') -- 압축 해제 작업 만들기

decomp_tsk:set_stdin(comp_tsk:stdout()) -- 압축 해제 작업의 입력 스트림을 압축 작업의 출력 스트림으로 설정

decomp_tsk:launch() -- 압축 해제 작업 시작
comp_tsk:launch() -- 압축 작업 시작

comp_tsk:stdin():write("你好世界") -- 압축 작업의 입력 스트림에 데이터 쓰기
comp_tsk:stdin():wclose() -- 압축 작업 입력 스트림의 쓰기 끝을 닫아 입력이 끝났음을 표시

comp_tsk:wait_until_exit() -- 압축 작업이 끝날 때까지 대기
decomp_tsk:wait_until_exit() -- 압축 해제 작업이 끝날 때까지 대기

nLog(decomp_tsk:stdout():read()) -- 압축 해제 작업의 출력 스트림을 읽어 "你好世界" 출력

예제(TrollStore Persistence Helper를 호출하여 새로고침)

local persistence_helper_path = app.bundle_path('com.apple.tips')
if not persistence_helper_path then
nLog('没有检测到 Tips.app,请先从 AppStore 安装 Tips(提示)应用')
return
end
local root_helper_path = persistence_helper_path..'/trollstorehelper'
if file.exists(root_helper_path) ~= 'file' then
nLog('未注入巨魔持久助手到 Tips,请先从 TrollStore -> Settings -> Install Persistence Helper 选择 Tips 注入')
return
end
local tsk = sys.task(root_helper_path, 'refresh')
tsk:launch()
tsk:wait_until_exit()
sys.killall(9, 'SpringBoard', 'backboardd')

예제(TrollStore를 호출하여 App 설치·제거 및 아이콘 캐시 재구축)

local function _trollstorehelper(...)
local troll_path = app.bundle_path('com.opa334.TrollStore')
assert(type(troll_path) == 'string', 'TrollStore needs to be installed.')
local tsk = sys.task(troll_path..'/trollstorehelper', ...)
tsk:set_stdin('/dev/null')
tsk:launch()
local outbuf = {}
local errbuf = {}
local function drain_stream(stream, buf)
local data = stream:read('nb')
if data and #data > 0 then
buf[#buf + 1] = data
end
end
while tsk:is_running() do
drain_stream(tsk:stdout(), outbuf)
drain_stream(tsk:stderr(), errbuf)
sys.msleep(100)
end
tsk:wait_until_exit()
drain_stream(tsk:stdout(), outbuf)
drain_stream(tsk:stderr(), errbuf)
if tsk:termination_status() == 0 then
local ret = table.concat(outbuf)
return ret
else
local ret = table.concat(errbuf)
return nil, ret
end
end

--- TrollStore로 App 설치
---@param ipapath string 설치 패키지 경로
---@return string? 설치 결과 텍스트. 실패하면 nil 반환
---@return string? 오류 정보. 성공하면 nil 반환
function troll_install(ipapath)
return _trollstorehelper('install', ipapath)
end

--- TrollStore로 설치한 App 제거
---@param bid string 앱 번들 식별자
---@return string? 제거 결과 텍스트. 실패하면 nil 반환
---@return string? 오류 정보. 성공하면 nil 반환
function troll_uninstall(bid)
return _trollstorehelper('uninstall', bid)
end

--- TrollStore 아이콘 캐시 재구축. 다소 시간이 걸림
---@return string? 재구축 결과 텍스트. 실패하면 nil 반환
---@return string? 오류 정보. 성공하면 nil 반환
function troll_rebuild_icon_cache()
return _trollstorehelper('refresh-all')
end

-- 호출 예제
nLog(troll_install(XXT_SCRIPTS_PATH..'/TrollDecrypt.tipa')) -- TrollDecrypt 설치
nLog(troll_uninstall('com.fiore.trolldecrypt')) -- TrollDecrypt 제거
nLog(troll_rebuild_icon_cache()) -- 아이콘 캐시 재구축