Get notifications on your desktop or phone after completing a long-running command with Noti

Have you ever wanted to be notified not only on your desktop but also on your phone after a long-running process/command is completed? Noti can do it.

Notti It is a command line tool for macOS, Linux and Microsoft Windows, used to monitor processes/commands, and trigger notifications on desktops or phones after finishing running.

Supported notifications:

  • Desktop notification (banner)
  • Speech
  • BearyChat
  • Key library
  • the most important
  • Push bullet
  • push ups
  • Push safe
  • Single push
  • relaxation
  • telegraph
  • Zulip
  • Teverio

All these supported notifications can run on macOS, Linux and Windows.

It’s worth noting that if you only want to receive desktop notifications after completing a long-running command on Linux, you just need to run command; notify-send Done (E.g sleep 3; notify-send Done).

By default, Noti only displays the text “Done!”. When a long-running task is completed, it can be extended. You can specify the notification title (default is the utility name), set the notification message (default is “Done!”; use the following command to read from stdin) -), and display the execution time in the notification message.

You can use Noti at the beginning of the command, for example:

noti sleep 3

And after the command, for example:

sleep 3; noti

You can even use Noti after the command is started by obtaining the process PID and then using the following command:

noti --pwatch PID

Or press Ctrl + z After the process begins to temporarily hang, then use the following command to resume it:

fg; noti

For more information about Noti, including how to use various services, Noti environment variables, etc. to set Noti notifications, please see This page. During testing, I noticed that this page is a bit outdated. For example, Slack no longer allows the creation of old-style tester tokens.Therefore, you may also want to check the Noti man page from hereAnd noti.yaml man from here.

Read on for information on how to install and configure Noti, including complete instructions for setting up Noti, which can be notified on your mobile device via Telegram and Slack when the long-running command is completed.

You might also like: How to repeat a command every X seconds on Linux

Install and configure Noti

The Noti installation instructions linked using the button above contain multiple ways to install this tool.If you want to get the binary file directly, just go to the tool’s GitHub release page.

The easiest way to install Noti on Linux is to download the binary file from the release page, unzip the archive file, and install noti Binary to /usr/local/bin Use (assuming you have unzipped the Noti binary archive into the home folder and opened the terminal in the home directory):

sudo install noti*linux-amd64/noti /usr/local/bin

By default, Noti uses desktop notifications (banners). Below, I will show you how to configure Noti to send notifications on your mobile device via Slack and Telegram after the long-running process is over, because this is what I use. If you want to use it with other services, you need to configure it yourself according to the instructions (although I think it is somewhat incomplete and out of date in some cases, for example, you can no longer create a Slack token and you must use a s application) from here.

You might also like: Bit is a modern Git CLI with interactive prompts

Configure Noti to notify you when a long-running command finishes running by sending a message on Telegram

To make Noti send you a telegram message after the long-running command has finished running, you need a telegram chatId with token. This applies to telegram chats or channels. In the following description, we will use the Telegram channel.

1. Create a telegram channel.

The first thing you need to do is to create a Telegram channel. It can have any name and description you want.

2. Create a Telegram bot and get the HTTP API access token.

Next, you need to use Father of Zombies (@Botfather) Create a bot and get an HTTP API access token.

To do this, start a chat with @botfather and type /start View available commands and more in the chat.Type now /newbot Create a new robot and follow the instructions (you need to set a name and username for the new robot). Upon completion, @botfather will display a token to access the HTTP API. Copy this token because you will need it later.

3. Add the Telegram bot created in step 2 to your channel and get chatId.

Now we have a token, but we also need to get the chatId.

Add the bot you created under step 2 to the new Telegram channel (Go to that channel and select from the options Add users And search for the robot you created.

After adding the bot, type a message in the Telegram channel.This is important and essential in order to obtain chatId.

Now you can get chatId You will have to edit it by visiting the following link:

https://api.telegram.org/botXXX:YYYYY/getUpdates

instead XXX:YYYYY Use the robot’s HTTP API token you obtained under step 2.

This link will display a bunch of text, where you will find:

chat":{"id":-100999999999}

The ID from here is yours chatId You need to use it with Noti.that - The symbol at the beginning of the number is chatId, So make sure you also use that number, not just the number.

4. Create a Noti profile (if not already created), and then add Telegram to it.

Now we know the telegram chatId And HTTP API token, Create a Noti configuration file named noti.yaml in ~/.config/noti/

Open this file with a text editor and paste the following content:

telegram:  token: THE_HTTP_API_TOKEN_GOT_UNDER_STEP_2  chatId: 'THE_CHAT_ID_GOT_UNDER_STEP_3'

Example (edited to not include my real name token with chatId):

telegram:  token: 1598888700:AEEijJJcsABpYR1LdJ4_Llkr1Vq48vxAUSg  chatId: '-10099999999999'

Use the following command to test Noti sending a telegram message after the long-running command finishes running:

sleep 3; noti --telegram

Don’t worry, you don’t need to add the –telegram command line parameter every time you use Noti.You can export NOTI_DEFAULT And a list of notification types to be triggered, for example, to use desktop banner notifications and telegram notifications, you need to export NOTI_DEFAULT="banner telegram".

Add to export NOTI_DEFAULT="banner telegram" Give you ~/.profile File to make the changes permanent.Obviously, edit NOTI_DEFAULT The type of notification you want to use.Log out and log back in after changing your account ~/.profile File to use the new settings.

You may also be interested in the following: How to use find to find files modified in the last N days or minutes

Configure Noti to notify you when a long-running command finishes running by sending a message on Slack

The Noti documentation page mentions creating OAuth tokens for testing and development, but this is No longer supported by Slack. Instead, we need to create a Slack application.

1. Use a web browser to log in to Slack.

2. Create a Slack application.

Go to https://api.slack.com/apps/new And create a new Slack app with any name you want.

3. Activate incoming Webhooks.

After creating the Slack application, you will be redirected to the settings page of the new application.here has Basic Information Select in the left sidebar), expand Add features and functionality Section and click Incoming Webhooks.Here, turn on the switch next to it Activate Incoming Webhooks.

4. Create an incoming Webhook.

at the bottom Incoming Webhooks On the page, click Add New Webhook to Workspace, Then choose a channel or yourself (the latter is in Direct Messages). You will be notified here. I chose my username here.

[[Edit]]It also explains the steps of creating a Slack application and passing in a Webhook. Slack API documentation.

After creating the incoming Webhook, you will be redirected to Incoming Webhooks page.At the bottom of this page, you should see Webhook URL. Copy this URL.

5. Create a Noti profile (if not already created), and then add Slack to it.

Now that we have the Slack Webhook URL, create a Noti configuration file called noti.yaml in ~/.config/noti/

Open this file with a text editor and paste the following content:

slack:  appurl: 'https://hooks.slack.com/services/xxx/yyy/zzz'

Here, replace appurl The value of the Slack Webhook URL you got under step 4, and then save the file.

Use the following command to test Noti to send a Slack notification when the long-running command finishes running:

sleep 3; noti --slack

Always use Slack to send notifications as you type noti (So ​​you don’t need to enter noti --slack You can export every time you want to receive a notification on Slack NOTI_DEFAULT And a list of notification types to be triggered. For example, to use desktop banner notifications and Slack notifications, you need to export NOTI_DEFAULT="banner slack".

Add to export NOTI_DEFAULT="banner slack" Give you ~/.profile File to make the changes permanent.Obviously, edit NOTI_DEFAULT The type of notification you want to use.Log out and log back in after changing your account ~/.profile File to use the new settings.

You might also like: Bash history: how to display the timestamp (date/time) when executing each command

Source

Related Posts