Skip to content

Services do not start when Windows starts (nginx, php-fpm, mysql) #834

Closed

Description

Hello,

Everyday I need start the services on Bash (nginx, php-fpm, mysql). Is there any way to do it is automatic?

Build number: 14393.10

Activity

  1. aseering commented on Aug 9, 2016

    @aseering
    Contributor

    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.

  2. rodrymbo commented on Aug 9, 2016

    @rodrymbo

    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.

  3. deyvisonrocha commented on Aug 9, 2016

    @deyvisonrocha
    Author

    Adam Seering (@aseering) It did not work for me. =/

  4. aseering commented on Aug 9, 2016

    @aseering
    Contributor

    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.

  5. benhillis commented on Aug 9, 2016

    @benhillis
    Member

    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.

  6. fpqc commented on Aug 9, 2016

    @fpqc

    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.

  7. rodrymbo commented on Aug 9, 2016

    @rodrymbo

    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.

  8. benhillis commented on Aug 9, 2016

    @benhillis
    Member

    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.

  9. rodrymbo commented on Aug 9, 2016

    @rodrymbo

    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).

  10. aseering commented on Aug 10, 2016

    @aseering
    Contributor

    Bash scripts can sleep forever too :-) sleep 99999999999999....

  11. benhillis commented on Aug 10, 2016

    @benhillis
    Member

    Sleep -1 for the win?

  12. aseering commented on Aug 10, 2016

    @aseering
    Contributor

    Nope! No sleep for us.

    $ sleep -- -1
    sleep: invalid time interval ‘-1’
    Try 'sleep --help' for more information.
    
  13. therealkenc commented on Aug 10, 2016

    @therealkenc

    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.

  14. aseering commented on Aug 10, 2016

    @aseering
    Contributor

    therealkenc -- you should start emacs, add another operating system :-)

    (says /me as an emacs user)

  15. aseering commented on Aug 10, 2016

    @aseering
    Contributor
  16. 5 remaining items

  17. rodrymbo commented on Aug 13, 2016

    @rodrymbo

    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.

  18. therealkenc commented on Aug 13, 2016

    @therealkenc

    The physical VT100 terminals you started out with talked to a service that ran all the time without closing. It's called getty.

    init
      ->getty
          ->login
          ->bash
    
  19. mstrelan commented on Oct 5, 2016

    @mstrelan

    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.

  20. AaronMcHale commented on Dec 4, 2017

    @AaronMcHale

    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

  21. fpqc commented on Dec 4, 2017

    @fpqc

    Aaron McHale (@AaronMcHale) parts of this feature are already implemented. It's coming along, so please be patient.

  22. sunilmut commented on Dec 4, 2017

    @sunilmut
    Member

    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.

  23. fpqc commented on Dec 6, 2017

    @fpqc

    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?

  24. benhillis commented on Dec 6, 2017

    @benhillis
    Member
  25. benhillis commented on Dec 6, 2017

    @benhillis
    Member

    No, the old blog is for more technical topics.

  26. WSLUser commented on Jan 12, 2018

    @WSLUser

    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.

  27. moralrebuild commented on Aug 31, 2018

    @moralrebuild

    2 years passed, does MS have ability to solve this simple issue?

  28. fenchu commented on Feb 22, 2019

    @fenchu

    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 start after each reboot is a no-go. I rather use docker. https://redislabs.com/blog/redis-on-windows-10/

  29. therealkenc commented on Jan 20, 2020

    @therealkenc

    Ran course. LZ for systemd #994.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions