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 =
Version constant, used by gemspec
"0.1.0"- @@pid_dir =
nil
Class Method Summary collapse
- .pid_dir ⇒ Object
- .pid_dir=(dir) ⇒ Object
-
.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.
Instance Method Summary collapse
-
#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
13 14 15 |
# File 'lib/runfile-exec/extensions.rb', line 13 def self.pid_dir @@pid_dir end |
.pid_dir=(dir) ⇒ Object
9 10 11 |
# File 'lib/runfile-exec/extensions.rb', line 9 def self.pid_dir=(dir) @@pid_dir = dir end |
.run(cmd) ⇒ Object
Run a command, wait until it is done and continue
18 19 20 21 |
# File 'lib/runfile-exec/extensions.rb', line 18 def run(cmd) say "!txtgrn!> #{cmd}" system cmd end |
.run!(cmd) ⇒ Object
Run a command, wait until it is done, then exit
25 26 27 28 |
# File 'lib/runfile-exec/extensions.rb', line 25 def run!(cmd) say "!txtgrn!> #{cmd}" exec cmd end |
Instance Method Details
#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
33 34 35 36 37 38 39 |
# File 'lib/runfile-exec/extensions.rb', line 33 def run_bg(cmd, pid: nil, log: '/dev/null') 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 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’
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/runfile-exec/extensions.rb', line 43 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 |