You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Services do not start when Windows starts (nginx, php-fpm, mysql) #834
Hi Deyvison Rocha (@deyvisonrocha) -- are you able to run a command to start each of these services manually? If so: There are instructions here on how to start sshd when your computer starts. You could probably do the same thing for each of the three services that you listed, but swap the command to start sshd with the command that you are using to start each of those processes.
Sounds very much like #612, and the Devs seem to think it needs a resolution of this Uservoice issue.
It's possible to get things running as services, but it is not exactly user friendly. I'm still having trouble getting services to start after the OS boots but before I log in.
One could probably make a folder somewhere, populate it with scripts that need to run as "services", and use run-parts to start them all. Think of it as an alternative to init that just launches things in a folder on startup. Having that as a workaround (or something else) might make it easier for volunteers to support as more and more people try to use WSL. The question is, whether the "official" WSL method that is in the backlog will take long enough coming to make it worth one's while to implement.
Deyvison Rocha (@deyvisonrocha) -- hm... Do you have any errors in either the Windows Task Scheduler or the logs for the programs that you were trying to start?
There was also recently a mention on some ticket (I forget which?) that recent versions of WSL require that the Windows startup script used here to also restart the lxssmanager? Maybe someone here will have seen that thread and can comment.
To give a little bit of context why our service story is currently a little funny. We create a WSL "instance" when an NT client launches a Linux binary. Other NT processes launching Linux binaries will create them within the same instance. When the last NT client goes away the instance is terminated meaning all Linux applications running will exit. This way you're only running Linux binaries when you need them.
Services represent an interesting design problem. To support running them natively we'd have to boot an entire (potentially a subset) Ubuntu OS including the Ubuntu's init and daemons. These would be running at all times and you'd essentially be running two full operating systems with one kernel.
We're definitely looking at ways to support running Linux services better in the future. Potentially some kind of "keep alive" to keep an instance running even after an NT client has gone away.
Ben Hillis (@benhillis) I even came up with a name for this "ubuntu as a linux service host process" idea, "lxsssvchost.exe" with obvious allusions to the Win32 service host process.
Ben Hillis (@benhillis) - As long as the -c "scriptname" option works with bash.exe, if you put a "while true" loop at the end of the script (with some sleeping to keep load reasonable?), it will run pretty much forever. Or one can run a daemon in the "foreground" to keep the environment open forever. The technique for launching it from Task Scheduler without a console window cluttering the desktop means we are almost there (see the instructions at Adam's forum, and other places, for the technique).
It's not a full upstart/init, but it is definitely running without a console window open. Keeping the script running forever or the daemon running in the foreground means the environment doesn't close. So it clearly functions as a service, even if we have to go through extra steps to make it work. So having the devs operating on the assumption that the only thing that can possibly work is the full console while the user is logged in -- that seems to be a bit more restriction than needed or helpful.
rodrymbo Personally the -c "scriptname" in a while loop sounds worse than just keeping a bash.exe window open. What I'm going to suggest is simply a workaround (not a final solution) but I feel like I can share it since this is a developer feature :)
If that part really bothers you, you could always write a small NT app that uses the CreateProcess api to launch bash.exe hidden (and then sleep forever).
That being said, we do have an undocumented com interface that some people have already started poking around at (#790) that could be used to similar effect. Eventually I suspect we will document the interface, but later when things are less in flux.
Ben Hillis (@benhillis) -- well, the while loop is in the bash script, not a CMD script. So it basically keeps the bash script running forever, like a daemon running in the foreground. The point is that technique, run from Task Scheduler, means the console is not running. The process running forever inside bash.exe keeps it from exiting. So while CreateProcess would do it, TaskScheduler does also, and doesn't require me to compile anything. :P
A while loop in Bash (or sleeping forever) is a bit inelegant. If you need something simpler, think of running sudo /usr/sbin/cron -f or sudo /usr/sbin/sshd -D -- both run forever (without an external script sleeping anywhere) if one minimizes the console window, or if one launches it via Task Scheduler with the technique I mentioned so there is no console window to be closed accidentally.
The reason something has to run or sleep forever in the foreground, is that, since it is a script, it is not running interactively. As soon as the last thing finishes, the script -- and bash -- exits, so anything launched into the background is lost, by WSL design. So you can have a script launch things into the background or as daemons, but unless it sleeps or does something in a loop, or launches the last process to run forever in the foreground, as soon as the script finishes, bash and all the stuff it so carefully started running, will close (currently).
Try not to lose too much sleep over the 'interesting design problem' of having two operating systems running. I don't know about everyone else, but according to my Resource Monitor, I've got several operating sized systems running already. My biggest operating system, by far, is called chrome, which on an average day has a commit size of around a half a gigabyte whether there's a browser window on my screen or not. In second place is MsMpEng.exe, which, based on the 200MB committed as I write this, I'm going to go ahead and assume must be running it's own operating system under the hood. The svchost.exe tree comes in third.
So at this point, I'm pretty comfortable with init/upstart starting on boot.
Reacted by Ben Hillis, Adam Kruszewski, cgull, Daniel Bartoníček and mikesm
We are pretty much there, now, except Microsoft seems to be saying that the minimum number of things running (besides init) should be one (bash) while the rest of us are saying we'd like the option to run 0 (or at least 0 bash) without it shutting down. I doubt it would run with 0 very often, but that's a way to generalize, sure.
init in WSL doesn't seem very huge or use much resource (in real time). It probably has a virtual size that is much larger, but it's not using it all the time. I can't imagine Microsoft thinks having init running all the time will bog down a machine being used by a developer. (It might on a phone, but I wasn't contemplating setting up a cheap underpowered phone that way.) So it is just a matter of convincing them it's what we want.
Microsoft wants us to tell them what we want it to do. Almost every use case for me involves some daemon or service or something running in the background. Programs I run (not all) expect to be logging to a syslog daemon. Jobs need to run when I am not sitting there to start them, and they want to log to syslog too. And so forth. I expect to be able to have some things running all the time. Having everything in /etc/init.d running probably wouldn't be that huge a deal, but I don't need more than a few, or even the same ones all the time.
I probably shouldn't call them "windows", those rectangular things with the black background and white text (typically). Regardless of what I call them, I think of them as terminal emulators, like the physical terminals I started out with (after playing with actual teletype-and-paper style machines). But yet they are rectangular, and all those other rectangular things on the desktop are called windows, so there you have it. Bash does of course know whether it is talking to a terminal (interactively) versus running in the background, and what kind of terminal it is controlling. Perhaps we can get away with a relaxed use of "window"?
I think I like the terminal paradigm in linux better than the cmd/conhost console in Wndows. Being required to have conhost hanging around seems somewhat backward. But what I've seen is conhosts closing when they aren't needed. The only conhosts running on my machine are associated with some process that thinks it needs to run in a console (even when it isn't visible), and the conhost closes when that process closes. Maybe there are some that don't close, but I've not seen them. So probably not the best example of things running in the background all the time without closing.
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)
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "C:\Windows\System32\bash.exe -c ~/autostart.sh",0
Set WshShell = Nothing
autostart.sh file
#!/bin/bash
sudo service mysql start
sudo service apache2 start
bash
and in /etc/sudoers
# 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.
Reacted by Tunji Gbadamosi, Fuchal, Rumen Damyanov, srini4000, panjianom, Aleksei Gorb, Alex I. Nițu, Maximilian Füsslin, Alexey Toklovich, Wayne Maurer and 6 more
People can argue back and forward about the best way to do this but at the end of the day as a developer all I want is when services like nginx, PHP-FPM, etc are told to start at start-up I can expect them to be running next time I login to start working on something
Aaron McHale (@AaronMcHale) - Exapanding on fpqc comment, for example, you can see this blog post for support for background tasks. So, while, startup tasks are still not supported, workarounds are possible now.
Sunil Muthuswamy (@sunilmut) Is the old WSL blog abandoned? If so, you should put a post on there pointing people to the commandline blog. Or is that blog going to be reserved exclusively for technical details of the WSL internals?
These would be running at all times and you'd essentially be running two full operating systems with one kernel.
I actually like the idea of 2 full operating systems with one kernel. Just look at the history of the kernel. "Strictly speaking, an operating system (and thus, a kernel) is not required to run a computer. Programs can be directly loaded and executed on the "bare metal" machine, provided that the authors of those programs are willing to work without any hardware abstraction or operating system support." This being the case, there's no reason why we can't make it so services start from Linux upon Windows boot. Just provide the core that allows Debian, RPM, Gentoo, etc. -based systems to run upon boot once you install a distro. Work has already been done to make WSL agnostic to a particular distro. Autostarting services really is a necessity for devs (who are the target audience right now) but also for enterprise scenarios as well. The overall goal of WSL shouldn't just be about devs, it should be about supporting all customers who could potentially utilize it. This one issue would be one of those things to help reach that goal.
Activity
aseering commentedon Aug 9, 2016
Hi Deyvison Rocha (@deyvisonrocha) -- are you able to run a command to start each of these services manually? If so: There are instructions here on how to start sshd when your computer starts. You could probably do the same thing for each of the three services that you listed, but swap the command to start sshd with the command that you are using to start each of those processes.
rodrymbo commentedon Aug 9, 2016
Sounds very much like #612, and the Devs seem to think it needs a resolution of this Uservoice issue.
It's possible to get things running as services, but it is not exactly user friendly. I'm still having trouble getting services to start after the OS boots but before I log in.
One could probably make a folder somewhere, populate it with scripts that need to run as "services", and use
run-partsto start them all. Think of it as an alternative toinitthat just launches things in a folder on startup. Having that as a workaround (or something else) might make it easier for volunteers to support as more and more people try to use WSL. The question is, whether the "official" WSL method that is in the backlog will take long enough coming to make it worth one's while to implement.deyvisonrocha commentedon Aug 9, 2016
Adam Seering (@aseering) It did not work for me. =/
aseering commentedon Aug 9, 2016
Deyvison Rocha (@deyvisonrocha) -- hm... Do you have any errors in either the Windows Task Scheduler or the logs for the programs that you were trying to start?
There was also recently a mention on some ticket (I forget which?) that recent versions of WSL require that the Windows startup script used here to also restart the lxssmanager? Maybe someone here will have seen that thread and can comment.
benhillis commentedon Aug 9, 2016
To give a little bit of context why our service story is currently a little funny. We create a WSL "instance" when an NT client launches a Linux binary. Other NT processes launching Linux binaries will create them within the same instance. When the last NT client goes away the instance is terminated meaning all Linux applications running will exit. This way you're only running Linux binaries when you need them.
Services represent an interesting design problem. To support running them natively we'd have to boot an entire (potentially a subset) Ubuntu OS including the Ubuntu's init and daemons. These would be running at all times and you'd essentially be running two full operating systems with one kernel.
We're definitely looking at ways to support running Linux services better in the future. Potentially some kind of "keep alive" to keep an instance running even after an NT client has gone away.
fpqc commentedon Aug 9, 2016
Ben Hillis (@benhillis) I even came up with a name for this "ubuntu as a linux service host process" idea, "lxsssvchost.exe" with obvious allusions to the Win32 service host process.
rodrymbo commentedon Aug 9, 2016
Ben Hillis (@benhillis) - As long as the -c "scriptname" option works with bash.exe, if you put a "while true" loop at the end of the script (with some sleeping to keep load reasonable?), it will run pretty much forever. Or one can run a daemon in the "foreground" to keep the environment open forever. The technique for launching it from Task Scheduler without a console window cluttering the desktop means we are almost there (see the instructions at Adam's forum, and other places, for the technique).
It's not a full upstart/init, but it is definitely running without a console window open. Keeping the script running forever or the daemon running in the foreground means the environment doesn't close. So it clearly functions as a service, even if we have to go through extra steps to make it work. So having the devs operating on the assumption that the only thing that can possibly work is the full console while the user is logged in -- that seems to be a bit more restriction than needed or helpful.
benhillis commentedon Aug 9, 2016
rodrymbo Personally the -c "scriptname" in a while loop sounds worse than just keeping a bash.exe window open. What I'm going to suggest is simply a workaround (not a final solution) but I feel like I can share it since this is a developer feature :)
If that part really bothers you, you could always write a small NT app that uses the CreateProcess api to launch bash.exe hidden (and then sleep forever).
http://stackoverflow.com/questions/780465/winapi-createprocess-but-hide-the-process-window
That being said, we do have an undocumented com interface that some people have already started poking around at (#790) that could be used to similar effect. Eventually I suspect we will document the interface, but later when things are less in flux.
rodrymbo commentedon Aug 9, 2016
Ben Hillis (@benhillis) -- well, the while loop is in the bash script, not a CMD script. So it basically keeps the bash script running forever, like a daemon running in the foreground. The point is that technique, run from Task Scheduler, means the console is not running. The process running forever inside bash.exe keeps it from exiting. So while CreateProcess would do it, TaskScheduler does also, and doesn't require me to compile anything. :P
A while loop in Bash (or sleeping forever) is a bit inelegant. If you need something simpler, think of running
sudo /usr/sbin/cron -forsudo /usr/sbin/sshd -D-- both run forever (without an external script sleeping anywhere) if one minimizes the console window, or if one launches it via Task Scheduler with the technique I mentioned so there is no console window to be closed accidentally.The reason something has to run or sleep forever in the foreground, is that, since it is a script, it is not running interactively. As soon as the last thing finishes, the script -- and bash -- exits, so anything launched into the background is lost, by WSL design. So you can have a script launch things into the background or as daemons, but unless it sleeps or does something in a loop, or launches the last process to run forever in the foreground, as soon as the script finishes, bash and all the stuff it so carefully started running, will close (currently).
aseering commentedon Aug 10, 2016
Bash scripts can sleep forever too :-)
sleep 99999999999999....benhillis commentedon Aug 10, 2016
Sleep -1 for the win?
aseering commentedon Aug 10, 2016
Nope! No sleep for us.
therealkenc commentedon Aug 10, 2016
Try not to lose too much sleep over the 'interesting design problem' of having two operating systems running. I don't know about everyone else, but according to my Resource Monitor, I've got several operating sized systems running already. My biggest operating system, by far, is called
chrome, which on an average day has a commit size of around a half a gigabyte whether there's a browser window on my screen or not. In second place isMsMpEng.exe, which, based on the 200MB committed as I write this, I'm going to go ahead and assume must be running it's own operating system under the hood. Thesvchost.exetree comes in third.So at this point, I'm pretty comfortable with
init/upstartstarting on boot.aseering commentedon Aug 10, 2016
therealkenc -- you should start emacs, add another operating system :-)
(says /me as an emacs user)
aseering commentedon Aug 10, 2016
rodrymbo et al -- I've summarized the
sleep <bignum>approach here:http://wsl-forum.qztc.io/viewtopic.php?f=6&t=10
Definitely a workaround. But it might help for now.
5 remaining items
rodrymbo commentedon Aug 13, 2016
We are pretty much there, now, except Microsoft seems to be saying that the minimum number of things running (besides init) should be one (bash) while the rest of us are saying we'd like the option to run 0 (or at least 0 bash) without it shutting down. I doubt it would run with 0 very often, but that's a way to generalize, sure.
init in WSL doesn't seem very huge or use much resource (in real time). It probably has a virtual size that is much larger, but it's not using it all the time. I can't imagine Microsoft thinks having init running all the time will bog down a machine being used by a developer. (It might on a phone, but I wasn't contemplating setting up a cheap underpowered phone that way.) So it is just a matter of convincing them it's what we want.
Microsoft wants us to tell them what we want it to do. Almost every use case for me involves some daemon or service or something running in the background. Programs I run (not all) expect to be logging to a syslog daemon. Jobs need to run when I am not sitting there to start them, and they want to log to syslog too. And so forth. I expect to be able to have some things running all the time. Having everything in /etc/init.d running probably wouldn't be that huge a deal, but I don't need more than a few, or even the same ones all the time.
I probably shouldn't call them "windows", those rectangular things with the black background and white text (typically). Regardless of what I call them, I think of them as terminal emulators, like the physical terminals I started out with (after playing with actual teletype-and-paper style machines). But yet they are rectangular, and all those other rectangular things on the desktop are called windows, so there you have it. Bash does of course know whether it is talking to a terminal (interactively) versus running in the background, and what kind of terminal it is controlling. Perhaps we can get away with a relaxed use of "window"?
I think I like the terminal paradigm in linux better than the cmd/conhost console in Wndows. Being required to have conhost hanging around seems somewhat backward. But what I've seen is conhosts closing when they aren't needed. The only conhosts running on my machine are associated with some process that thinks it needs to run in a console (even when it isn't visible), and the conhost closes when that process closes. Maybe there are some that don't close, but I've not seen them. So probably not the best example of things running in the background all the time without closing.
therealkenc commentedon Aug 13, 2016
The physical VT100 terminals you started out with talked to a service that ran all the time without closing. It's called getty.
mstrelan commentedon Oct 5, 2016
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)
autostart.sh file
and in /etc/sudoers
If I need to kill it all I can find the bash process in Task Manager and kill that.
AaronMcHale commentedon Dec 4, 2017
People can argue back and forward about the best way to do this but at the end of the day as a developer all I want is when services like nginx, PHP-FPM, etc are told to start at start-up I can expect them to be running next time I login to start working on something
fpqc commentedon Dec 4, 2017
Aaron McHale (@AaronMcHale) parts of this feature are already implemented. It's coming along, so please be patient.
sunilmut commentedon Dec 4, 2017
Aaron McHale (@AaronMcHale) - Exapanding on fpqc comment, for example, you can see this blog post for support for background tasks. So, while, startup tasks are still not supported, workarounds are possible now.
fpqc commentedon Dec 6, 2017
Sunil Muthuswamy (@sunilmut) Is the old WSL blog abandoned? If so, you should put a post on there pointing people to the commandline blog. Or is that blog going to be reserved exclusively for technical details of the WSL internals?
benhillis commentedon Dec 6, 2017
benhillis commentedon Dec 6, 2017
No, the old blog is for more technical topics.
WSLUser commentedon Jan 12, 2018
I actually like the idea of 2 full operating systems with one kernel. Just look at the history of the kernel. "Strictly speaking, an operating system (and thus, a kernel) is not required to run a computer. Programs can be directly loaded and executed on the "bare metal" machine, provided that the authors of those programs are willing to work without any hardware abstraction or operating system support." This being the case, there's no reason why we can't make it so services start from Linux upon Windows boot. Just provide the core that allows Debian, RPM, Gentoo, etc. -based systems to run upon boot once you install a distro. Work has already been done to make WSL agnostic to a particular distro. Autostarting services really is a necessity for devs (who are the target audience right now) but also for enterprise scenarios as well. The overall goal of WSL shouldn't just be about devs, it should be about supporting all customers who could potentially utilize it. This one issue would be one of those things to help reach that goal.
moralrebuild commentedon Aug 31, 2018
2 years passed, does MS have ability to solve this simple issue?
fenchu commentedon Feb 22, 2019
Wanted to install Redis on windows today and found the documentation to refer to run it under WSL.
Nice and easy, but having to run start bash and do
sudo service redis-server startafter each reboot is a no-go. I rather use docker. https://redislabs.com/blog/redis-on-windows-10/therealkenc commentedon Jan 20, 2020
Ran course. LZ for systemd #994.