3

Morning all, I am trying to run a little script on startup I wrote to configure my desktop if the VGA1 output is connected (which it is). Running BunsenLabs-Hydrogen (Debian based).

Script:

#!/bin/bash
### BEGIN INIT INFO
# Provides:          resVGA1
# Required-Start:
# Required-Stop:
# Default-Start:     2 3 4 5 
# Default-Stop:      0 1 6
# Short-Description: Setup xrandr with VGA1
# Description:       Setup Multi-screen resolution with VGA1 connected
### END INIT INFO

if xrandr | grep "VGA1 connected"; then
    xrandr -s 1366x768                     #As it doesn't default to this with VGA1 connected
    xrandr --auto                          #To populate the second screen
    xrandr --output VGA1 --right-of LVDS1  #Right of VGA1, not duplicate
fi

I've made the script executable and it works when called, then placed it in /etc/init.d/ and ran:

update-rc.d resVGA1 defaults

The output of "ls -l /etc/rc?.d/resVGA" is:

lrwxrwxrwx 1 root root 17 Dec 19 12:33 /etc/rc0.d/K01resVGA1 -> ../init.d/resVGA1
lrwxrwxrwx 1 root root 17 Dec 19 12:33 /etc/rc1.d/K01resVGA1 -> ../init.d/resVGA1
lrwxrwxrwx 1 root root 17 Dec 19 12:33 /etc/rc2.d/S01resVGA1 -> ../init.d/resVGA1
lrwxrwxrwx 1 root root 17 Dec 19 12:33 /etc/rc3.d/S01resVGA1 -> ../init.d/resVGA1
lrwxrwxrwx 1 root root 17 Dec 19 12:33 /etc/rc4.d/S01resVGA1 -> ../init.d/resVGA1
lrwxrwxrwx 1 root root 17 Dec 19 12:33 /etc/rc5.d/S01resVGA1 -> ../init.d/resVGA1
lrwxrwxrwx 1 root root 17 Dec 19 12:33 /etc/rc6.d/K01resVGA1 -> ../init.d/resVGA1 

So to my knowledge this should work on startup, but it doesn't - I checked /var/log/syslog and found the following reports:

Dec 19 12:33:46 DankPad resVGA1[553]: Can't open display
Dec 19 12:33:46 DankPad kernel: [    0.244129] ACPI: bus type PNP unregistered
Dec 19 12:33:46 DankPad kernel: [    0.250523] pci 0000:00:1c.0: PCI bridge to [bus 02]
Dec 19 12:33:46 DankPad systemd[1]: Started LSB: Setup xrandr with VGA1.
Dec 19 12:33:46 DankPad kernel: [    0.250541] pci 0000:00:1c.1: PCI bridge to [bus 03]
Dec 19 12:33:46 DankPad kernel: [    0.250548] pci 0000:00:1c.1:   bridge window [mem 0xf2400000-0xf24fffff]
Dec 19 12:33:46 DankPad kernel: [    0.250560] pci 0000:00:1c.3: PCI bridge to [bus 05-0c]

Am I doing something wrong or just fundamentally misunderstanding what I'm trying to do?

Any help would be appreciated.

BodneyC
  • 33
  • 1
  • 5

1 Answers1

3

You're hooking in to the system startup scripts, which don't have an X11 session available to them. That's why you're getting the "Can't open display" error. (I'll leave aside that you're writing an init.d script on a systemd system, instead of a systemd unit. And that your init script fails to actually follow the API, e.g., it doesn't check for "start" or "stop").

Instead, you need to hook into the X11 startup. You have five options (at least—kept thinking of more while writing this):

  1. System wide, put a script in /etc/X11/Xsession.d/. This script is sourced in to the X session setup, basically all you should need is the if block. This would be my choice.

  2. You should be able to set the default config for the monitor by putting stuff in /etc/X11/xorg.conf.d/ (which you may need to mkdir first). Though you'd need to learn the Xorg config syntax.

  3. For one user, put it in your ~/.Xsession.

  4. For one user, put it in your desktop environment's startup scripts (most have this). Or maybe your DE can actually just remember the monitor setting and apply it automatically.

  5. System wide (or for one user), put it in the systemd user session. This would involve putting a unit file in /etc/system.d/user or ~/.config/systemd/user/. Very flexible, but requires learning systemd.

derobert
  • 107,579
  • 20
  • 231
  • 279