Module: Runfile::Exec
- Defined in:
- lib/runfile/extensions/exec.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#after_run(&block) ⇒ Object
Set a block to be called after each run.
-
#before_run(&block) ⇒ Object
Set a block to be called before each run.
-
#run(cmd) ⇒ Object
Run a command, wait until it is done and continue.
-
#run!(cmd) ⇒ Object
Run a command, wait until it is done, then exit.
-
#run_bg(cmd, pid: nil, log: '/dev/null') ⇒ Object
Run a command in the background, optionally log to a log file and save the process ID in a pid file.
-
#stop_bg(pid) ⇒ Object
Stop a command started with ‘run_bg’.
Class Method Details
.pid_dir ⇒ Object
12 13 14 |
# File 'lib/runfile/extensions/exec.rb', line 12 def self.pid_dir @@pid_dir end |
.pid_dir=(dir) ⇒ Object
8 9 10 |
# File 'lib/runfile/extensions/exec.rb', line 8 def self.pid_dir=(dir) @@pid_dir = dir end |
Instance Method Details
#after_run(&block) ⇒ Object
Set a block to be called after each run
65 66 67 |
# File 'lib/runfile/extensions/exec.rb', line 65 def after_run(&block) @after_run_block = block end |
#before_run(&block) ⇒ Object
Set a block to be called before each run
60 61 62 |
# File 'lib/runfile/extensions/exec.rb', line 60 def before_run(&block) @before_run_block = block end |
#run(cmd) ⇒ Object
Run a command, wait until it is done and continue
17 18 19 20 21 22 23 |
# File 'lib/runfile/extensions/exec.rb', line 17 def run(cmd) cmd = @before_run_block.call(cmd) if @before_run_block return false unless cmd say "!txtgrn!> #{cmd}" system cmd @after_run_block.call(cmd) if @after_run_block end |
#run!(cmd) ⇒ Object
Run a command, wait until it is done, then exit
26 27 28 29 30 31 |
# File 'lib/runfile/extensions/exec.rb', line 26 def run!(cmd) cmd = @before_run_block.call(cmd) if @before_run_block return false unless cmd say "!txtgrn!> #{cmd}" exec cmd end |
#run_bg(cmd, pid: nil, log: '/dev/null') ⇒ Object
Run a command in the background, optionally log to a log file and save the process ID in a pid file
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/runfile/extensions/exec.rb', line 35 def run_bg(cmd, pid: nil, log: '/dev/null') cmd = @before_run_block.call(cmd) if @before_run_block return false unless cmd full_cmd = "exec #{cmd} >#{log} 2>&1" say "!txtgrn!> #{full_cmd}" process = IO.popen "exec #{cmd} >#{log} 2>&1" File.write pidfile(pid), process.pid if pid @after_run_block.call(cmd) if @after_run_block return process.pid end |
#stop_bg(pid) ⇒ Object
Stop a command started with ‘run_bg’. Provide the name of he pid file you used in ‘run_bg’
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/runfile/extensions/exec.rb', line 48 def stop_bg(pid) file = pidfile(pid) if File.exist? file pid = File.read file File.delete file run "kill -s TERM #{pid}" else say "!txtred!PID file not found." end end |