diff options
| author | tv <tv@krebsco.de> | 2026-11-23 03:50:52 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-23 16:04:45 +0100 |
| commit | 90ae6a7461ac19e54a1b44130743bd0bc55ee47f (patch) | |
| tree | ac8ff042e4f68eba125da4874ee0cf35a1b6dec7 | |
| parent | fdf1aa54c0f3335b092d7698c32f01389a07b661 (diff) | |
pkgs/claude-remote: --schedule, --schedules, --unschedule
--schedule NAME --when=TIME|CALENDAR --prompt=TEXT [--id=ID]
[--permission-mode=MODE] defines a task (see configs/claude.nix), its
time checked and normalized by systemd-analyze: a calendar event first
(so Mon 03:00 is every Monday), recurring if it elapses more than once,
else once (a past one refused); otherwise a timestamp (+2h, tomorrow),
once, if it's ahead. It makes the task's timer; --schedules [NAME]
lists them (when, next, last run, result), and --unschedule NAME
--id=ID removes one. A remote with tasks can't be renamed or removed
(they name it): unschedule them first. Help, colors and completion
(permission modes, a remote's task ids).
| -rw-r--r-- | pkgs/claude-remote.nix | 111 |
1 files changed, 104 insertions, 7 deletions
diff --git a/pkgs/claude-remote.nix b/pkgs/claude-remote.nix index e8e0dc7..d21fe7f 100644 --- a/pkgs/claude-remote.nix +++ b/pkgs/claude-remote.nix @@ -25,6 +25,8 @@ , coreutils, findutils, gitMinimal, gnugrep, gnused, jq, systemd, tmux, util-linux # Where remotes started with systemctl are kept, see configs/claude.nix. , stateDir + # Makes a task's timer, from its definition, see configs/claude.nix. +, taskTimer }: let script = writeShellScript "claude-remote" '' @@ -41,6 +43,9 @@ let # their sessions (which make a remote interactive), see configs/claude.nix istate=''${state%/*}/interactive sessions=''${state%/*}/interactive-sessions + # tasks: their definitions, NAME:ID each, see configs/claude.nix + tasks=''${state%/*}/tasks + taskTimer=${taskTimer} home=/home/claude remotes=$home/remotes @@ -103,6 +108,23 @@ let and environment can go, on claude.ai. Not for static ones. --reset and --remove ask first (y/N), unless -f; as root, and not from the remote itself; all or nothing, like --rename. + --schedule NAME --when=TIME|CALENDAR --prompt=TEXT + [--id=ID] [--permission-mode=MODE] + A task: claude -p TEXT, in the remote's directory, as claude, + once, at a TIME, or at each elapse of a CALENDAR event (both + per systemd.time(7), a calendar event first: --when=+2h, + --when=tomorrow, --when='2026-12-24 18:00' once; --when=weekly, + --when='Mon 03:00', --when=03:00 again and again); ID: the task's + (default: the date and time, now), MODE: the permission mode, + as nobody's there to answer (default: auto). Its output goes + to the journal of claude-task@NAME:ID, and its transcript, to + the remote's. Kept across reboots; a one-time one missed then + runs after the boot (but missed recurring ones don't). + --schedules [NAME] + The tasks (of NAME): id, when, next, last run, and how it went. + --unschedule NAME --id=ID + The task, gone, its timer stopped. A remote with tasks can't be + renamed or removed: unschedule them first. -h, --help This. @@ -142,11 +164,11 @@ let # their colors, if any (a colored word follows an m, so each loop ends) paint() { if test -z "$C_OFF"; then cat; return; fi - local x='[^-A-Za-z0-9_]' c='list|inspect|start|stop|restart|attach|log|rename|reset|remove' + local x='[^-A-Za-z0-9_]' c='list|inspect|start|stop|restart|attach|log|rename|reset|remove|schedule|schedules|unschedule' sed -E \ -e "s/^([A-Z][A-Za-z ]*:)$/$C_HEAD\1$C_OFF/" \ -e ":a; s/(^|$x)(--($c)|COMMAND)($x|$)/\1$C_CMD\2$C_OFF\4/; ta" \ - -e ":b; s/(^|$x)(--interactive|--type|-f|--force|--from|--to|-h|--help|OPTION|JOURNALCTL-OPTION)($x|$)/\1$C_OPT\2$C_OFF\3/; tb" \ + -e ":b; s/(^|$x)(--interactive|--type|-f|--force|--from|--to|--when|--prompt|--id|--permission-mode|-h|--help|OPTION|JOURNALCTL-OPTION)($x|$)/\1$C_OPT\2$C_OFF\3/; tb" \ -e ":c; s/(^|$x)(NAME|OLD|NEW)($x|$)/\1$C_NAME\2$C_OFF\3/; tc" } die() { @@ -444,6 +466,9 @@ let if grep -qs "/$(unit "$name")" /proc/self/cgroup; then die "remove: this runs in $name: remove it from elsewhere (a root shell, another remote)" fi + if compgen -G "$tasks/$name:*" >/dev/null; then + die "remove: $name has tasks (claude-remote --schedules $name): --unschedule them first" + fi users=$(users_of "$name" "$remotes/$name" "$(project "$name")") test -z "$users" || die "remove: $name is in use, by more than itself:$(printf '\n %s' "$users")" rel=.removed/$name-$(date -u +%Y%m%dT%H%M%SZ) @@ -517,6 +542,9 @@ let # what it read before can bring one back) if is_static "$new"; then errs+=("$new is static (the system's configuration)"); fi if test -e "$state/$new"; then errs+=("$state/$new is there already"); fi + if compgen -G "$tasks/$old:*" >/dev/null; then + errs+=("$old has tasks (claude-remote --schedules $old): --unschedule them first") + fi for d in "$istate" "$sessions"; do if test -e "$d/$new"; then errs+=("$d/$new is there already"); fi done @@ -620,10 +648,10 @@ let # Commands and options, in any order (one set, without overlaps), and # the words, NAME (or OLD and NEW, for --rename, unless given by --from # and --to); after --, journalctl's options, for --log. - cmd= type= force= from= to= words=() jargs=() + cmd= type= force= from= to= spec= prompt= id= mode= words=() jargs=() while test $# -gt 0; do case $1 in - --list | --inspect | --start | --stop | --restart | --attach | --log | --rename | --reset | --remove | --type-of | --names) + --list | --inspect | --start | --stop | --restart | --attach | --log | --rename | --reset | --remove | --schedule | --schedules | --unschedule | --type-of | --names) test -z "$cmd" || test "$cmd" = "''${1#--}" || die "--$cmd or $1, not both" cmd=''${1#--} ;; -h | --help) colors 1; usage | paint; exit ;; @@ -631,6 +659,10 @@ let --type=server | --type=interactive) type=''${1#--type=} ;; --type=*) die "$1: the type is server or interactive" ;; -f | --force) force=1 ;; + --when=?*) spec=''${1#--when=} ;; + --prompt=?*) prompt=''${1#--prompt=} ;; + --id=?*) id=''${1#--id=} ;; + --permission-mode=?*) mode=''${1#--permission-mode=} ;; --from=?*) from=''${1#--from=} ;; --to=?*) to=''${1#--to=} ;; --) shift; jargs=("$@"); break ;; @@ -647,6 +679,8 @@ let test -z "$type" || case $cmd in start | restart) ;; *) die "--type is for --start and --restart" ;; esac test -z "$force" || case $cmd in rename | reset | remove) ;; *) die "-f is for --rename, --reset and --remove" ;; esac test -z "$from$to" || test "$cmd" = rename || die "--from and --to are for --rename" + test -z "$spec$prompt$mode" || test "$cmd" = schedule || die "--when, --prompt and --permission-mode are for --schedule" + test -z "$id" || case $cmd in schedule | unschedule) ;; *) die "--id is for --schedule and --unschedule" ;; esac test ''${#jargs[@]} = 0 || test "$cmd" = log || die "-- (journalctl's options) is for --log" # the one NAME the command takes, as n one_name() { @@ -744,6 +778,63 @@ let one_name type_of "$n" ;; + schedule) + one_name + test "$(id -u)" = 0 || die "--schedule: as root" + test -d "$remotes/$n" || die "there's no remote $n ($remotes/$n)" + test -n "$prompt" || die "--schedule: --prompt=TEXT, what to do" + test -n "$spec" || die "--schedule: --when=TIME|CALENDAR, when to do it (systemd.time(7))" + # when, per systemd.time(7), normalized (and so, checked): a + # calendar event first (so Mon 03:00 is every Monday, not today's + # only), recurring if it has a second elapse; else a timestamp, once + if cal=$(systemd-analyze calendar --iterations=2 "$spec" 2>/dev/null); then + when=$(printf '%s\n' "$cal" | sed -n 's/^ *Normalized form: //p') + case $cal in + *'Iteration #2:'*) kind=every ;; + *'Next elapse: never'*) die "--when=$spec: that's past ($when)" ;; + *) kind=at ;; + esac + else + kind=at + when=$(systemd-analyze timestamp "$spec" 2>/dev/null | sed -n 's/^ *Normalized form: //p') + test -n "$when" || die "--when=$spec: neither a timestamp nor a calendar event (systemd.time(7))" + test "$(date -d "$when" +%s)" -gt "$(date +%s)" || die "--when=$spec: that's past ($when)" + fi + id=''${id:-$(date +%Y%m%d-%H%M%S)} + case $id in *[!A-Za-z0-9_.-]* | .*) die "--id=$id: letters, digits, _ . - only" ;; esac + t=$tasks/$n:$id + test ! -e "$t" || die "$n has a task $id already (claude-remote --schedules $n)" + mkdir -p "$t" + printf '%s\n' "$n" > "$t"/remote + printf '%s\n' "$kind" > "$t"/kind + printf '%s\n' "$when" > "$t"/when + printf '%s\n' "$prompt" > "$t"/prompt + printf '%s\n' "''${mode:-auto}" > "$t"/mode + "$taskTimer" "$n:$id" || { rm -r "$t"; die "--schedule: no timer for $n:$id"; } + echo "scheduled $n:$id ($kind $when), next: $(systemctl show --value -p NextElapseUSecRealtime "claude-task@$n:$id.timer" 2>/dev/null)" + ;; + schedules) + test ''${#words[@]} -le 1 || die "--schedules [NAME] (claude-remote -h)" + printf '%-30s %-5s %-32s %-32s %-26s %s\n' TASK KIND WHEN NEXT LAST RESULT + for t in "$tasks"/*/; do + test -d "$t" || continue + t=''${t%/} + if test ''${#words[@]} = 1 && test "$(cat "$t"/remote)" != "''${words[0]}"; then continue; fi + next=$(systemctl show --value -p NextElapseUSecRealtime "claude-task@''${t##*/}.timer" 2>/dev/null || :) + printf '%-30s %-5s %-32s %-32s %-26s %s\n' "''${t##*/}" "$(cat "$t"/kind)" "$(cat "$t"/when)" \ + "''${next:--}" "$(cat "$t"/last 2>/dev/null || echo -)" "$(cat "$t"/result 2>/dev/null || echo -)" + done + ;; + unschedule) + one_name + test "$(id -u)" = 0 || die "--unschedule: as root" + test -n "$id" || die "--unschedule NAME --id=ID (claude-remote --schedules $n)" + t=$tasks/$n:$id + test -d "$t" || die "$n has no task $id (claude-remote --schedules $n)" + systemctl stop "claude-task@$n:$id.timer" 2>/dev/null || : + rm -r "$t" + echo "unscheduled $n:$id" + ;; names) # for the completion names ;; @@ -761,10 +852,10 @@ let w=''${COMP_WORDS[i]} case $w in --) return ;; - --list | --inspect | --start | --stop | --restart | --attach | --log | --rename | --reset | --remove) + --list | --inspect | --start | --stop | --restart | --attach | --log | --rename | --reset | --remove | --schedule | --schedules | --unschedule) cmd=''${w#--} ;; --type | --interactive) used="$used--type= --interactive " ;; - --from | --to) used="$used$w= " ;; + --from | --to | --when | --prompt | --id | --permission-mode) used="$used$w= " ;; -f | --force) used="$used-f --force " ;; -*) used="$used$w " ;; =) i=$((i + 1)) ;; @@ -782,13 +873,17 @@ let fi case $valueOf in --type) COMPREPLY=($(compgen -W "$other" -- "$cur")); return ;; + --when | --prompt) return ;; # free text --from | --to) COMPREPLY=($(compgen -W "$(claude-remote --names 2>/dev/null)" -- "$cur")); return ;; + --permission-mode) COMPREPLY=($(compgen -W "auto acceptEdits default dontAsk plan bypassPermissions" -- "$cur")); return ;; + --id) [ "$cmd" = unschedule ] && [ ''${#words[@]} -gt 0 ] && + COMPREPLY=($(compgen -W "$(ls /home/.var/lib/claude/tasks 2>/dev/null | sed -n "s/^''${words[0]}://p")" -- "$cur")); return ;; esac # what's left: the commands (until there's one), the command's # options, and as many names as it takes local cmds= opts= o left= max=1 c case $cmd in - "") cmds="--list --inspect --start --stop --restart --attach --log --rename --reset --remove" ;; + "") cmds="--list --inspect --start --stop --restart --attach --log --rename --reset --remove --schedule --schedules --unschedule" ;; list) max=0 ;; start | restart) opts="--type=" @@ -796,6 +891,8 @@ let rename) opts="-f --force --from= --to=" max=2 ;; reset | remove) opts="-f --force" ;; log) opts="--" ;; + schedule) opts="--when= --prompt= --id= --permission-mode=" ;; + unschedule) opts="--id=" ;; esac for o in $opts; do case $used in *" $o "*) ;; *) left="$left $o" ;; esac |
