[SOLVED] Change themes on wake script

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
neek
Level 2
Level 2
Posts: 65
Joined: Wed Feb 27, 2019 2:17 am

[SOLVED] Change themes on wake script

Post by neek »

I've written a basic script that checks the time and switches my Cinnamon themes to dark if it's between 8pm and 6:30am.

Code: Select all

#!/bin/bash

currenttime=$(date +%H:%M);
        if [[ "$currenttime" > "20:00" ]] || [[ "$currenttime" < "06:30" ]]; then gsettings set org.cinnamon.desktop.interface gtk-theme Mint-Y-Dark; gsettings set org.cinnamon.theme name Mint-Y-Dark; fi
The script works if I run it as user, but doesn't work when run as root. This means it can't be executed it from /lib/systemd/system-sleep so that it runs after system sleep like so:

Code: Select all

#!/bin/sh

case $1 in
  post)
        sleep 3
        source /home/username/scripts/check_day_night.sh
        ;;
esac
Any ideas how to handle this?
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
neek
Level 2
Level 2
Posts: 65
Joined: Wed Feb 27, 2019 2:17 am

Re: Change themes on wake script

Post by neek »

In answer to my own question, I just needed to add su <username> -c to the system-wake script, like this:

Code: Select all

#!/bin/sh

case $1 in
  post)
        sleep 3
        su <username> -c "
        source /home/username/scripts/check_day_night.sh"
        ;;
esac
rene
Level 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: [SOLVED] Change themes on wake script

Post by rene »

A better option would be to change the !/bin/sh in the system-wake script to !/bin/bash: you are in check_day_night.sh using [[ which is not available in /bin/sh (on Debian/Ubuntu/Mint the shell "dash" rather than "bash") but the system-wake script sources check_day_night.sh (i.e., includes it) rather than executes it. The solution with su -c does execute it and therethrough has things work, but as of now you have written a rather confusing form of just /bin/bash /home/username/scripts/check_day_night.sh --- which you can of course also just do, without sourcing check_day_night.sh, and that seems more to the point still.

[EDIT] Looking again, you in fact do need to run as the user for gsettings so my final bit above is not right. I'd however just make it sudo -u <username> /home/username/scripts/check_day_night.sh or alike; the "source" thing is fairly confusing (i.e., it confused me...)
gm10

Re: [SOLVED] Change themes on wake script

Post by gm10 »

rene wrote: Thu Jan 23, 2020 8:22 am [EDIT] Looking again, you in fact do need to run as the user for gsettings so my final bit above is not right. I'd however just make it sudo -u <username> /home/username/scripts/check_day_night.sh or alike; the "source" thing is fairly confusing (i.e., it confused me...)
Don't you need to use sudo, anyway, for gsettings to work? At least on my system (which is not Mint, which may be the reason), su is not configured to init any of the GNOME stuff like gsettings, gvfs, etc.
rene
Level 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: [SOLVED] Change themes on wake script

Post by rene »

Urm, lovely. As in, sudo vs. su does not on this 19.3 Cinnamon system seem to make a difference as such but both su and sudo need to launch a login shell so as to have a basic user environment, and need DBUS_SESSION_BUS_ADDRESS (or DISPLAY at minimum, to allow a new instance to launch) set explicitly for this to work at all. I.e., both these work from a root shell:

Code: Select all

su -l rene -c "env DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus gsettings set org.cinnamon.desktop.interface gtk-theme Mint-Y"
and

Code: Select all

sudo -u rene -i env DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus gsettings set org.cinnamon.desktop.interface gtk-theme Mint-Y
Note; "root shell" as from a virtual console, i.e., without yet another level of sudo involved...

Without -l resp. -i and/or DISPLAY or DBUS_SESSION_BUS_ADDRESS itself defined, neither works. This of course also means that if the user is not logged in, or given the lingering behaviour of logind at the very least has not been logged in, this still won't do, no DISPLAY or session bus existing in the first place: the system-sleep script would ideally check for existence of /run/user/<uid>/bus for the relevant <uid> before doing anything.
gm10

Re: [SOLVED] Change themes on wake script

Post by gm10 »

rene wrote: Thu Jan 23, 2020 11:36 am
Yes, that's exactly the problem I meant when using su, but interestingly on my system the environment gets set up correctly when using sudo -iu. I don't even recall what I may have configured there to make work out of the box. The things you forget. But let's not confuse the thread with that.
rene
Level 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: [SOLVED] Change themes on wake script

Post by rene »

gm10 wrote: Thu Jan 23, 2020 1:22 pm But let's not confuse the thread with that.
Quite. As far as I saw there's no way OP in fact has this working from a root-run system-sleep script, so I do believe this thread needs to be unmarked solved...
neek
Level 2
Level 2
Posts: 65
Joined: Wed Feb 27, 2019 2:17 am

Re: [SOLVED] Change themes on wake script

Post by neek »

Thanks for the comments!
rene wrote: Thu Jan 23, 2020 2:11 pm Quite. As far as I saw there's no way OP in fact has this working from a root-run system-sleep script, so I do believe this thread needs to be unmarked solved...
It definitely works, but you're quite right; I forgot that I did actually include DISPLAY in check_day_night.sh, and didn't modify my answer accordingly. The working check_day_night.sh script is:

Code: Select all

#!/bin/bash

currenttime=$(date +%H:%M);
        if [[ "$currenttime" > "20:00" ]] || [[ "$currenttime" < "06:30" ]]; then DISPLAY=:0 gsettings set org.cinnamon.desktop.interface gtk-theme Mint-Y-Dark; gsettings set org.cinnamon.theme name Mint-Y-Dark; fi
I thought source might be necessary in the system-sleep script, as per this: https://stackoverflow.com/a/8352939
[source] executes the script in the first script's process, and pulls in variables and functions from the other script so they are usable from the calling script
rene
Level 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: [SOLVED] Change themes on wake script

Post by rene »

neek wrote: Fri Jan 24, 2020 2:52 am It definitely works, but you're quite right; I forgot that I did actually include DISPLAY in check_day_night.sh, and didn't modify my answer accordingly.
OK; that works.

As to the source thing: it as said includes, "virtually copy-pastes in", the named script but in this case said script used a Bash-specific construct even though the script you were sourcing it from was run through Dash instead. And, in fact, it's worse than that, with the source keyword itself an in Bash but not Dash available synonym for .. I.e., the [[ was in fact not even relevant here; it bombed out already in the system-sleep script itself.

So, just to emphasize, always be sure to, 1, know that on Debian/Ubuntu/Mint /bin/sh is not Bash (it is on many other Linux distributions) and, 2, to keep tabs on what is executing what. In the case of actual executable shell scripts, the shebang on its first line mentions such, but note that once sourced, again "virtually copy-pasted in", that shebang is no more than a simple comment; does not do anything any more.
Locked

Return to “Scripts & Bash”