DEV Community

WSL Tips: Starting Linux Background Services on Windows Login

Expert Amateur on July 14, 2018

Note: I'm cross posting this from my blog to dip my toe into dev.to More and more developers are finding out that the Windows Subsystem for Linux ...
Collapse
 
peteruhrig profile image
Peter Uhrig

This will not necessarily work when you are runnning multiple WSL distros in parallel. You can resolve this by changing the command line in the task schedulder. In the following example I assume you want to run postgresql in Debian, which is not your primary/default WSL distro:
Instead of 'C:\Windows\System32\bash.exe' write 'wsl'
Instead of '-c "sudo ~./local/bin/start_postgresql.sh"' write '-d debian -e sudo /home/calvin/.local/bin/start_postgresql.sh".
(Yes, I had trouble with the ~ shortcut for the home directory, no idea why.)

Collapse
 
blanxd profile image
Blanxd.H

if the command is sudo, anything after that is already run as root, so the ~ would mean /root and not /home/calvin

Collapse
 
longlivechief profile image
Brian Vanderbusch

So, I started with your suggestion, but quickly realized most services are already scripts in /etc/init.d, and you usually manage these things with users. For example, for docker, I created a /etc/sudoers.d/docker.sudoers file, with the following:

%docker ALL=(root) NOPASSWD: /etc/init.d/docker start stop restart status
Enter fullscreen mode Exit fullscreen mode

I think I'm also going to see if I can get a feature request endoresed for a [autostart] config section for the /etc/wsl.conf file where you could just list services to autostart like so:

[autostart]
docker = true
postgresqsl = true
Enter fullscreen mode Exit fullscreen mode
Collapse
 
rbanffy profile image
Ricardo Bánffy

It's nice to bring Unix tooling into Windows, but, as a Linux and Mac user, I'm curious as to what Windows brings to the table. My brief experiences with Windows post-XP were mostly frustrating. The UI for managing the machine is confusing and crude, downloading updates is slow and either slows down the machine or locks it down until finished.

Collapse
 
ironfroggy profile image
Expert Amateur

I dunno, man. I'm not trying to sell you on Windows. I'm trying to help out people who already decided to use it. :-)

Collapse
 
jnunez166 profile image
jnunez166

There are some of us that are forced to use Windows because of our employers, IT and the MS Apps that they require. This is easier than trying to get Linux working in a MS environment and using Open Source apps and WINE to satisfy the requirements.

Collapse
 
firecentaur profile image
Paul Preibisch

if u are a full stack dev, or freelance solutions provider, its nice to have adobe products at your fingertips without rebooting while in programming mode

Collapse
 
oliveirawro profile image
oliveirawro

I would like to give my 20 cents and suggest to use just one single script (/home/youruser/autoexec.sh) to call ALL services wanted thru Task Scheduller.

And not just for postgres.

But man... you save the day!!

Thanks a lot!!!!!

Collapse
 
blarvin profile image
North

I'm afraid it didn't work for me...
I need to run the same command: service postgresql start and it just isn't started (it runs fine as root/sudo). File permissions for the script are good wrxwrxwrx. I do believe shell starts and the script is being run.

I have Windows build 17692. I must be missing something...

Great post, I appreciate it!

I'll keep trying. Cheers.

Collapse
 
blarvin profile image
North

UPDATE: IT IS WORKING!!!!!!!!!!!

Yay!!!

I had been rebooting the windows machine by "restart"... This morning when I booted it from shutdown state, the Postgres server was running. And this is from the script and task scheduler you explained.

Thanks again.

Collapse
 
doganmeh profile image
Mehmet Dogan • Edited

it worked for me, thanks!

However, I am curious why not put 'sudo' in bash script, but put it in the task scheduler's command. If you put it in the bash script, you could avoid typing sudo when you used it from the console.

Collapse
 
peteravram profile image
Peter Avram

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:

I would not do what is suggested here.

What you have done is allowed passwordless execution of a user-editable shell script haven't you?

If I wanted to execute arbitrary code under sudo I would simply edit that shell script and then use the permission to execute that shell script as a sudo user.

You need to make sure that whatever script you are allowing anyone to call without providing the SUDO password (as the sudo user) is uneditable.

Otherwise there is NO advantage to trying to avoid allowing passwordless SUDO.

I'm not a nix guru, so I don't have a working solution for others to read here, but the specific advice given here isn't great, I'd suggest looking for a different script that is owned by the sudoers that cannot be edited by a non-sudo'er to allow passwordless sudo'ers to execute to achieve the same functionality.

Collapse
 
rizkysyazuli profile image
Rizky Syazuli

I was also late to realize that Apache and MySQL was not started automatically in WSL land.

Although this method shows an awkward flash of bash window during startup. But hey, at least it works. Thx for the tip!

Collapse
 
wlisesrivas profile image
Wlises R.

I just start the postgresql server in the script and add that script to ~/.bashrc

Collapse
 
mohammedalmahdawi profile image
Mohammed

I've tried very hard to make it work with Redis but it didn't work!

I used this official Redis article to install Redis redislabs.com/blog/redis-on-window...

Any help?

Collapse
 
satalink profile image
Neal Garrett

Thanks! You got my engine started on this. Here's what I came up with.

satalink.blogspot.com/2019/05/wind...

Collapse
 
ben profile image
Ben Halpern

Nice. As a non-Windows user who is intrigued by WSL I found this a useful look into where Windows devs heads are at with this stuff. Have you encountered any major frustrations yet?

Collapse
 
ironfroggy profile image
Expert Amateur

Oh for sure! It has some filesystem performance issues in the IO layer between the Linux kernel interface and the Windows storage system. It doesn't affect most things but operations on lots of tiny files (like a Git repo!) can be a big problem. The team is working on that, I understand.

Collapse
 
kirkrohani profile image
Kirk Rohani

Thanks @ironfroggy ! This helped me as well.

Collapse
 
jimmerk profile image
Jim Merk

Perfect. Just used this to start php7.3-fpm, nginx and mysql. Works well! Thank you!