0

I comeback with a very strange behavior

when we run this command on Linux redhat machine

echo "$(tput bold)" start write to log  "$(tput sgr0)" >> /tmp/log.txt

we get ended bold text in /tmp/log.txt

more /tmp/log.txt
start write to log  <----- BOLD TEXT

but when we run it from cron job under /etc/cron.d

*/1 * * * * root [[ -f /home/mng_log.sh ]] &&   echo "$(tput bold)" start write to log  "$(tput sgr0)" >> /tmp/log.txt

then the text in /tmp/log.txt isn't bold

why the cli from cron job not write the bold test ?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
yael
  • 12,598
  • 51
  • 169
  • 303

1 Answers1

3

tput bold writes the character sequence that is to be used to tell the current terminal it is running in to start writing in bold.

It knows the type of the terminal based on the value of the $TERM environment variable. That variable is set by terminal emulators or by getty.

tput queries the termcap or terminfo databases to know what sequence to use for a given attribute for a given terminal type.

For instance, when running in an xterm, where $TERM will be something like xterm or xterm-256color, tput bold will write \e[1m which is the sequence recognised by xterm (and most modern terminal emulators) to start writing in bold. When running in an hpterm, it will send \e&dB instead.

When a script is running from cron, it is not running in a terminal. If you want it to send a sequence to enable the bold attribute, you need to tell it for what terminal that should be, by setting the $TERM environment variable.

Maybe something like:

export TERM="${TERM-xterm}" # set $TERM to xterm if not set
printf '%s\n' "$(tput bold)start write to log$(tput sgr0)" >> /tmp/log.txt

Then your /tmp/log.txt will contain the xterm sequence to turn bold on. When the content of the file is sent to a xterm terminal emulator, it will be displayed in bold, YMMV for other terminals.

Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
  • thank you so much for the great answer , let me check your solution and I will back to you ......... – yael Aug 06 '19 at 13:34
  • after testing its not work – yael Aug 06 '19 at 13:53
  • we set it in the script - export TERM="${TERM-xterm}" , bot still test isnt BOLD -:( – yael Aug 06 '19 at 13:53
  • any other option? – yael Aug 06 '19 at 14:03
  • @yael just use the escapes directly `printf '\033[1m%s\033[m\n' "start write to log"` instead of that `tput` command subst overkill. –  Aug 06 '19 at 14:14
  • 2
    @yael or much better, don't put binary cr^Wescapes in text files, write just `'## start write to log ##'` and then use postprocessing, syntax highlighting or similar when displaying the log. –  Aug 06 '19 at 14:19
  • @mosvy, another device independent way to mark text in bold is to use the roff-style `t\bte\bex\bxt\bt` approach (recognised by pagers and invisible when sent to terminals). (not that I would recommend it for a log file either). – Stéphane Chazelas Aug 06 '19 at 14:24
  • Dear Stéphane , see please my answer , it is working when I set it – yael Aug 06 '19 at 14:50
  • 3
    @yael; Stéphane's answer sets TERM only if it's not already set, so you might be running into a situation where you've already set TERM to something that doesn't support the bolding sequence. – Jeff Schaller Aug 06 '19 at 14:54