Skip to content

How to auto start service #511

Closed
Closed

Description

@rornor

Hi,

it seems that Ubuntu used upstart and now uses systemd for service management.

I installed mpd and want this service to auto-start. Installation seem to created valid auto start service script as /lib/systemd/system/mpd.service but service does not start unless I manually start with sudo service mpd start.

So I wanted to ask how to auto start service generally in WSL?

Activity

  1. sunilmut commented on Jun 15, 2016

    @sunilmut
    Member

    Background tasks are currently not supported on WSL. One way to accomplish this right now would be to use the "bash.exe -c " to start whatever you want. If you want that to auto start post Windows boot, you can hook that command to the Windows startup.

  2. benhillis commented on Jun 15, 2016

    @benhillis
    Member

    Thanks Sunil Muthuswamy (@sunilmut). I'd also like to add that any daemons that you run will be terminated when the last bash.exe goes away so you'll have to leave one bash.exe running.

  3. ymeiner commented on Aug 12, 2016

    @ymeiner

    Correct me if i'm wrong just running -c with a command will not be enough, you need to exec bash at the end to get the window to stay open.

    You can either do this:
    bash.exe -c 'bash /path/to/file.sh; bash'
    or add $SHELL at the end of the script.

  4. ZuSe commented on Apr 10, 2017

    @ZuSe

    Is there any more elegant way in the meantime that is more similar to systemd/upstart?

  5. skywind3000 commented on Jul 26, 2017

    @skywind3000

    It is 2017 now, we still have this problem.

    the background service is the last thing stopping me to swith from cygwin to wsl.

    Has it been planned in the feature release ?

    Any roadmaps ??

  6. dioptre commented on Jan 10, 2018

    @dioptre

    This question is not answered here. Any progress on this?

  7. sunilmut commented on Jan 11, 2018

    @sunilmut
    Member

    Andrew Grosser (@dioptre) - Background tasks, daemons are now supported starting Insider build 17046. See the blog for more details. But, auto start of services is still not available. More detailed discussion on that at #834, #612

  8. troytse commented on Jul 16, 2018

    @troytse

    Hi guys, I made a script to support starting the WSL services on Windows start.
    WSL-Autostart

  9. mathieucaroff commented on Sep 1, 2018

    @mathieucaroff

    Ben Hillis (@benhillis)'s remark is no longer true.
    I can successfully exit wsl while I run shellinabox in a tmux server in wsl, without my shellinabox session being interrupted.

  10. benhillis commented on Sep 1, 2018

    @benhillis
    Member

    Yeah that was 2+ years ago. We have added support for background tasks in the meantime.

  11. Justsoos commented on Dec 8, 2018

    @Justsoos

    Ben Hillis (@benhillis) really? now I run WSL Ubuntu 18.04.1

    # lsb_release -a
    No LSB modules are available.
    Distributor ID: Ubuntu
    Description:    Ubuntu 18.04.1 LTS
    Release:        18.04
    Codename:       bionic
    

    and the services, some with +, but how to set up the - service as autorun ?

    #service --status-all
     [ - ]  acpid
     [ - ]  apparmor
     [ ? ]  apport
     [ - ]  atd
     [ - ]  bootmisc.sh
     [ - ]  checkfs.sh
     [ - ]  checkroot-bootclean.sh
     [ - ]  checkroot.sh
     [ - ]  console-setup.sh
     [ - ]  cron
     [ ? ]  cryptdisks
     [ ? ]  cryptdisks-early
     [ - ]  dbus
     [ - ]  ebtables
     [ - ]  hostname.sh
     [ ? ]  hwclock.sh
     [ + ]  irqbalance
     [ + ]  iscsid
     [ - ]  keyboard-setup.dpkg-bak
     [ - ]  keyboard-setup.sh
     [ - ]  killprocs
     [ - ]  kmod
     [ - ]  lvm2
     [ + ]  lvm2-lvmetad
     [ + ]  lvm2-lvmpolld
     [ - ]  lxcfs
     [ - ]  lxd
     [ - ]  mdadm
     [ - ]  mdadm-waitidle
     [ - ]  mountall-bootclean.sh
     [ - ]  mountall.sh
     [ - ]  mountdevsubfs.sh
     [ - ]  mountkernfs.sh
     [ - ]  mountnfs-bootclean.sh
     [ - ]  mountnfs.sh
     [ ? ]  networking
     [ ? ]  ondemand
     [ + ]  open-iscsi
     [ - ]  open-vm-tools
     [ ? ]  plymouth
     [ ? ]  plymouth-log
     [ - ]  procps
     [ - ]  rc.local
     [ + ]  resolvconf
     [ - ]  rsync
     [ - ]  rsyslog
     [ - ]  screen-cleanup
     [ - ]  sendsigs
     [ - ]  ssh
     [ + ]  tor
     [ - ]  udev
     [ - ]  ufw
     [ - ]  umountfs
     [ - ]  umountnfs.sh
     [ - ]  umountroot
     [ - ]  unattended-upgrades
     [ - ]  urandom
     [ - ]  uuidd
     [ - ]  x11-common
    
  12. mnpenner commented on Jan 10, 2019

    @mnpenner

    Ben Hillis (@benhillis) We can start the cron service manually, and it will run as long as the terminal is open, but when we reboot Windows it doesn't start automatically, nor after opening a Bash window.

  13. Justsoos commented on Jan 18, 2019

    @Justsoos

    Now my solution is adding such like command to ~/.bashrc,
    service start tor
    Works well.

  14. mathieucaroff commented on Jan 18, 2019

    @mathieucaroff

    Yaron Meiner (@ymeiner)

    Correct me if i'm wrong just running -c with a command will not be enough, you need to exec bash at the end to get the window to stay open.

    You can either do this:
    bash.exe -c 'bash /path/to/file.sh; bash'
    or add $SHELL at the end of the script.

    Here's a minor improvement: in bash.exe -c 'bash /path/to/file.sh; bash', the last bash will start bash as a child of the current bash process. Using exec will replace the current process:

    bash.exe -c 'bash /path/to/file.sh; exec bash -i'

    The final -i may be omitted in most situations.
    Use pstree to show the current process with its ancestors.

  15. lenowng commented on Nov 30, 2019

    @lenowng

    Now my solution is adding such like command to ~/.bashrc,
    service start tor
    Works well.

    In this case, for every instance you'll have extra line of command that potentially cause a delay (might not quite noticeable but definitely not elegant) for example when opening a new tab.

  16. javierguzman commented on Mar 15, 2021

    @javierguzman

    Any better way to do this in 2021? Thank you in advance and regards

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