Skip to content
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 · 16 comments
Closed

How to auto start service #511

rornor opened this issue Jun 11, 2016 · 16 comments

Comments

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

@benhillis
Copy link
Member

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
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
Copy link

ZuSe commented Apr 10, 2017

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

@skywind3000
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
Copy link

dioptre commented Jan 10, 2018

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

@sunilmut
Copy link
Member

@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
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
Copy link

@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
Copy link
Member

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

@Justsoos
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
Copy link

@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
Copy link

Justsoos commented Jan 18, 2019

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

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

@lenowng
Copy link

lenowng commented Nov 30, 2019

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.

@javierguzman
Copy link

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
Projects
None yet
Development

No branches or pull requests