You are not logged in.
I'm writing a bash script that should either shutdown system or send user back to tty1 if there are servers running currently.
For now it's implemented like this:
services=(VintagestorySer ts3server)
fin=0
for i in ${services[@]}
do
app=($(ps -A | grep -m1 -e $i))
if [[ -n "$app" ]]
then
fin=1
hyprctl notify -1 3000 0 Qutting to tty.
sleep 3
systemctl stop sddm
hyprctl dispatch exit
fi
done
if [[ fin -eq 0 ]]
then
hyprctl notify -1 3000 0 Shutdown now.
sleep 3
shutdown now
fiso as you can see for returning back to tty i'm using systemctl unit stop and hyprctl for exiting. however, i have several questions:
1. does 'hyprctl dispatch exit' even executes? i suppose Hyprland starts as SDDM child, nor a independent application, so stopping sddm should kill Hyprland and apparently, this script?
2. for some reason, instead of returning back to tty1, the screen freezes at black screen with blinking cursor, so if I want to get back to Hyprland I must either restart the whole system or use other ttys. Is there's something I'm doing wrong? using ps -A | grep -e sddm/SDDM/hyprland/hypr/Hyprland/Hypr/tty1 won't show anything to me
Offline
Before getting into race-conditions that affect your script: can you expand a little more on what your actual goal is? This could be an X-Y problem, where there is a better solution to the core problem, rather than trying to force logouts, shutdowns, etc.
Offline
This is from my shutdown script, which asks several apps to close and waits for them, then shutdowns system. But now there are some services that required to stay online, so i'm trying to transform my PC to server every time i'm not using PC (so replace shutdown with server mode). So either way system is forced to logout/shutdown in this scenario.
Offline
So you want a user-initiated action that calls a script which will either 1) shut down the machine OR 2) log off that user, depending on whether one of 'VintagestorySer' or 'ts3server' is running? Because then you shouldn't stop sddm, since it's the Login Manager, but instead use something like `loginctl --terminate-session`. Keep in mind the notes on Kill user processes on logout specific to Arch's build of systemd.
At the very least, you shouldn't call loginctl or `systemctl stop` (if you still want to actually kill the login manager) inside a for loop. Instead, set your 'fin' flag only and run the kill process command outside the loop, just like you have when 'fin=0'.
Generally, the problem with scripts like this is that it 'saws off the branch you're sitting on', since you are killing all processes belonging to the current user, which includes the script. You also need to be aware that loginctl and systemctl can run asynchronously, depending on the situation, so you either have to have nothing else following those commands that depend on their outcome, or wait for the completion of those commands, which, again, is problematic since you're terminating the script as well.
A more elegant approach might be to have your log-out event trigger a script that will additionally shut down the machine if those services are not running. That might have no race conditions.
Offline
'VintagestorySer' and 'ts3server' have no dependency on SDDM and Hyprland, since ts3server is a systemd unit and VintagestorySer booted up in tty2. I completely aware that on closing session every app launched there is gone too, so that's why i'm using tty2 for. Thanks for advising using log-out event for making sure no services running right now, i'll check out how to implement this
Offline