New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to auto start service #511

Closed
rornor opened this Issue Jun 11, 2016 · 14 comments

Comments

Projects
None yet
@rornor
Copy link

rornor commented Jun 11, 2016

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?

@sunilmut

This comment has been minimized.

Copy link
Member

sunilmut commented Jun 15, 2016

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.

@benhillis

This comment has been minimized.

Copy link
Member

benhillis commented Jun 15, 2016

Thanks @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.

@ymeiner

This comment has been minimized.

Copy link

ymeiner commented Aug 12, 2016

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.

@ZuSe

This comment has been minimized.

Copy link

ZuSe commented Apr 10, 2017

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

@skywind3000

This comment has been minimized.

Copy link

skywind3000 commented Jul 26, 2017

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 ??

@dioptre

This comment has been minimized.

Copy link

dioptre commented Jan 10, 2018

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

@sunilmut

This comment has been minimized.

Copy link
Member

sunilmut commented Jan 11, 2018

@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

@troytse

This comment has been minimized.

Copy link

troytse commented Jul 16, 2018

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

@mathieucaroff

This comment has been minimized.

Copy link

mathieucaroff commented Sep 1, 2018

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

@benhillis

This comment has been minimized.

Copy link
Member

benhillis commented Sep 1, 2018

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

@Justsoos

This comment has been minimized.

Copy link

Justsoos commented Dec 8, 2018

@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
@mnpenner

This comment has been minimized.

Copy link

mnpenner commented Jan 10, 2019

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

@Justsoos

This comment has been minimized.

Copy link

Justsoos commented Jan 18, 2019

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

@mathieucaroff

This comment has been minimized.

Copy link

mathieucaroff commented Jan 18, 2019

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

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