0

In the effort to bring some of the Linux advantages to my customers running Windows 10, I've developed a few bash scripts in order to automate some of their everyday tasks.

I use to let them run the scripts through a Windows batch with the following content:

Ubuntu1804 run /usr/local/bin/script.sh

Unfortunately some of the scripts are taking long, either for huge sql queries or slow net and I/O operations, and during this wait a few users just can't refrain from closing the "strange black window", thus killing the process.

The command above will be killed even if put in quotes with & at the end.

I tried the following and didn't get testOK on my desktop.

Ubuntu1804 run "sleep 10 && touch /mnt/c/Users/myuser/Desktop/testOK &"

While this keeps the window open and creates the file testOK instead.

Ubuntu1804 run "sleep 10 && touch /mnt/c/Users/myuser/Desktop/testOK"

I was wondering if there is a way to avoid the process in Ubuntu WSL to be killed as soon as the window is closed.

Prevent the window to appear at all would be great too.

Prevent the user to interact with it would be an acceptable workaround.

Clients are Windows 10 Professional with latest security and quality rollup, I'm not providing site details since this is happening at more than one customer, some of them are big MS AD domains, some other are small offices with just a workgroup.

Marco
  • 1,679
  • 3
  • 17
  • 31

2 Answers2

4

Linux X11 sends a SIGHUP to running processes that have a file handle open to the terminal window when the terminal window closes. All programs by default have stdin, stdout, and stderr file handles open to the terminal window that spawned the program. WSL emulates that behavior.

One solution is to use 'nohup'. If test.sh is:

#!/bin/sh
sleep 10
touch ~/testOK

Then you can run it like this:

D:> ubuntu run nohup sh -x ~/test.sh &

If I immediately close the cmd window after typing that line, I get a file "nohup.out" in D: (my current working directory) that has the text generated by test.sh, and a few seconds later get testOK in my Ubuntu home directory. You can change things around inside your .bat script to put the nohup.out somewhere else though.

eric.green
  • 385
  • 1
  • 4
0

You can create a .vbs file like so:

set shell = CreateObject("WScript.Shell")

command = "bash -c 'touch /mnt/c/Users/Yourdesktop/Desktop/testOK2'"

shell.Run command,0

The last line, the "0" means don't show a window, "1" would mean show window.

I think this should accomplish what you are trying to do.

  • 4
    Maybe it would work, but VBS is one of the old technologies we're trying to move away from! – Michael Hampton Jan 24 '19 at 20:56
  • lol, it isn't like this is a giant vbs solution. It's 3 lines of code in a file that you could create a shortcut to so your users could click on to accomplish running a script, but to each their own I suppose – MinnesotaSlim Jan 24 '19 at 22:19