Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Winsomnia-noPython

Inspired by Winsomnia-ssh but works without python (just uses bash ans wsl).

dependencies: iproute2 systemd coreutils procps

Usage

Version 1 (deprecated)

This version doesn't work if the bash login shell is force killed (SIGKILL), since then the trap doesn't trigger and the process isn't killed. This can also not be fixed using PDEATHSIG because for some reason this makes the corresponding windows process survive

  • Set Powershell path in winsomnia-old.sh
  • Set (absolute WSL) path to keep_awake.ps1 in winsomnia-old.sh
  • Add the line source /path/to/winsomnia-old.sh to your ~/.bashrc or other shell initialization script.

Version 2 (working)

  • Set Powershell path in winsomnia.sh
  • Set (absolute WSL) path to keep_awake.ps1 in winsomnia.sh

Running Scripts that need the pc to stay awake

Run:

source stay_awake.sh
my_script.py > out.txt 2>&1; exit

Running as a systemd service

Instead of sourcing the script per-shell, you can run winsomnia.sh as a long-running systemd service. This avoids spawning a powershell process per shell session and survives shell exits cleanly.

Ubuntu

  1. Copy the service unit file (create ~/.config/systemd/user/winsomnia.service or /etc/systemd/system/winsomnia.service):

    [Unit]
    Description=Keep system awake while SSH clients are connected
    
    [Service]
    Type=simple
    ExecStart=/bin/bash /absolute/path/to/winsomnia.sh
    Restart=on-failure
    RestartSec=10s
    
    [Install]
    WantedBy=multi-user.target
  2. Reload systemd and enable:

    sudo systemctl daemon-reload
    sudo systemctl enable --now winsomnia.service
  3. Verify:

    systemctl status winsomnia.service

NixOS

Add the included winsomnia.nix module to your configuration:

# flake.nix (or configuration.nix)
imports = [ ./winsomnia.nix ];

Then rebuild:

sudo nixos-rebuild switch --flake /etc/nixos

The module defines ssh-keepawake-keepalive.service as Type=simple, so the bash loop and its backgrounded powershell child both live in the same cgroup and are reaped cleanly when the service is stopped. Restart=on-failure keeps the loop alive across transient failures.

How does it work?

Whenever a new shell is initialized, the script checks if it is inside an ssh session. If that is the case, a Powershell child process is started that sets the SetThreadExecutionState to ES_CONTINUOUS | ES_SYSTEM_REQUIRED. This state is active until the windows thread exits. Fortunately, the windows process will be killed when the corresponding linux process is killed (see here), which is done using the trap ... EXIT.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages