Scheduling Night Mode for Better Screen Viewing

Ojukwu EbubeOjukwu Ebube
2 min read

INTRODUCTION

This is a bash script code that helps the user schedule an automated night mode. It automatically reduces blue light emission from your screen at night by adjusting display color temperature.

#!/bin/bash

CONFIG_FILE="$HOME/.nightmode.conf"
TEMP_DAY=6500    # Default daytime temperature (neutral white)
TEMP_NIGHT=3500  # Night temperature (warm yellow/red)
TRANSITION_TIME=60  # Transition duration in seconds

# Load or initialize config
load_config() {
    if [ -f "$CONFIG_FILE" ]; then
        source "$CONFIG_FILE"
    else
        ENABLED=false
        CURRENT_TEMP=$TEMP_DAY
        save_config
    fi
}

save_config() {
    echo "ENABLED=$ENABLED" > "$CONFIG_FILE"
    echo "CURRENT_TEMP=$CURRENT_TEMP" >> "$CONFIG_FILE"
}

# Adjust screen temperature
set_temperature() {
    local temp=$1
    local transition=$2

    for output in $(xrandr --current | grep " connected" | cut -d' ' -f1); do
        xrandr --output "$output" --gamma 1:1:1 --brightness 1.0
        if [ "$transition" -gt 0 ]; then
            xrandr --output "$output" --gamma 1:0.8:0.7 --brightness 0.9 \
                   --transition "$TRANSITION_TIME"
        fi
    done
    CURRENT_TEMP=$temp
    save_config
}

# Main control functions
start_nightmode() {
    if [ "$ENABLED" = false ]; then
        set_temperature "$TEMP_NIGHT" "$TRANSITION_TIME"
        ENABLED=true
        echo "Night mode activated (Temperature: $TEMP_NIGHT K)"
    else
        echo "Night mode is already running!"
    fi
}

stop_nightmode() {
    if [ "$ENABLED" = true ]; then
        set_temperature "$TEMP_DAY" "$TRANSITION_TIME"
        ENABLED=false
        echo "Night mode deactivated (Temperature: $TEMP_DAY K)"
    else
        echo "Night mode is not active!"
    fi
}

check_status() {
    if [ "$ENABLED" = true ]; then
        echo "Night mode is ON (Current temp: $CURRENT_TEMP K)"
    else
        echo "Night mode is OFF (Daytime temp: $CURRENT_TEMP K)"
    fi
}

# Parse command-line arguments
case "$1" in
    "start")
        load_config
        start_nightmode
        ;;
    "stop")
        load_config
        stop_nightmode
        ;;
    "status")
        load_config
        check_status
        ;;
    *)
        echo "Usage: $0 [start|stop|status]"
        exit 1
        ;;
esac

How to Use

  1. Save & Make Executable
chmod +x nightmode.sh
  1. Test Manually
./nightmode.sh start   # Enable night mode
./nightmode.sh stop    # Disable
./nightmode.sh status  # Check status
  1. Schedule with Cron (Automate)

Add to crontab (crontab -e)

# Turn on at 8 PM daily
0 20 * * * /path/to/nightmode.sh start

# Turn off at 7 AM
0 7 * * * /path/to/nightmode.sh stop

Importance of This Script

  • Reduces eye strain at night

  • Lightweight and automatable

  • Customizable for different screens.

0
Subscribe to my newsletter

Read articles from Ojukwu Ebube directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ojukwu Ebube
Ojukwu Ebube