Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add terminate cli argument for shell-command. #319

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/watchdog/tricks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,25 @@ class ShellCommandTrick(Trick):

def __init__(self, shell_command=None, patterns=None, ignore_patterns=None,
ignore_directories=False, wait_for_process=False,
drop_during_process=False):
drop_during_process=False, terminate_on_event=False):
super(ShellCommandTrick, self).__init__(patterns, ignore_patterns,
ignore_directories)
self.shell_command = shell_command
self.wait_for_process = wait_for_process
self.drop_during_process = drop_during_process
self.process = None
self.terminate_on_event = terminate_on_event

def on_any_event(self, event):
from string import Template

if self.drop_during_process and self.process and self.process.poll() is None:
return

if self.process and self.terminate_on_event:
# TODO hit this with more of a sledge hammer, like autorestarttrick.stop
self.process.terminate()

if event.is_directory:
object_type = 'directory'
else:
Expand Down
6 changes: 6 additions & 0 deletions src/watchdog/watchmedo.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,11 @@ def log(args):
default=False,
help="Ignore events that occur while command is still being executed " \
"to avoid multiple simultaneous instances")
@arg('-t', '--terminate',
dest='terminate_on_event',
action='store_true',
default=False,
help="Kill currently running commands upon new event.")
@expects_obj
def shell_command(args):
"""
Expand All @@ -439,6 +444,7 @@ def shell_command(args):
ignore_patterns=ignore_patterns,
ignore_directories=args.ignore_directories,
wait_for_process=args.wait_for_process,
terminate_on_event=args.terminate_on_event,
drop_during_process=args.drop_during_process)
observer = Observer(timeout=args.timeout)
observe_with(observer, handler, args.directories, args.recursive)
Expand Down