How to run a bash script automatically after completing any Taskwarrior task?

98 Views Asked by At

I would like to automate the execution of a bash script each time I complete any Taskwarrior task. How can I set up Taskwarrior to run this script automatically after completing any task?

I have written the bash script, and it runs correctly when executed manually. However, I'm not sure how to trigger it automatically after completing a Taskwarrior task.

I have looked into Taskwarrior's hooks feature, but I'm not sure which hook I should use and how to configure it to run my bash script. Can someone provide a step-by-step guide on how to set up a Taskwarrior hook to run a bash script after completing any task?

Any help or guidance on how to achieve this would be greatly appreciated. Thank you!

1

There are 1 best solutions below

0
On

Yeah, so this is what I just tried:

  1. create a script in $HOME/.task/hooks (or wherever your config is)
  2. give it the name of the hook, so e.g. on-modify.savetask (+x for exec right of course)
  3. in the script, you have access to some data via stdin.

Simple example:

#!/usr/bin/env bash

# Read the original task and modified task from stdin
read original_task
read modified_task

# Check if the task is being marked as completed
if echo "$modified_task" | grep -q "\"status\":\"completed\""; then
    # Extract task description (or any other details you need)
    description=$(echo "$original_task" | jq -r '.description')

    # Write to a file in the home directory
    echo "Task completed: $description" >> ~/completed_tasks.txt
fi

# Output the modified task to stdout to complete the hook process
echo "$modified_task"