3

I want to turn off monitor on a specific period of time automatically (for example between 07:00 and 11:00). Is there any command to be set in crontab file? Is there any better way?


In summary:

Turning off the monitor at 07:00 automatically.

Turning on the monitor at 11:00 automatically.

  • scren-saver & lock screen are disabled!
mini
  • 2,325
  • 8
  • 35
  • 53
  • 2
    Close-voter(s): The answer here is generally applicable to other people who want something similar--even if the OP never replied back this should still not be closed as not reproducible. (Also, the OP said it would take about one day to come back and check if the answer worked.) If there's some other reason to close this, I recommend commenting. – Eliah Kagan Sep 06 '13 at 03:09

2 Answers2

4

Assuming that you are using the default gnome-screensaver for Ubuntu, open a terminal and run next commands followed by instructions:

  1. mkdir -p bin - this command will make a bin directory in your home folder if you don't already have it.
  2. gedit ~/bin/screen_on_or_off.sh - this will create the new file screen_on_or_off.sh in gedit.
  3. Copy and paste the next script:

    #!/bin/bash
    
    export DISPLAY=:0 #very important if you want to be runned by a cron job
    
    current_hour=$(date +"%k")
    
    # Defining the disable_screensaver function
    function disable_screensaver {
        # Disabling sleep time
        # 0 value will never turn the screen off; you can change this value as you wish:
        # for example to turn the screen of after 10 min, use 600
        gsettings set org.gnome.settings-daemon.plugins.power sleep-display-ac 0
        gsettings set org.gnome.settings-daemon.plugins.power sleep-display-battery 0
        gsettings set org.gnome.desktop.session idle-delay 0
    }
    
    # Defining the enable_screensaver function
    function enable_screensaver {
        # Enabling sleep time to 1 second
        gsettings set org.gnome.settings-daemon.plugins.power sleep-display-ac 1
        gsettings set org.gnome.settings-daemon.plugins.power sleep-display-battery 1
        gsettings set org.gnome.desktop.session idle-delay 1
    
        notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "Let's go to sleep now!"
    }
    
    if [ "$current_hour" -ge "7" ] && [ "$current_hour" -lt "11" ]; then
        enable_screensaver
    else
        disable_screensaver
    fi
    
    exit 0
    
  4. Save the file and close it.

  5. Go back into terminal and run: chmod +x screen_on_or_off.sh - to grant execute access for the script.
  6. Just for test, to run your new script, type in terminal ~/bin/screen_on_or_off.sh.
  7. Edit the crontab entries using crontab -e command (by default this will edit the current logged-in users crontab) and add the following line:

    */1 * * * * /home/$USER/bin/screen_on_or_off.sh  #change $USER with your username
    

    I have set the cron job for every minute, but you can change as you wish or as you think is better. See http://en.wikipedia.org/wiki/Cron in this sense.

  8. Save the file and check the new crontab entry with crontab -l.

Now your screen will turn off every day between 7 and 11.

kiri
  • 28,246
  • 16
  • 81
  • 118
Radu Rădeanu
  • 169,590
  • I've disabled gnome-screensaver and lock-screen, this is the reason that I want to turn off the monitor by command line automatically! Another note: The computer is at work, I'm on vacation now; however, I'll check your instruction tomorrow and put a feedback here :) – mini Jun 30 '13 at 17:54
3

Newer and much simpler answer, put this in cron:

0 07 * * *  export DISPLAY=:0 && xset dpms force off
0 11 * * *  export DISPLAY=:0 && xset dpms force on