More and more developers are finding out that the Windows Subsystem for Linux (WSL) is pretty great. Being able to transparently use a ton of established *nix tooling on Windows can make your machine a kind of best-of-both hybrid as both a user and a developer.
Updates to WSL have included adding support for background tasks when all your Linux console windows are closed, where previously the WSL processes had to be running under a live shell. However, you still needed to start those services yourself because Linux sessions could only be initiated explicitly. There was no start-up config to launch services like Postgresql or RabbitMQ and the like.
After several restarts after which I neglected to start up these services manually I wanted a solution that would get Postgres running in the background in the WSL layer automatically when I started up my machine.
Here’s how you can start WSL background services on Windows login:
First you’ll need a startup script. I called mine
start_postgresql.sh and put it in
~/.local/bin/ . All mine does is start the Postgres service, but you can start as many other services or do as many other start-up time actions as you want.
mkdir -p ~/.local/bin/
echo "service postgresql start" >> ~/.local/bin/start_postgresql.sh
chmod +x ~/.local/bin/start_postgresql.sh
Of course, running service requires sudo privledges. But, since we need to run this at start-up without interaction and I don’t want to enable password-less sudo… what do we do?
It turns out you can enable password-less sudo for specific commands without enabling the security faults of allowing your user to sudo just anything without any safety check. Open visudo to edit your sudo config:
And then add this line to enable sudo run only this script.
calvin ALL=(root) NOPASSWD: /home/calvin/.local/bin/start_postgresql.sh
Great! Of course, your name probably isn’t “calvin” like mine. Change that to your own username.
All that’s left to do is get Windows to run this script in the WSL layer when you login. We can use the Task Scheduler to do this, so open the start menu and type “Task Scheduler” to find the application and run it.
Now, click “Task Scheduler Library” on the left and then “Create Task…” on the right to create a new task you can configure to run your script with these steps:
- Name the task anything you want, like “Start Postgres”
- Under the “Triggers” tab, click “New…” to add a new trigger for this task
- In the “Begin the task” dropdown select “At log on”
- Select “Any user”
- Under the “Actions” tab, click “New…” to add a new action for this task
- Pick “Start a program” for the action type and then enter
C:\Windows\System32\bash.exe as the program to run
- Finally, as the last and most important step, at “Add arguments (optional)” set this argument string to run the command with:
-c "sudo ~/.local/bin/start_postgresql.sh"
That’s all you need. When you log into Windows next this task will be triggered and run your custom script as root and start Postgres and any other services you decide to spin up.
Hopefully this will make developing on Windows with Linux tooling even easier than WSL already does!