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?
Best Answer
The
fg
job commands are connected to interactive sessions only, and have no meaning in batch mode (scripts).Technically, you could enable them with
set -m
, 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 can't.