Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Service doesn't automatically start when Windows boots up #25

Closed
jsweet-dev opened this issue Jun 7, 2023 · 4 comments
Closed

Service doesn't automatically start when Windows boots up #25

jsweet-dev opened this issue Jun 7, 2023 · 4 comments
Assignees

Comments

@jsweet-dev
Copy link
Owner

I'm currently running this application in WSL2 on Windows 11 with Docker installed in WSL2. The problem is that if the computer reboots, the service is down until I log on, open a WSL terminal, start the docker service and then run the docker-compose up -d command to start the containers.

Ideally, the WSL environment would automatically launch and start the docker service and then run the docker-compose command without any manual intervention so that the least amount of data is missed in the logs.

@jsweet-dev jsweet-dev self-assigned this Jun 7, 2023
@jsweet-dev
Copy link
Owner Author

Based on what I've found so far, I believe that a combination of using a scheduled task with VBS script to start the instance, an entry in the wsl.conf to start the service and then a shell script to run the docker-compose command and possibly another command such as keychain, may be the way to go.

This is more complicated than on a traditional system simply because Windows is configured to terminate the WSL environment if there are no interactive processes running. So, the shell script is critical to keep the environment online.

Some resources I've found so far:
https://medium.com/swlh/how-to-run-ubuntu-in-wsl2-at-startup-on-windows-10-c4567d6c48f1
https://superuser.com/questions/1343558/how-to-make-wsl-run-services-at-startup
https://askubuntu.com/questions/1435938/is-it-possible-to-run-a-wsl-app-in-the-background

@jsweet-dev
Copy link
Owner Author

I will add this information to the documentation as well. But here is the portion of the solution on the WSL side that appears to be working thus far. Step 3 still needs to be completed and tested, but I'm including it here for completeness.

Step 1 - Add boot command to wsl.conf

As root, create /etc/wsl.conf if necessary, then edit the file and add the following:

[boot]
command = service docker start

Step 2 - Create shell script

sudo -e /etc/profile.d/launch_minermonitor.sh

In the text editor add the following lines to the launch_minermonitor.sh script

#!/usr/bin/env/ sh
cd /home/jps/programming/BTCMiningMonitor
docker-compose up -d

Step 3 - Create the VBS script and scheduled task

Follow the example at https://medium.com/swlh/how-to-run-ubuntu-in-wsl2-at-startup-on-windows-10-c4567d6c48f1

@jsweet-dev
Copy link
Owner Author

jsweet-dev commented Jun 18, 2023

Couldn't get the vbScript example to work, so I decided to try powershell however that's not working either. A little more digging and I found there is an issue microsoft/WSL#8835. Several people mentioned different workarounds (psexec, non-store version, etc.) I will have to explore some of these to see if they will work (as success is reported as hit or miss with each workaround)

@jsweet-dev
Copy link
Owner Author

Using a combination of vbscript, powershell and PsExec I was able to get the scheduled task for starting WSL2 to work. Scripts have been saved in the utils/automatic_startup_files folder of this repo. This solution is inspired by cheng-chi's repo here.

Updated step 3 instructions:

Step 3 - Setting up the Scheduled Task

  1. Download PsTools and copy PsExec, along with the runner.vbs and start_wsl.ps1 files to an accessible location on your Windows host filesystem.

  2. In run_wsl.ps1, replace the distribution name with your distribution name and updated the output file paths to an accessible location on your filesystem.

  3. In runner.vbs, update the file paths for PsExec.exe start_wsl.ps1 to the actual location of these files on your system.

  4. Set up the scheduled task as detailed in this tutorial, but use the runner.vbs script in the action to execute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant