0

I am trying to create a script to use a movie as ScreenSaver, but once the movie is opened, the system freezes and reboots.

I have been pulling hairs off of my head because I cannot figure out why this happens. I tried the same script in another machine and it worked perfectly for many months...

The machine it worked on was a Linux Mint 13 machine and the one it didn't work on was a Linux Mint 17 machine.

The script is the following:

#!/bin/bash
screen_on=false;
state=0;
time_idle=1200;

while true;do

IDLE=$(./idletime)


if [ $IDLE -gt $time_idle ];then

    if [ $(pidof mplayer) ];then

        echo "Screen is on " >> mylog.log

    else
        ./test.sh &

    fi

else
    if [ $(pidof mplayer) ];then

        pkill mplayer

    else
        echo "Screen is off." >> mylog.log

    fi

fi

done

The idletime program is actually the same as xprintidle... It uses the X Server to get the system's idle time. The test.sh script is as follows:

#!/bin/bash

mplayer -nostop-xscreensaver movie.mp4 -fs -loop 0

Thank you!

1 Answers1

3

What's probably happening is a Denial of Service attack.

If you have two mplayer processes, your script starts bombing the system, starting an infinite number of mplayers as quickly as possible.

Use shellcheck. It would have warned you about missing quotes in if [ $(pidof mplayer) ]. The correct code is:

if [ "$(pidof mplayer)" ]
then
  echo "There is one or more mplayer processes"
else
  echo "There are no mplayer processes."
fi

It can be more directly written as

if pidof mplayer > /dev/null
then 
  ...

PS: You can also replace while true with while sleep 1, which will reduce the script's CPU usage from 100% to <1% with no loss of functionality.

that other guy
  • 116,971
  • 11
  • 170
  • 194
  • I don't see how the script would cause that to happen but yes as soon as it did it would absolutely start spawning `mplayer` processes as fast as it possibly could. An explicit lock for this would be a much better idea. – Etan Reisner Aug 18 '14 at 23:35
  • 1
    Apart from external factors it could be a race condition when test.sh is started in the background. – that other guy Aug 19 '14 at 00:43
  • True, with a while loop as greedy as that it could absolutely be racing the process spawning. – Etan Reisner Aug 19 '14 at 01:59