2

I use the i3 window manager and every day of remote work I would like to run a script that is responsible for setting up my monitors. On Tuesdays, I work onsite, then I'll want to run another script. I could probably solve my problem using bash conditionals, but I'd rather use this opportunity to learn more about systemd than just start/stop/restart/enable/disable :D

This is my script (it works ok, when launched from the terminal)

#!/bin/sh
xrandr --output eDP-1 --off --output HDMI-1 --primary --mode 1920x1080 --pos 0x0 --rotate normal --output DP-1 --off --output DP-2 --off --output DP-3 --off --output DP-4 --off

This is my unit (it works only when launched from the terminal via systemctl --user start home_screen_setup.service)

[Unit]
Description=Monitors setup when I work from home
After=graphical-session.target
BindsTo=graphical-session.target
PartOf=graphical-session.target
Requisite=graphical-session.target
ConditionEnvironment=DISPLAY
ConditionEnvironment=XAUTHORITY

[Service]
Type=oneshot
ExecStart=/home/userX/.screenlayout/home_setup.sh

[Install]
WantedBy=graphical-session.target

This is my timer

[Unit]
Description=Setup my screen configuration on every day except Tuesday

[Timer]
#OnBootSec=15seconds
OnCalendar=Mon,Wed,Thu,Fri

[Install]
WantedBy=timers.target

Some status output:

userX@HP-ProBook-450:~$ systemctl --user status home_screen_setup.service
○ home_screen_setup.service - Disables laptop screen when I work from home
 Loaded: loaded (/home/userX/.config/systemd/user/home_screen_setup.service; enabled; vendor preset: enabled)
 Active: inactive (dead)
TriggeredBy: ● home_screen_setup.timer


userX@HP-ProBook-450:~$ systemctl --user status home_screen_setup.timer
● home_screen_setup.timer - Setup my screen configuration on every day except Tuesday
 Loaded: loaded (/home/userX/.config/systemd/user/home_screen_setup.timer; enabled; vendor preset: enabled)
 Active: active (waiting) since Sun 2024-03-03 12:00:22 CET; 1min 4s ago
Trigger: Mon 2024-03-04 00:00:00 CET; 11h left
Triggers: ● home_screen_setup.service

mar 03 12:00:22 HP-ProBook-450 systemd[2306]: Started Setup my screen configuration on every day except Tuesday.

What am I doing wrong?

2
  • Advice: It's usually much easier to invoke the script/command every day and have the script check the day and decide to perform its work or exit without performing work.
    – Sotto Voce
    Commented Mar 3 at 12:34
  • There are plenty of resources that you can use to learn more about system day. A more efficient way of achieving what you want to do is to use a cron job that runs on only on those days, does what it needs, and then verifies that it is done. Commented Mar 3 at 13:10

0

You must log in to answer this question.

Browse other questions tagged .