как отправить уведомление, если выполнение команды терминала занимает более x секунд?

#unix #terminal #zsh

Вопрос:

Я ищу способ отправить себе уведомление о любом процессе, который занимает более 60 секунд.

Чтобы отправить уведомление, я могу использовать что-то вроде

 notify-send -t 1 "hey command finished"
 

Есть ли способ сохранить config файл где-нибудь или автоматизировать это поведение в моем zsh ?

Похоже на этот неотвеченный вопрос с другого веб-сайта

Ответ №1:

Добавить в свой .zshrc файл:

 notify() {
  emulate -L zsh  # Reset shell options inside this function.

  # Fetch the last command with elapsed time from history:
  local -a stats=( "${=$(fc -Dl -1)}" )
  # = splits the string into an array of words.
  # The elapsed time is the second word in the array.

  # Convert the elapsed minutes (and potentially hours) to seconds:
  local -a time=( "${(s.:.)stats[2]}" )
  local -i seconds=0 mult=1
  while (( $#time[@] )); do
    (( seconds  = mult * time[-1] ))
    (( mult *= 60 ))
    shift -p time
  done

  (( seconds >= 60 )) amp;amp; 
      notify-send -t 1 
          "hey command '$stats[3,-1]' finished in $seconds seconds"

  return 0  # Always return 'true' to avoid any hiccups.
}

# Call the function above before each prompt:
autoload -Uz add-zsh-hook
add-zsh-hook precmd notify