How to Display Some Message on a Terminal Every Time

Asked 2 years ago, Updated 2 years ago, 94 views

When I work on a local terminal, I sometimes feel dazed.In such cases, I would like to regularly display messages on the terminal every hour to remind you, but is there the safest way to do so?

For example, if you simply want messages to be displayed regularly, write a shell script with an infinite loop like the one below and run it in the background (Ctrl+zbg), and you'll get a message for now:

while true
 do
     echo "\n"
     echo `date`
     sleep10s
 done

However, if there is a command in progress, for example, a message will barge in and get in the way, and you will have to use the kill command to end it, so I don't think it's a good idea.Please let me know if there is a good way.

Also, I know that notify-send can display balloons, but I don't expect such a method (it's preferable to be on the terminal).

Your shell is bash and your operating system is Ubuntu14.04.

linux bash

2022-09-30 16:43

2 Answers

If the problem is that the location will be out of place if you leave it as it is, it might be a good idea to switch locations in the escape sequence.

Specifically

echo-n-e"\033[s\033[1;40H\033[07;37m ---ALERT---\033[m\033[u]

will be

  • -n—Do not add a new line
  • -e —Use escape characters
  • \033[s:Save Current Cursor Location
  • \033[Y;XH:Change the cursor position
  • \033[07;37m:Inverting characters to white
  • \033[m:Returns the color of the character.
  • \033[u:Return the cursor to the location where you saved it.


2022-09-30 16:43

To perform periodic display (cursor movement and string output) on one terminal without interrupting other processes or input echo back, you cannot fully respond without using a terminal multiplexer such as screen or tmux.

With screen, it is easy to display the time on the status line.For example, if ~/.screenrc describes the following configuration and launches screen, the date and time will be displayed in the status line:

hardstatus alwayslastline'%m of %d%02c:%s'

Even if it is not a status line, you can split the screen and have one of them execute any command periodically.The new screen will help you with the layout functionality. You can run commands on one screen periodically and on another by writing through /.screenrc as follows:

screen0
screen-t date 99 sh-c "while:; do clear; echo-n$(date'+%F%T'); sleep10; done"

layout new 0:layout 0:horizontal

layout select 0
split

focus down
resize-v1
select 99

focus up
select 0


2022-09-30 16:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.