0

I have a small script that checks if my rsync backup has completed and if so sleeps my machine.
This is on WSL (Debian).

#!/bin/bash

# if rsync is running, then wait 5
while ps aux | grep -q -e "[r]s";
do
    sleep 5
    echo "script running..."
done

# sleep PC
psshutdown64.exe -d -t 0

This works fine when launching from a WSL window.
If the script is running, it waits, and when I terminate rsync, the machine sleeps.

script running...
script running...
script running...
script running...

PsShutdown v2.53 - Shutdown, logoff and power manage local and remote systems
Copyright (C) 1999-2021 Mark Russinovich
Sysinternals - www.sysinternals.com

I'm trying to create a Windows shortcut to launch the same script.

When launching the shortcut, the loop never exits.
It keeps showing script running... without sleeping.

I've tried bash.exe -c, wsl.exe, debian.exe, and powershell.exe to launch the script.
None worked.

Interestingly, when I launch the script through a cmd or Powershell window, then it works fine:

wsl sh sleep.sh

PsShutdown v2.53 - Shutdown, logoff and power manage local and remote systems
Copyright (C) 1999-2021 Mark Russinovich
Sysinternals - www.sysinternals.com

But it fails if I use a shortcut to launch it.

EDIT: the script is saved in a folder within Documents. But the script is indeed executable and works fine in WSL.

6
  • So what is different in ps aux between the two cases? Jan 6 at 21:01
  • Seemingly, nothing. The output is the same.
    – NoExpert
    Jan 6 at 21:17
  • Does ps aux | grep -e '[r]s' inside the loop (or drop -q from the condition) produce output? I expect an environmental difference in a path containing "rs" or something along those lines. Jan 6 at 21:22
  • There's no output either on WSL or through the Windows shortcut. Which is actually the expected output, if rsync is not running. But for the reasons that are unclear to me the very same script never exits the loop using the shortcut. The "solution" below works, notwithstanding that it doesn't answer the question directly.
    – NoExpert
    Jan 6 at 23:48
  • Interesting! grep shouldn't return true without any output (except with -q or -v), so something is funny there. For anyone else with a similar problem in future, the scenario I'm thinking of is that the Windows shortcut results in running something like sh /mnt/c/Users/noexpert/Documents/script.sh, where "/Use**rs**/" matches the grep pattern and so causes the test to succeed by finding the script itself running. Jan 7 at 21:14

1 Answer 1

0

I'm posting this as an answer in case it helps someone else.
While it doesn't directly answer the question, it gets the result I want.

  1. create symlink to script in home
  2. create Windows shortcut: C:\Windows\System32\wsl.exe sh ~/sleep.sh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.