Module: RunfileExec
- Defined in:
- lib/runfile-exec/extensions.rb,
lib/runfile-exec/version.rb
Overview
This module provides methods for easily and politely run shell commands through a Runfile action. It is mainly a convenient wrapper around ‘system` and `exec` and it also adds functions for running background tasks with ease.
Constant Summary collapse
- VERSION =
"0.2.1"
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
11 12 13 |
# File 'lib/runfile-exec/extensions.rb', line 11 def self.pid_dir @@pid_dir end |
.pid_dir=(dir) ⇒ Object
7 8 9 |
# File 'lib/runfile-exec/extensions.rb', line 7 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
64 65 66 |
# File 'lib/runfile-exec/extensions.rb', line 64 def after_run(&block) @after_run_block = block end |
#before_run(&block) ⇒ Object
Set a block to be called before each run
59 60 61 |
# File 'lib/runfile-exec/extensions.rb', line 59 def before_run(&block) @before_run_block = block end |
#run(cmd) ⇒ Object
Run a command, wait until it is done and continue
16 17 18 19 20 21 22 |
# File 'lib/runfile-exec/extensions.rb', line 16 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
25 26 27 28 29 30 |
# File 'lib/runfile-exec/extensions.rb', line 25 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
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/runfile-exec/extensions.rb', line 34 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’
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/runfile-exec/extensions.rb', line 47 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 |