Run task in Windows on WSL2 startup

Alex Chashin

When developping applications I run a server on WSL2 and I need to connect to it from browser on windows host. To do this, I need to bind some ports and create firewall rules every time WSL is started. It is done with a powershell script that is located on windows machine. For now I have a task in task scheduler that runs this script when I login to windows. It works fine, but it doesn't fire when I restart WSL manually which I sometimes do. To run a script on startup in WSL I could invoke it in .profile or .bashrc, but I want to run it on Windows, not in WSL. Is there a way to do this?

NotTheDr01ds

You're on the right track. Just run the PowerShell script in your .profile using the Windows (not Linux) PowerShell interpreter:

/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe "C:\myscript.ps1"

It probably doesn't need to be fully qualified, but it's probably safer to do so.

You can also convert paths from WSL to Windows if needed, e.g. powershell.exe $(wslpath -w script/myscript.ps1) if you want to store the script on the WSL ext4 filesystem (e.g. perhaps you want it relative to your home directory).

Of course, substitute pwsh.exe if are using PowerShell core. And fully qualify it if it is either (a) not in your path or (b) you have the Linux PowerShell core version installed as well.

Since you are using the PowerShell interpreter running on Windows, all of the commands (port forwarding, firewall rules) will be run against the Windows host.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Communication between Ubuntu on WSL2 and Windows is spotty after startup

From Dev

Run background task at startup

From Dev

Windows + Docker + WSL2

From Dev

Does WSL2 run Linux in a virtual machine or alongside the Windows kernel?

From Dev

Can I run VMware Player and WSL2 on the same Windows 10 Pro machine without disabling either?

From Dev

WSL2 preferred startup method

From Dev

How Can I Schedule A Task In Task Scheduler To Run At Startup Every 2 Days?

From Dev

Windows 8 Run script on startup

From Dev

Enable MOTD message on WSL and WSL2 for Windows 10?

From Dev

Windows Task Scheduler - Run task nonstop

From Dev

Windows Scheduled Task refuse to run

From Dev

How to run a task in background in windows

From Dev

C# Application not run on startup, Startup impact "not measured" on Windows 10

From Dev

Does WSL2 docker need windows docker desktop?

From Dev

WSL2 dynamic File Paths based on the username for linux and windows

From Dev

Cannot access file run on windows startup

From Dev

application run on windows startup after installation

From Dev

Checklist to configure programs to (not) run at startup in windows 8

From Dev

The Windows Firewall won’t run on startup

From Dev

How to run a trusted program as Admin on windows startup?

From Dev

How to run a program as an administrator at startup on Windows 10?

From Dev

application run on windows startup after installation

From Dev

Access to the Windows Startup directory via Run

From Dev

How to run Ubuntu service on Windows (at startup)?

From Dev

Do startup scripts in Windows Server run asynchronously?

From Dev

Master volume control in Windows, run on startup

From Dev

Windows Task Scheduler, run task if task isn't running?

From Dev

Django celery task run at once on startup of celery server

From Dev

Adding Startup task in Azure Cloud Service doesn't run locally

Related Related

  1. 1

    Communication between Ubuntu on WSL2 and Windows is spotty after startup

  2. 2

    Run background task at startup

  3. 3

    Windows + Docker + WSL2

  4. 4

    Does WSL2 run Linux in a virtual machine or alongside the Windows kernel?

  5. 5

    Can I run VMware Player and WSL2 on the same Windows 10 Pro machine without disabling either?

  6. 6

    WSL2 preferred startup method

  7. 7

    How Can I Schedule A Task In Task Scheduler To Run At Startup Every 2 Days?

  8. 8

    Windows 8 Run script on startup

  9. 9

    Enable MOTD message on WSL and WSL2 for Windows 10?

  10. 10

    Windows Task Scheduler - Run task nonstop

  11. 11

    Windows Scheduled Task refuse to run

  12. 12

    How to run a task in background in windows

  13. 13

    C# Application not run on startup, Startup impact "not measured" on Windows 10

  14. 14

    Does WSL2 docker need windows docker desktop?

  15. 15

    WSL2 dynamic File Paths based on the username for linux and windows

  16. 16

    Cannot access file run on windows startup

  17. 17

    application run on windows startup after installation

  18. 18

    Checklist to configure programs to (not) run at startup in windows 8

  19. 19

    The Windows Firewall won’t run on startup

  20. 20

    How to run a trusted program as Admin on windows startup?

  21. 21

    How to run a program as an administrator at startup on Windows 10?

  22. 22

    application run on windows startup after installation

  23. 23

    Access to the Windows Startup directory via Run

  24. 24

    How to run Ubuntu service on Windows (at startup)?

  25. 25

    Do startup scripts in Windows Server run asynchronously?

  26. 26

    Master volume control in Windows, run on startup

  27. 27

    Windows Task Scheduler, run task if task isn't running?

  28. 28

    Django celery task run at once on startup of celery server

  29. 29

    Adding Startup task in Azure Cloud Service doesn't run locally

HotTag

Archive