Skip to content

Instantly share code, notes, and snippets.

@StephenHwang
Created August 18, 2022 19:59
Show Gist options
  • Save StephenHwang/69b3b925925102abb9214e46594e1bbf to your computer and use it in GitHub Desktop.
Save StephenHwang/69b3b925925102abb9214e46594e1bbf to your computer and use it in GitHub Desktop.
Local notification of server-ran command completion/exception

Server command completion watcher

Commands ran on a server may take underdetermined amount of time. With tmux allowing for multiple windows and panes, I found it disruptive to jump around checking whether a command completed running. This gist outlines my solution using port forwarding and ncat to listen for connections and play a sound and pop-up notification on completion or error.

Set up

Local

Set up the local machine to recieve notification from server. The used ports are only for example; and should be changed to user's preference.

SUCCESS_PORT=11111
FAILURE_PORT=11110

ncat -klc "paplay /usr/share/sounds/freedesktop/stereo/complete.oga && notify-send "Failed""    localhost $SUCCESS_PORT &
ncat -klc "paplay /usr/share/sounds/freedesktop/stereo/complete.oga && notify-send "Succeeded"" localhost $FAILURE_PORT &
ssh -fNR $SUCCESS_PORT:localhost:$SUCCESS_PORT user@remoteserver
ssh -fNR $FAILURE_PORT:localhost:$FAILURE_PORT user@remoteserver

Server-side

Set up the server-side scripts and alias.

Create executable file success_ding.sh with the proper port:

#!/bin/bash
# success_ding.sh

SUCCESS_PORT=11111

ncat --idle-timeout 1s localhost $SUCCESS_PORT 2>&1 | grep -v "Ncat: Idle timeout expired (1000 ms)."
echo -n ""

Create executable file failure_ding.sh with the proper port:

#!/bin/bash
# failure_ding.sh

FAILURE_PORT=11110

ncat --idle-timeout 1s localhost $FAILURE_PORT 2>&1 | grep -v "Ncat: Idle timeout expired (1000 ms)."
echo -n ""

Alias the command to easily run scripts in .bashrc:

alias ding=success_ding.sh || failure_ding.sh

Usage

Once set up is completed, use by appending && ding to commands to be notified on you local machine upon successful completion or error.

command && ding

For long-running commands that have already been started but not tracked, use ctrl+z to stop the process and use fg to unsuspend the process with && ding appended.

command

ctrl+z

fg && ding

Example

Example commands to test usage. The ncat command can be changed to desired notification sound or message.

(exit 0) && ding     # succeed
(exit 1) && ding     # fail
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment