Redshift: Load redshift-gtk from bash script?

Created on 20 Feb 2018  路  2Comments  路  Source: jonls/redshift

Quick question. I have a simple bash script that

  • disables (kills) redshift and redshift-gtk when I load VLC
  • reloads redshift when I quit VLC.

However, I cannot load redshift-gtk (so that I get my redshift tray icon back) from my bash script: I tried everything imaginable ...

Is this possible (how)? Thanks ...

Arch Linux 4.15.3-1-ARCH | x86-64
redshift v.1.11-5 installed via pacman

#!/bin/bash
if pgrep vlc
then
    pkill redshift*
    redshift -x
#elif ! pgrep vlc -a ! pgrep redshift
elif ! pgrep redshift
then
    redshift &
    # I CAN'T  GET redshift-gtk TO WORK FROM THIS SCRIPT:
    #nohup redshift-gtk
    #/usr/bin/python3 2>/dev/null /usr/bin/redshift-gtk &
    #/usr/bin/redshift-gtk 2>/dev/null &
    # etc. etc. ...
fi
help wanted question

All 2 comments

The only advice I can offer in answering the question is to make sure that the DISPLAY variable is set in the script. If you run this script in a terminal, does it give you any output for the redshift-gtk command?

As an alternative, you can consider using redshift-adjust and using a wrapper script around VLC. The first time the script is called, save the current colour temperature using redshift-adjust -p and reset the colour temperature. Don't kill the redshift process, or send it a SIGSTOP. When all instances of the script finish, reset the temperature to the saved temperature, and if you sent the redshift process a SIGSTOP earlier, send it a SIGCONT.
This process is similar to the approach that my pulsewrap script takes. Feel free to reference and adapt that script.

OK, I solved this -- thank you @tpenguinltg ! :-)

TLDR:

#!/bin/bash
#/mnt/Vancouver/Programming/scripts/displays.sh

export DISPLAY=":0"
export XAUTHORITY=/home/victoria/.Xauthority

xrandr --display :0 --output HDMI1 --auto --primary  --output VGA1 --auto --right-of HDMI1 --output HDMI2 --auto --right-of VGA1

if pgrep vlc
then
    pkill -SIGSTOP redshift*
    # or (note the backticks):
    #kill -SIGSTOP `pgrep redshift`
    # Also needed:
    redshift -x
elif ! pgrep vlc
then
    pkill -SIGCONT redshift*
    # or:
    #kill -SIGCONT `pgrep redshift`
fi

EXPLANATION:

I have two monitors, plus a 1080p HDTV that I use exclusively with VLC for watching downloaded videos (Survivor; ...).

When I turn on that HDTV, it is recognized as a 3rd monitor, and -- annoyingly -- my screens sometime change positions.

So, I run a script, "run_every_10_sec.sh", that rearranges my monitors to my preferred positions -- IF that occurs:

#!/bin/bash
# /mnt/Vancouver/Programming/scripts/run_every_10_sec.sh

# ----------------------------------------------------------------------------
# PERMIT ONLY ONE INSTANCE OF THIS PROGRAM
# http://unix.stackexchange.com/questions/150778/prevent-a-second-instance-of-my-software-from-starting
# Abort startup if another instance was found
pidof "/mnt/Vancouver/Programming/scripts/run_every_10_sec.sh" > /dev/null && {
  echo Sorry. Only one instance allowed.
  exit
}

# ----------------------------------------------------------------------------
# LOCKFILE
#https://stackoverflow.com/questions/185451/quick-and-dirty-way-to-ensure-only-one-instance-of-a-shell-script-is-running-at/185473#185473
LOCKFILE=/tmp/run_every_10_sec_lock.txt

if [ -e ${LOCKFILE} ] && kill -0 `cat ${LOCKFILE}`; then
    echo "already running"
    exit
fi

# make sure the lockfile is removed when we exit and then claim it
trap "rm -f ${LOCKFILE}; exit" INT TERM EXIT
echo $$ > ${LOCKFILE}

# ----------------------------------------
# do stuff
while true
do
    /mnt/Vancouver/Programming/scripts/displays.sh
    sleep 10
done

#sleep 10

rm -f ${LOCKFILE}

As indicated, "run_every_10_sec.sh" calls another script ("displays.sh"), that ensures that my three monitors remain in their correct positions, when I turn on my HDTV.

I also added code to "displays.sh" to temporarily disable redshift whenever VLC is running, and re-enable redshift whenever VLC is not running:

#!/bin/bash
#/mnt/Vancouver/Programming/scripts/displays.sh

export DISPLAY=":0"
export XAUTHORITY=/home/victoria/.Xauthority

xrandr --display :0 --output HDMI1 --auto --primary  --output VGA1 --auto --right-of HDMI1 --output HDMI2 --auto --right-of VGA1

if pgrep vlc
then
    pkill -SIGSTOP redshift*
    # or:
    #kill -SIGSTOP `pgrep redshift`
    # also need this:
    redshift -x
elif ! pgrep vlc
then
    pkill -SIGCONT redshift*
    # or:
    #kill -SIGCONT `pgrep redshift`

fi

SCRIPT CHRONOLOGY:

When my system boots, this ~/.bashrc entry

/mnt/Vancouver/Programming/scripts/nohup.sh  "nice -n 19 /mnt/Vancouver/Programming/scripts/run_every_10_sec.sh" &

executes my

run_every_10_sec.sh

script, in the background, via my "nohup.sh" script:

#!/bin/bash
# /mnt/Vancouver/Programming/scripts/nohup.sh

# https://felixmilea.com/2014/12/running-bash-commands-background-properly/
# Running bash commands in the background properly

nohup $1 &>/dev/null &

Script "run_every_10_sec.sh" continually reloads every 10 seconds, so anytime I turn on my HDTV (and / or load VLC), within 10" (or less) my monitors keep their preferred locations (if not already there), and redshift (auto-started at system boot, along with redshift-gtk) is paused via a signal stop whenever VLC is running. :-)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

GITMichaelK picture GITMichaelK  路  4Comments

gamalan picture gamalan  路  4Comments

da2x picture da2x  路  6Comments

dimqua picture dimqua  路  3Comments

hjweide picture hjweide  路  4Comments