10

Windows Subsystem for Linux, as of May 2018 still doesn't support init.d or systemd service management, and I ran into issues with services and several options online, with non working optimally for all cases

4
  • Is there a reason why you can't create a Windows service for them? May 26, 2018 at 13:42
  • @IgnacioVazquez-Abrams can you please provide an explanation to how that works? May 26, 2018 at 13:45
  • As to how services on Windows work? May 26, 2018 at 13:46
  • As to how to create a service to run a Linux service/start up script from windows @IgnacioVazquez-Abrams May 26, 2018 at 13:52

3 Answers 3

5

The following bash script works for me.. Note the continue operator is needed to skip scripts that fail, and the start argument is needed by most service scripts.. i believe

for f in /etc/init.d/*; do sh "$f" start || continue; done ;
3
  • Where is this suppose to go and what are you claiming it does?
    – lijat
    Mar 1, 2019 at 14:42
  • It could go in a bash script and run on every reboot via Cron. For what it would do, it would attempt running any /init.d script, but skip any which fails Mar 31, 2019 at 22:07
  • my cron daemon is not started automatically in WSL
    – sjas
    Jul 3, 2020 at 16:00
2

This is just a general direction (it is how I did it) - maybe someone can improve the idea.

Whenever LxssManager service is started (restarted) there are 2 entries in Event Viewer -> Windows Logs -> Security

Audit Success Sun 13.01.2019 13:34:52 Microsoft Windows security auditing. 4672 Special Logon Audit Success Sun 13.01.2019 13:34:52 Microsoft Windows security auditing. 4624 Logon

If you right click the one with "Special logon" in task category column you can "Attach task to this event" with folowing actions (using Task Scheduler)

Program/script: C:\Windows\System32\wsl.exe and with Arguments: /usr/bin/apache.sh (or any other .sh file you have created)

On the linux side create .sh file to start your service within Linux (this is how I did it):

create apache.sh and put it into /usr/bin/

My apache.sh looks like:

#!/bin/sh 
sudo service apache2 start

If your script requires linux authentication you can use

ubuntu config --default-user root (will set default user as root ) - run this from cmd.exe with admin privileges.

For more on this https://docs.microsoft.com/en-us/windows/wsl/user-support

This seems like nice automated hussle free method (at least to me)

0

I've not see any real good answers to this in the past so I developed the following script that I use on a few machines. It makes things like apt install apache2 much easier as its then typically automatically configured. I added comments to make it easier to know how to use it and threw it up on github. Also decided to fix an annoying visual bug.

This assumes that you have nopasswd in your sudoers file as otherwise you will get a prompt at startup.

#!/bin/bash

# Add new shortcut under the following directory
# "C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
# Make the shortcut here the following is an example, assuming this file is /etc/init.sh
# You can change the 3 to 5 for example if for example you used x11
# C:\Windows\System32\bash.exe -c "/usr/bin/sudo /etc/init.sh 3"
# You will also need to update your sudoers to allow NOPASSWD as otherwise it will prompt you for a password each time.

[[ -z $1 ]] && { echo "Need to specify a run level"; exit 127; }

run_level=$1

[[ -d /etc/rc${run_level}.d/ ]] ||  { echo "Need to specify a run level"; exit 127; }

for rc_service in /etc/rc${run_level}.d/K*; do
        [[ -e "$rc_service" ]] && $rc_service stop
done

for rc_service in /etc/rc${run_level}.d/S*; do
        [[ -e "$rc_service" ]] && $rc_service start
done

I'll take any bug fixes or suggestions here: https://github.com/masshuku/wsl-init.sh

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .