Monday, August 20, 2012

An Easy Way to Turn Off Monitor on Linux and Other Operating Systems With Just a Click

There are many situations when we need to keep our computer running without using monitor. It usually happens when we are downloading any large file or when we are leaving our computer for a short while. In such a case, most of us leave our computer monitor turned ON even though it is not required until the power saving system automatically turns it off after some time. Monitor consumes a significant amount of power. So, turning it off when not in use will save power.


You might be thinking "why not use the button provided to turn off our monitor?"
Yes, you can do that way but not always. It can cause wear and tear and the button may get damaged sometimes as it has happened to me now (esp if you had an old monitor). Also, this button is not provided for most laptops these days. In this article I will tell you an easy solution to turn Off your monitor on Linux.


Conventional Method On Linux

Unlike other operating systems like Windows, etc, Linux (X.org) provides an easy way to do this. We can turn off the monitor by running the following command from CLI (terminal).


$ xset dpms force off

But opening the terminal and rememering and typing this long string often is quite painful. I use Linux for most of my work. I used this command for quite a long time hesitantly to turn off my monitor. Whenever I use this, i always used to question myself "Why not make it more simpler?". At last, i made it a bit simple.

Smarter Method to Turn Off Your Monitor

I felt it would be a better approach to have an easy to access button to do this. I have made a small program to solve this problem. Download this tiny utility "Turn-Off-Monitor" and put it on your desktop (after unzipping).
Click here to download.
Turn-Off-Monitor Linux Executable Screenshot

Run the executable by double clicking on the icon to turn off your monitor. You can wakeup your monitor again by simply pressing any key or by moving your mouse. I have tested this on Ubuntu 12.04. However, this utility should work on most Linux distributions like without any problem.

If it is not running, ensure that it is marked as executable. Ubuntu users can do this by right clicking on the file Properties Permissions. In this section, check the option "Allow executing file as program". If you are using any other Linux distribution, you can mark a file as executable from the terminal by using the command
$ chmod +x file_name_with_path

This is How I Did

The executable above is an output of just a few lines of C code as given below
//Start of C Code
#include <stdlib.h>
int main(void){
 system("xset dpms force off");
return 0; 
}
//End of C Code

The system function is used to execute the execute the operating system commands. I hope you understood. It looks simple but quite useful. I think it can also be done using shell script, etc too. I would like to hear more about this from you.

Make It More Easier

Would'nt it be cool if we have a keyboard shortcut for this? I have tried a tutorial from HowToGeek for this but it did not work for me. Please share your suggestions on this.

What About Other Operating Systems?

I think BSD also uses X. So this hack should also work there. Coming to Windows, I did not find any direct way to do this. But there is a third party command line utility called nircmd.exe. You can download it and use the command
nircmd.exe cmdshortcut "~$folder.desktop$" "Turn Off Monitor" monitor off 
to create a desktop shortcut for turning off your monitor. But let me warn you that nircmd.exe is considered as potentially dangerous application by a few anti-malware softwares. There is one more application called sleeper for windows which I did not try.

Enjoy!

I hope you like this. I would like to hear feedback from you regarding this article. Please leave a comment below.

7 comments:

  1. cat >> turn-off-monitor
    #!/bin/sh

    xset dpms force off
    (Press Control-D)
    chmod +x turn-off-monitor

    ReplyDelete
  2. Why in the name of baby jesus did you write a C program just to run a system() call?

    ReplyDelete
  3. @Anonymous
    Because I do not know shell script.

    ReplyDelete
  4. thank you it helped me (ignore all the people criticising you, at least you are trying to be helpful)

    ReplyDelete
  5. Dear Deekshith, your executable idea works better than the shell script. Thank a lot for that.
    Here is the shell script if you want to try (put it in ~/Desktop/turn-off-monitor):
    #!/bin/bash
    exec xset dpms force off

    The above two lines is the shell script to do this task.
    It works but the problem is that it always opens a dialog box asking whether to run it or not.
    So, your approach of making an executable is better. Before I saw your post, i used to use terminal to do that.
    Thank you.

    ReplyDelete
  6. It works like an obedient kid

    ReplyDelete
  7. for window systems code will go like this :
    #include
    #include

    int main()
    {
    std::cout<<"3...\n";
    Sleep(1000);
    std::cout<<"2...\n";
    Sleep(1000);
    std::cout<<"1...\n";
    Sleep(1000);
    std::cout<<"Turning monitor off\n";
    SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) 2);
    return 0;
    }
    I was having hard time converting it for ubuntu. thanx

    ReplyDelete