Previous Page
PCLinuxOS Magazine
Article List
Next Page

Two Quick, Simple Ways To Deactivate Xscreensaver Via A Bash Script

by Paul Arnote (parnote)

One of the most annoying things when you're watching a video is when your screensaver activates. Sure, and IF I remember to do it, I could go in and disable the screensaver before watching the video in my favorite media player. But, I usually “forget” that step. Yes, I could just pause the video and then go into my screensaver's settings and disable it, saving myself from continual periodic interruptions from the screensaver activating. That whole process involves several [very inconvenient] steps. But … what if there was a way to toggle my screensaver with a single mouse click? That would make the whole process WAY easier.

Spinal Tap playing in Kaffeine
This Is Spinal Tap, playing in Kaffeine

Now, I use almost exclusively the Xfce desktop. But, mainly due to a lack of screensaver options, I have Xfce's default screensaver disabled completely. Instead, I prefer to use Xscreensaver. I'm much more familiar with it, and it has many, many options available to use (even though I mostly use the “Blank” screensaver). At least, I have options available.

On my HTPC that's “permanently” attached to my living room television (it's a repurposed, refurbished desktop computer that resides on my entertainment center), I circumvent the problem in two different ways. First, I have Xscreensaver set to disabled. That alone takes care of the problem. But, just in case, the second way is that even if I didn't have Xscreensaver disabled, there is a setting in VLC (my primary media viewer on my HTPC) to disable the screensaver whenever VLC is displayed in full screen mode, which is how it's used on my HTPC. So, at least on my HTPC, the possibilities for Xscreensaver to pop on at the most inopportune times are zero.

I looked around, and I was quite disappointed by the lack of scripts or programs to quickly and easily suspend or disable Xscreensaver. There is a SUPER nice bash script over at GitHub, called LightsOn+. All 348 lines of it. But, even it fell short of what I wanted. It ONLY works with videos being displayed in various browsers, or with videos being played full screen. Even then, if you're not using one of the media players LightsOn+ is looking for (LightsOn+ recognizes MPV, Mplayer, VLC, and Totem), then the script is of limited value, unless you go in and modify it to accommodate the media player you prefer using. As it exists in the version on GitHub, if you use Kaffeine or SMPlayer (two relatively popular media players on Linux), you're (as Scooby Doo would say), “outta ruck.” There are *many* times when I'm watching a video … don't let this freak you out … in my media player's window. That means NOT full screen. Which means LightsOn+ won't help. I know. How dare I? BUT, that's how I tend to generally use my media players on my other, non-HTPC computers. So, unless I'm watching a video full screen, even LightsOn+ is of limited value to me.

Baribie playing in VLC
Barbie, playing in VLC

For what it's worth, LightsOn+ is a fork of the original LightsOn script (also available on GitHub). And, LightsOn+ is reported on GitHub to have 15 additional forks. After seeing how “complex” the LightsOn+ and LightsOn scripts are, without either meeting my needs, I didn't bother to check out all the forks. It was time to see if I could figure out another approach.

For what it's worth, while I was looking for a solution, a lot of people suggested using the playback of sound as criteria for suspending screensaver activity. But, I took exception to using that criteria, since there may be times when you're listening to audio, like streaming audio content (I sometimes listen to baseball Spring Training games streaming in the background), a podcast, or MP3 music files, when activation of the screensaver might actually be appropriate (or at least doesn't pose a problem like it does with the playback of video content).

Remember me saying I like Xscreensaver because of all the options it provides? Well, one of those options is the ability to control and interact with Xscreensaver from the command line. Also, being able to control and interact with Xscreensaver from the command line also means that you can include those commands in a bash script. Hmmmm.

So, that got me thinking. My early iterations of the script just did one thing: disable the activation of Xscreensaver. But then, when I'm done watching a video, I needed a way to restore/re-enable Xscreensaver so it could continue doing its intended job. So, I had one launcher setup to run the script to disable Xscreensaver, and another launcher just to restore/re-enable Xscreensaver. That method certainly worked, and worked well. But it was a bit inconvenient and a bit of a kludge, having to have two launchers.

So, I wondered … could I make just one script, and thus just one launcher, to do both actions? Let's just call that “challenge accepted.”

Now here's something else that's funny. Back in October 2020, I had proposed a similar “solution” in the PCLinuxOS forum. And, to be 101% honest, I had completely forgotten about that post that I made nearly four years ago. But wouldn't you know it, that “other solution” works just as well today as it did in 2020. My “other” solution is based on turning off Xscreensaver for a specified amount of time (in minutes), and then reactivating it when that time has elapsed. It seems I've been down this path before. No wonder why the scenery looked so familiar, eh? That's ok, though. It's good to have multiple ways to “solve” a singular problem. One deactivates Xscreensaver for a specified number of minutes, and the other one deactivates/reactivates Xscreensaver with a single mouse click, on a schedule YOU decide, when you decide.

We'll take a look at both scripts. I have gone back and “enhanced” the timer-based script, both to prevent the “mental acrobatics” to convert hours and minutes to the total number of minutes, and to provide some feedback. We'll take a look at the version of the script that toggles Xscreensaver with a mouse click first.

Indiana Jones playing in Mplayer
Indiana Jones: The Dial of Destiny, playing in Mplayer

Toggle Screensaver

I call the script I came up with “” It doesn't create any windows, so there's no Yad or Zenity dialog boxes. But, it works very simply and very well to temporarily disable Xscreensaver with just one click of the mouse, instead of going into the PC menu, filtering down through to the “Configuration” category, filtering down through that list of programs to Configure Xscreensaver, opening that, and then disabling the screensaver. And then you get to do that whole process again after you're done watching your video, just to re-enable the screensaver. IF you even remember to re-enable Xscreensaver after you've watched your video(s).

You can download the script from the magazine's website. As usual, download the script to the location where you store your scripts, strip the “.txt” file extension off of it, and mark the file as “executable.”

First, below is the script. Then, we'll go through the script, line by line, explaining what's happening.

The first thing you might notice is that my script is nowhere near 348 lines of bash code. Nope. I've accomplished what I needed in just 17 lines of bash code. That's just less than 5% of the number of lines of bash code in the LightsOn+ script. So far, so good. Small and economical. In fact, the whole script is only 520 bytes long.

Notification screensaver on

Line 1, of course, is the typical opening stanza in any bash script … the bash script shebang that tells bash that yes, this is a bash script. Line 3 is the start of an if-then-else statement, and it checks to see if a “lock” file exists in the user's ~/tmp directory. That will mean that the screensaver is already disabled. In line 4, if that “lock” file is found, it is removed, restoring normal functionality to Xscreensaver by exiting the script. There's nothing in that file, but the presence/existence of that file is key. So we know that something has occurred, (line 5) we use notify-send to pop up a notification message (shown above) in/near your notification area. In our use of notify-send, we use the “org.xfce.ScreenSaver” icon to provide a graphical element to the notification. If you do not have this icon on your installation, you can change the icon to one that is on your system. Or, you can just eliminate the “--icon=...” part of notify-send altogether (but you'll end up with a plain vanilla notification). Then, we tell the script to exit in line 6.

Notification screensaver off

Of course, if the “lock” file doesn't exist, it skips right over the first part of the if-then-else statement and moves right on to the “else” part of that conditional statement. That first line in the “else” part of the if-then-else statement (line 8) uses the “touch” command to create the “lock” file. Line 9 uses the notify-send command to display a message in/near your notification area that Xscreensaver has been “turned off” (image above) In actuality, it isn't turned off. Rather, it's just disabled. Line 10 starts up a while-do loop that returns true if it finds the “lock” file (which it should, because we just created it). For as long as that “lock” file exists while the script is executing, it will stay in the while-do loop, executing the commands within that loop over and over again. We can safely ignore line 11, because it's just a comment line. In line 12, we tell the script to “sleep” for 30 seconds. After that 30 seconds has elapsed, line 13 tells Xscreensaver, via the xscreensaver-command - deactivate command, to disable the screensaver. Any output from the xscreensaver-command is redirected to the Linux “black hole” known as /dev/null, to prevent any nuisance messages (just in case you're running it from the command line in a terminal). Line 14 closes up the back end of the while-do loop, and exits the script, while line 15 closes up the back end of the if-then-else loop. Finally, line 17 tells the script to exit cleanly.

That's really it! If you activate the script without the presence of the “lock” file, the “lock” file is created, and Xscreensaver is disabled by issuing the “-deactivate” command every 30 seconds. If you activate the script again, and the “lock” file is found, that “lock” file is removed, and the normal functionality of Xscreensaver is restored.

App launcher 1  App launcher 2

I created this script to be run from a launcher, either on one of your panels (my personal preference), or on your desktop. The images above show the launcher defined on my Xfce desktop, and the “Edit Launcher” dialog box where you fill in all of the necessary data for that launcher.

As I mentioned earlier, I use the Xfce desktop pretty much exclusively, coupled with Xscreensaver (instead of Xfce's default screensaver, xfce4-screensaver). If you use Mate, KDE, LXQt or some other desktop, this script will work exactly as described here, just so long as you're using Xscreensaver to manage your screensavers. If you are, however, using a different screensaver management program (e.g., Kscreensaver, etc.), then you may be able to alter the script to call on that particular screensaver management program, instead of Xscreensaver. But, since I don't run or use the “other” desktop flavors, I can't really say with a lot of certainty what it is you could/would/should/might do to accommodate any other screensaver but Xscreensaver. And, as far as I know, Xscreensaver, with all of its many options, is desktop-agnostic, and should be able to run on any desktop.

Timed Screensaver Deactivation

This is an “enhanced” version of the script I posted in the forum four years ago (you know … the one I posted in the forum and “forgot” about). The original version required the user to do the “mental acrobatics” of converting hours and minutes into the total number of minutes to deactivate Xscreensaver. This version has separate entry dialogs for hours and minutes, relieving the end user of having to perform those calculations themselves. This “enhanced” version of the script also displays a notification when Xscreensaver is deactivated, complete with displaying the amount of time that Xscreensaver has been set to be deactivated. Then, 15 seconds before Xscreensaver reactivates, another notification pops up to let the end user know that Xscreensaver will be reactivated.

Unlike the “toggle” version, this timed version of the script to deactivate Xscreensaver uses Zenity dialog boxes for the end user to enter the number of hours and the number of minutes to deactivate Xscreensaver. That, coupled with the time “calculations” in the script, make this version of the script a little bit longer. Specifically, this version of the script is 1,068 bytes long, or just a scad more than 1.0KiB.

You can download the timer-based version of the script from the magazine website. Just as with all of the other scripts you can download from the magazine website, save it to where you normally store your scripts, remove the “.txt” file extension, and mark the file as being “executable.”

So, here's the timer-based version of the script to deactivate Xscreensaver.

Screensaver toggle with timer

Screensaver timer hours

So, let's look at what's going on in the script. As with all bash scripts, Line 1 is the “shebang” that all bash scripts start with. In lines 3 through 7, the user is presented with a Zenity dialog box to enter the number of hours you want to deactivate Xscreensaver (image above). That number is quietly converted to minutes. If the user clicks on the “Cancel” button, the script exits.

Screensaver timer minutes

Lines 9 through 16 present a Zenity dialog box to allow the user to enter the number of minutes to disable the screensaver (image above). So, let's say you're watching a video/movie that is 1:47 (one hour, 47 minutes) long. You would enter “1” for the hours, and “47” for the minutes (or you could round it up to 1:50, if you so choose). If the number of minutes is less than zero or greater than 59 minutes, an error message is displayed, and the script exits. If the user clicks on the “Cancel” button, the script exits.

Screensave time notification

Line 18 adds the total number of minutes, and stores them in the variable named “TIME”. Line 20 uses the “printf” command to format the message that we'll display in the notification that Xscreensaver has been turned off for HH hours MM minutes. The “printf” command gives us a LOT more options to format the text the way we want, compared to the more common “echo” command. Line 22 uses the “notify-send” command to display that notification message (image above).

Then, lines 24 through 29 set up the “while … do” loop that decrements the number of minutes ($TIME) once every minute, and issues the “xscreensaver-command -deactivate” command on every pass through the while … do loop. All output from the “xscreensaver-command -deactivate” command is sent to the Linux black hole, known as /dev/null, to suppress any output from the xscreensaver-command. It then “sleeps” for 60 seconds, and continues in that manner until the specified amount of time is up (when $TIME reaches zero).

Timer reactivation time

With line 31, which is executed when the specified amount of time has elapsed, another notification is displayed (via the “notify-send” command) to inform the user that Xscreensaver will be reactivated in 15 seconds (image above). Line 32 sets up the 15-second delay, before line 33 issues the “xscreensaver-command -activate” command to reactivate Xscreensaver. Any output from that command is, again, sent to /dev/null to prevent any “nuisance” messages from being displayed (should you be launching this from the command line). Finally, line 34 exits the script cleanly.

You could very well alter the script to NOT activate Xscreensaver. The “xscreensaver-command -activate” command starts Xscreensaver immediately. If you don't issue this command, Xscreensaver will activate on its own, since the specified amount of time has elapsed and Xscreensaver is no longer being suppressed with the “xscreensaver-command -deactivate” command being issued every minute. So, if you choose this route, you can comment out the “sleep 15s” and “xscreensaver-command -activate” commands (make the first character on each line a # symbol), and change the message in the notification popup to “Screensaver now active.” I think it's still important enough, though, to let the user know that Xscreensaver has resumed its “normal duties.”

Also, you may use whatever icon you wish for the notification command(s). I chose “preferences-desktop-screensaver” because it's relatively unused, is slightly different from the icon used in the script, and it best reflects the purpose of the script. In fact, you may need to change this icon image simply because you may have different icon sets installed on your computer than I have installed. I like the Faenza icon set on Xfce, so my choice of icons is somewhat dictated by what icons that icon set includes.

Edit launcher

This is the “Edit Launcher” window in Xfce. I've entered the “Name” as “Timed Screensaver,” and “Deactivate Xscreensaver for the specified amount of time” in the “Comment” field. The command is changed to “” in the “Command” field, and I've selected a clock icon to better indicate that this script is time-based. On my copy of Xfce, my launcher is on my top panel. You can also place this launcher on your desktop, if you so choose.

Wrapping Up…

We didn't really do anything “fancy” here with either of these two scripts. We didn't use dBus paths to enable and disable the screensaver (which is possible, and one method which scripts like LightsOn+ employ). I just wanted to keep things as simple as possible. While I'm not calling out or disrespecting the authors of scripts like LightsOn+ or any of its derivatives (props to them for creating them), I didn't think it required anything as complicated or complex as what they created. At the end of the day, those scripts only work if you're watching a video full screen, or in a browser. But, if you're watching a video or movie in a media player window that is NOT playing full screen, you're outta luck with those “other” scripts. I really set out to keep things as simple as possible. This is, after all, PCLinuxOS, and the PCLinuxOS motto is “Radically Simple.”

As a result, I have presented not one, but two MUCH leaner bash scripts that essentially achieve the same end result. If you read through the documentation for the “xscreensaver-command -deactivate” command, one of the things it mentions is the ability for it to be called repeatedly from a bash script to inhibit the activation of Xscreensaver … which is exactly what this script does. And, this script works regardless if you're watching a video or movie, or not. Let's say you have a complex/complicated process going on that you want to monitor. You might not want the screensaver activating, and interrupting your oversight of the process(es). Any time you want or need Xscreensaver to “pause” its normal activities, these small scripts can be used to deactivate it, and then run again to return Xscreensaver to its normal duties and functionality (in the case where we toggle Xscreensaver off and on), or for a specified amount of time before Xscreensaver is reactivated without any further actions on your part (in the case of the timed version).

Now, at last, I can watch a video or movie in my favorite media player(s) without having to worry about the screensaver activating right in the middle of a key scene, and without having to burrow deep into the PC menu to bring up the Xscreensaver setting. Those “favorite” media players, on my computers, are VLC and Kaffeine … and Kaffeine isn't even an option with LightsOn+, without modification of the script. Now, it's one click of the mouse to disable the screensaver, and one click of the mouse to return things to “normal,” regardless of what media player I'm using, or regardless of the reason I'm wanting to suspend activation of Xscreensaver. Or, you can disable the screensaver for a specified amount of time. Linux is all about “choice,” and in this case, the choice is yours as to which one you decide to use.

Previous Page              Top              Next Page