3

I am trying to create a fun Terminal Screensaver which consists of the cmatrix package (one that turns terminal in one similar to the movie The Matrix) and xprintidle to determine idle time of the Terminal.

I took some help from this Thread answer at SuperUser and using a shell-script similar to it as follows:

screensaver.sh

#!/bin/sh
set -x  # Used for debugging
IDLE_TIME=$((60*1000)) #a minute in milliseconds

screen_saver(){
# My screensaver function
    cmatrix -abs
}

sleep_time=$IDLE_TIME
triggered=false

while sleep $(((sleep_time+999)/1000)); do
    idle=$(xprintidle)
    if [ $idle -ge $IDLE_TIME ]; then
        if ! $triggered; then
            screen_saver
            triggered=true
            sleep_time=$IDLE_TIME
        fi
    else
        triggered=false
        sleep_time=$((IDLE_TIME -idle+100))
    fi
done

The script runs flawlessly when I run it in foreground using:

./screensaver.sh

and I can see the matrix terminal triggered.

However If I run it in background with &; the function screen_saver() is triggered in the background and I cannot view it. The only possible way to see the matrix terminal is using fg which brings it foreground.

Question

Is it possible to use the fg command in the function screen_saver() like:

screen_saver(){
     cmatrix -abs && fg
}

or similar option to bring it to the foreground within the shell-script?

I wish to add this script into my .bashrc so that it actually becomes a customizable feature. Is this possible?

  • 1
    Probably not, according to: unix.stackexchange.com/questions/224562/… – thrig Mar 29 '17 at 21:33
  • While you can't use fg the way you mean, this shouldn't prevent you from seeing the output of cmatrix. Can you reproduce the problem with a small script that doesn't use cmatrix? – Gilles Mar 29 '17 at 23:10
1

The fg, job commands are connected to interactive sessions only, and has no meaning in batch mode (scripts).

Technically, you could enable them by the -m flag, but it still would make little (if any) sense, as this only relates to interactive applications, where you can send jobs to back by ^Z, but since you don't have that interaction in a shell script, it makes it pointless.

The answer is in practice, No, you cant.

1

If you use tmux it has a option for "screensaver"

set -g lock-command "cmatrix -abs"
set -g lock-after-time 10 #inSeconds

Not exactly a answer to your question.

  • Will give this a try. Kudos for tmux – Shan-Desai Mar 30 '17 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.