You are not logged in.
Eg. I have the following script running background executed in terminal after starting my window manager.
#!/bin/bash
while true; do
if [[ "$XDG_SESSION_TYPE" = "tty" ]]; then
echo "running in TTY" > /tmp/testchecktty
else
echo "NOT running in tty" > /tmp/testchecktty
fi
sleep 3
done
So, I can check the testing result by $ cat /tmp/testchecktty.
Then, if I exit the window manager and am being on TTY, the script is still running but checking $XDG_SESSION_TYPE doesn't know tty. The script will still know it is not in TTY.
How should I do it? If the question is silly, sorry!
Thanks in advance.
Offline
Ah, no - you need to take several steps back as that script doesn't make any sense at all. First the loop makes no sense, the environment variable is what it is, regardless of how many times you check it. It cannot / will not change within a process unless something within that process changes it. It's an environment variable, not a global variable.
Second, that problem also demonstrates the bigger problem with the script logic: it will not magically "move" to a tty. It was launched from a pty, and it will forever run in that pty. That's it.
So whatever you intend to do with this, your approach is wrong. So please tell us about what you really want to do as this is a perfect example of an X-Y problem.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
thanks, I think I understand.
It's an environment variable, not a global variable.
Second, that problem also demonstrates the bigger problem with the script logic: it will not magically "move" to a tty. It was launched from a pty, and it will forever run in that pty. That's it.
What I am really trying is my battery monitoring script. I am using sway starting from tty. I start my a script for checking battery level.
Later, I notice that sometime I used to do my stuffs on TTY leaving sway. At that time, the script will be running and I become worried if a command like notify-send is executed from the script that would produce error in TTY.
So, adjust my script as the following.
#!/bin/dash
echo "1" > /tmp/mshbattnotionoff
BNF=/tmp/mshbattnotionoff
while true; do
BATCAP=$(cat /sys/class/power_supply/BAT0/capacity)
BATSTATUS=$(cat /sys/class/power_supply/BAT0/status)
if [ $XDG_SESSION_TYPE = "tty" ]; then
[ $((BATCAP)) -lt 25 ] && systemctl poweroff
sleep 10
continue
fi
[ ! -f "$BNF" ] && echo "1" > "$BNF"
CBN=$(cat "$BNF")
if [ "$BATSTATUS" = "Discharging" ]; then
[ $((BATCAP)) -lt 20 ] && systemctl poweroff
[ $((BATCAP)) -lt 25 ] && notify-send -u critical -t 12000 "BATTERY POWER" \
"Battery power is critically low.\n$(acpi)\nCharge it NOW."
[ $((CBN)) -eq 1 ] && [ $((batcap)) -lt 40 ] && notify-send -u critical -t 12000 "BATTERY POWER" \
"Battery power is lower than 40%.\n$(acpi)\nPlease charge the battery before shutdown soon."
[ $((BATCAP)) -gt 50 ] && sleep 90
else
[ $((CBN)) -eq 1 ] && [ $((batcap)) -gt 80 ] && notify-send -t 7000 "BATTERY CHARGING" \
"Battery power is now over 80%.\n$(acpi)\nIt is recommended to stop charging for battery health."
sleep 30
fi
sleep 30
done &
The script is working well as I expect without the following.
if [ $XDG_SESSION_TYPE = "tty" ]; then
[ $((BATCAP)) -lt 25 ] && systemctl poweroff
sleep 10
continue
fi
Last edited by duyinthee (2023-07-02 13:16:41)
Offline
Thanks for the context / background.
I notice that sometime I used to do my stuffs on TTY leaving sway. At that time, the script will be running and I become worried if a command like notify-send is executed from the script that would produce error in TTY.
This is a faulty approach. First, when you "leave sway" do you actually close / exit sway, or do you just switch to another tty?
Next, the notify-send command cannot produce an error "in TTY" as it is not running in the tty ... ever. If you run that script in a way that it keeps running after sway exits, then notify-send will generate an error - but only into it's own detatched pty.
Forget about having a unified script that does everything. Just have a script that runs in sway (and terminates when sway exits) that provides handles the notify-send commands. Then if you *also* want to poweroff when the battery is below 25% (is that really your goal?? That's hardly a "critical" level) then do that as a seperate process.
Last edited by Trilby (2023-07-02 13:23:47)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
well explanation.
to poweroff when the battery is below 25% (is that really your goal?? That's hardly a "critical" level) then do that as a seperate process.
Yes, I should rethink it, thanks.
Offline