Ubuntu – Windows Subsystem for Linux Autoload Apache2 and MySql

16.04Apache2bashMySQLwindows-subsystem-for-linux

I have activated the Developer Platform in Windows and installed Ubuntu VIA Windows Store which works fabulous! Only thing I need is when I "Activate" Ubuntu Apache2 and MySql does not autostart.

Is there a simple way to make this happen under this Platform? When I open Bash, I can just manually start the Services but Auto Start would be preferred. I have tried using a cron job but the services never started, perhaps my syntax was wrong?

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
@reboot . $HOME/.profile; /usr/sbin/sshd -D
@reboot . $HOME/.profile; service mysql start
@reboot . $HOME/.profile; service apache2 start

Best Answer

Found It!!!!! Although not elegant.

Instead of looping or sleeping just add "bash" to the end of the shell script. Here are my scripts for apache and mysql to run on startup.

VBS file (runs on startup) Win + r shell:startup create autostart.vbs

Set WshShell = CreateObject("WScript.Shell") 
WshShell.Run "C:\Windows\System32\bash.exe -c ~/autostart.sh",0
Set WshShell = Nothing

autostart.sh file (Just make this in your Home Folder, chmod +x runnable)

#!/bin/bash
sudo service mysql start
sudo service apache2 start
bash

and in /etc/sudoers (Append to the end of the file)

# Allow apache2 and mysql to start without a sudo password
%sudo   ALL=(ALL) NOPASSWD: /usr/sbin/service apache2 *
%sudo   ALL=(ALL) NOPASSWD: /usr/sbin/service mysql *

If I need to kill it all I can find the bash process in Task Manager and kill that.

Credit: https://github.com/mstrelan

Related Question