1

I am trying to run a script before the login screen in Ubuntu 19.04. The script checks if there is an external monitor connected to my notebook and if there is one it changes the resolution to 3440x1440@44 and switches off the internal laptop display. This is my script:

#!/bin/bash

EXTERNAL_OUTPUT="HDMI-1-1"
INTERNAL_OUTPUT="eDP-1-1"

xrandr |grep $EXTERNAL_OUTPUT | grep " connected "
if [ $? -eq 0 ]; then
    xrandr --output $INTERNAL_OUTPUT --off
    xrandr --newmode "3440x1440_44.00"  299.75  3440 3664 4024 4608  1440 1443 1453 1479 -hsync +vsync
    xrandr --addmode HDMI-1-1 "3440x1440_44.00"
    xrandr --output HDMI-1-1 --mode "3440x1440_44.00"
else
    xrandr --output $INTERNAL_OUTPUT --auto --output $EXTERNAL_OUTPUT --off
fi

I have tried to put this script both in ~/.profile and also in the Startup Applications but my GDM login is still present only on my notebook screen. I want this script to be executed even before that time so that I am not present with a black picture upon starting my computer.

[UPDATE]: I have tried both proposed solutions but they were both not working. First I have tried the solution of Fiximan. I have created the monitor-check.service in /etc/systemd/system and tried to execute the /bin/bash /home/user/monitor-check.service and it successfully changed the laptop resolution to 3440x1440. Then I have tried to execute: sudo systemctl start monitor-check.sh and this is the status message:

monitor-check.service - Service to check for external monitors during boot.
   Loaded: loaded (/etc/systemd/system/monitor-check.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Tue 2019-07-09 19:27:17 CEST; 5s ago
  Process: 4177 ExecStart=/bin/bash /home/user/Documents/scripts/display_setup.sh (code=exited, status=1/FAILURE)
 Main PID: 4177 (code=exited, status=1/FAILURE)

Jul 09 19:27:17 acer systemd[1]: Started Service to check for external monitors during boot..
Jul 09 19:27:17 acer bash[4177]: Can't open display
Jul 09 19:27:17 acer bash[4177]: Can't open display
Jul 09 19:27:17 acer systemd[1]: monitor-check.service: Main process exited, code=exited, status=1/FAILURE
Jul 09 19:27:17 acer systemd[1]: monitor-check.service: Failed with result 'exit-code'.

I have tried to add the environment variable to the Xauthority by adding: Environment=XAUTHORITY=/run/user/1000/gdm/Xauthority to the Service section but it didn't help.

I have also tried the proposed solution by Praveen but it didn't work either. In fact, I have added my script to /etc/profile.d directory but it didn't execute the script so my resolution was still the old one.

In conclusion, the only two solutions which worked was to add the script to the ~/.profile or to add it manually to the Startup Applications but both solutions change the resolution and switches off the internal laptop display after logon to the system.

  • Best practice is to answer your own question and mark it as solved even if you found a solution on your own. That way others who have the same problem will directly know that an answer exists. Thanks for the feedback on my question. – Fiximan Jul 9 '19 at 19:52
  • The problem is that I didn't find a solution. Unfortunately, both proposed methods didn't work out for my scenario. – Georgе Stoyanov Jul 9 '19 at 20:53
1
Kindly add the script in /etc/profile.d

If You want it to be user specific Kindly add in below location so it will execute when user logins

/home/username/.profile
  • Thanks for the hint, first of all, why kindly? And second of all, I want this evaluation to be executed before the login screen. So according to my understanding, if I have a couple of users, then the /home/username/.profile will be executed only after the particular user logs into the system, but not before. This is exactly what I get now. I will test the /etc/profile.d in the evening when I am back home. – Georgе Stoyanov Jul 9 '19 at 9:55
  • your solution didn't work. I have added an [Update] to my original thread with a summary of the additional tests I have done. – Georgе Stoyanov Jul 9 '19 at 17:38
1

Ubuntu 19.04 uses systemd so the preferred way for such scripts would be using a sysd-service.

For this you will need two files: 1) the script.sh you already wrote (I assume it is tested and functional) and 2) a unit file defining the systemd service.

An example of the latter would be:

[Unit]
Description=Service to check for external monitors during boot.

[Service]
Type=Simple
ExecStart=/bin/bash /path/to/your/monitor/script.sh

[Install]
WantedBy=multi-user.target

Description and ExecStart should be self-explanatory, Type=Simple means the script is to be run once only (and not every X seconds or so) and WantedBy=multi-user.target makes it run once you have a "normal" boot (not shutdown, not rescue mode, and graphics optional). Of course you can make the script executable and not add /bin/bash in the ExecStart-line.

The script goes to /etc/systemd/system e.g. by the name monitor-check.service, note that the .service-suffix is mandatory! Set ownership to root and permissions to 644.

You can test the service by

systemctl start monitor-check

or

service monitor-check start

and check the status by replacing start with status in the above commands.

To enable is as boot service use:

systemctl enable monitor-check.service

If you now look at service monitor-check status, you should read enabled in the Loaded:-line.

Of course the script itself should be owned by root and have 644-permissions for safety reasons. Best is to keep it in new directories like e.g. /etc/my_scripts or /etc/systemd/my_scripts.

After reboot you can use the status of the service to check for its exit code.

  • Unfortunately, the proposed solution doesn't work for displays. You can check the update I have posted in my original post for additional information why. – Georgе Stoyanov Jul 9 '19 at 17:38
  • @GeorgеStoyanov I assume the execution is set to the wrong timing then. Unfortunately, I have no monitor to test it myself but graphical.target instead of multi-user.target might do the trick as it waits for the graphics server to start. If you're up for some testing, hopefully. – Fiximan Jul 9 '19 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.