Skip to content

Commit 94ee011

Browse files
committed
feat(docker.win): autostop after 10 minutes when no RDP sessions are active
1 parent 79c8c72 commit 94ee011

4 files changed

Lines changed: 119 additions & 2 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[Unit]
2+
Description=Windows container auto-stop check
3+
Documentation=https://github.com/folke/dot
4+
5+
[Service]
6+
Type=oneshot
7+
ExecStart=%h/dot/windows/bin/win --autostop
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[Unit]
2+
Description=Windows container auto-stop timer
3+
Documentation=https://github.com/folke/dot
4+
5+
[Timer]
6+
OnBootSec=2min
7+
OnUnitActiveSec=1min
8+
9+
[Install]
10+
WantedBy=timers.target

ansible/roles/user/tasks/main.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,24 @@
3838
enabled: true
3939
state: started
4040
scope: user
41+
42+
- name: Windows autostop systemd service
43+
ansible.builtin.copy:
44+
src: roles/user/files/windows-autostop.service
45+
dest: ~/.config/systemd/user/windows-autostop.service
46+
mode: "0644"
47+
notify: Reload systemd user daemon
48+
49+
- name: Windows autostop systemd timer
50+
ansible.builtin.copy:
51+
src: roles/user/files/windows-autostop.timer
52+
dest: ~/.config/systemd/user/windows-autostop.timer
53+
mode: "0644"
54+
notify: Reload systemd user daemon
55+
56+
- name: Enable windows-autostop.timer
57+
ansible.builtin.systemd:
58+
name: windows-autostop.timer
59+
enabled: true
60+
state: started
61+
scope: user

windows/bin/win

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,38 @@ function win_start
3939
end
4040
end
4141

42+
function win_autostop
43+
# Check if container is running
44+
if not docker ps --format '{{.Names}}' | grep -q '^windows$'
45+
return 0
46+
end
47+
48+
set -l state_file ~/.cache/win_last_active
49+
50+
# Check for active xfreerdp processes
51+
if pgrep -x xfreerdp3 >/dev/null
52+
# Active sessions - update timestamp
53+
touch $state_file
54+
echo "Active Windows sessions detected, keeping running."
55+
else
56+
# No active sessions - check idle time
57+
if test -f $state_file
58+
# Check if file is older than 10 minutes
59+
if string length -q -- (find $state_file -mmin +10 -print -quit)
60+
echo "No active Windows sessions for 10 minutes, stopping..."
61+
win_stop
62+
rm -f $state_file
63+
else
64+
echo "No active Windows sessions, but within idle timeout."
65+
end
66+
else
67+
# Create initial timestamp
68+
touch $state_file
69+
echo "No active Windows sessions, starting idle timer."
70+
end
71+
end
72+
end
73+
4274
function win_stop
4375
echo "Stopping windows..."
4476
pkill xfreerdp3 2>/dev/null
@@ -52,7 +84,7 @@ function win_windows -a class
5284
| sort -k2 -r
5385
end
5486

55-
function win -a app_path
87+
function win_app -a app_path
5688
# Ensure WinBoat container is running
5789
win_start
5890
or return 1
@@ -143,4 +175,51 @@ function win -a app_path
143175
return 1
144176
end
145177
146-
win $argv
178+
function win_help
179+
echo "Usage: win [OPTIONS] [APP]"
180+
echo ""
181+
echo "Launch Windows desktop or applications via RDP"
182+
echo ""
183+
echo "Options:"
184+
echo " --start Start Windows container"
185+
echo " --stop Stop Windows container and kill all RDP sessions"
186+
echo " --autostop Check and auto-stop if idle for 10 minutes"
187+
echo " --help, -h Show this help message"
188+
echo ""
189+
echo "Examples:"
190+
echo " win # Launch Windows desktop"
191+
echo " win pwsh # Launch PowerShell"
192+
echo " win 'C:\\Program Files\\App\\app.exe' # Launch custom app"
193+
echo " win --start # Just start the container"
194+
echo " win --stop # Stop container"
195+
end
196+
197+
function main
198+
argparse -n win h/help start stop autostop -- $argv
199+
or return 1
200+
201+
if set -q _flag_help
202+
win_help
203+
return 0
204+
end
205+
206+
if set -q _flag_start
207+
win_start
208+
return
209+
end
210+
211+
if set -q _flag_stop
212+
win_stop
213+
return
214+
end
215+
216+
if set -q _flag_autostop
217+
win_autostop
218+
return
219+
end
220+
221+
# If no flags, launch app (or desktop if no app specified)
222+
win_app $argv
223+
end
224+
225+
main $argv

0 commit comments

Comments
 (0)