How can I run services in WSL
After searching this site and various Q, it is clear that services and systemd is not available for WSL. I need to run a program in WSL everytime I start my pc so I read this page on how to use crontab: How to run Ubuntu service on Windows (at startup)? | Super User but I got confused because the format does not tally with the format in crontab. However this is my cron:
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
PATH=cd /usr/local/src/:cd xmr-stak-cpu:cd bin/
@reboot . sudo ./xmr-stak-cpu
I have also done this:
Run bash/cron loop on start
Create a file called linux.bat in shell:startup
Paste: C:\Windows\System32\bash.exe -c 'while [ true ]; do sudo /usr/sbin/cron -f; done'
It does not work.
How can I run a service in WSL? Surely there must be a way that does not require me to be a Linux phd? Or is there a way to use Windows
Because in Windows I have tried the following: using https://github.com/Microsoft/WSL/issues/612
Run: When the computer starts,
Action: Start a program,
Program: c:\Windows\system32\bash.exe,
Arguments: -c "sudo /xmr-stak-cpu/bin/xmr-stak-cpu -D"
Start in: /usr/local/src/
And as you guessed, it still does not work. Frankly I wish I could do this in WSL because it is my preferred way but I will take any way. Please help guys.
Solution 1:
WSL can’t run true services without additional support from and configuration of the Windows host system because it lacks an “init” daemon among other sub-systems and features that all fully fledged Linux systems have. Such differences are off topic here on Ask Ubuntu but you’re welcome to ask over on our sister site Super User.
Solution 2:
Update August 25, 2019
From: Hands-On with WSL: Executing Daily Tasks it is possible to run cron
without systemd
. I've summarized the link below but please visit for the complete story along with screenshots.
Running Cron Jobs
If you need to run the same script or program at the same time every day, Ubuntu provides the cron command. To learn more about the cron
command, type in man cron
.
Cron isn't currently well-supported within WSL because it's a background service, and WSL is only designed to run programs that are associated with a terminal window. Hence, when you close your WSL Ubuntu window, all Linux processes, including the cron daemon, will be closed. That said, as long as you have your windows open or minimized, your cron jobs will continue to run.
I first checked to see if the cron service was running by typing in service --status-all
. This showed cron had a dash ("-") in front of it, indicating that it wasn't running.
I then started the cron and the atd services by typing in: service cron start
and service atd start
.
To test the cron command, I created a file named MyDate.sh
that had the following two lines in it:
#! /bin/bash
date >> /tmp/MyDate.txt
The first line tells Ubuntu to run the command using the Bash shell, and the second writes the current day and time to file called /tmp/MyDate.txt
. I then ran chmod 755 MyDate.sh
to allow the program to be executable.
Next, I added an entry in cron to run by running crontab –e
and added the following line to the crontab:
40,45,50 * * * * /home/user01/MyDate.sh
This line tells cron to run MyDate.sh
at 40, 45 and 50 minutes of every hour of every day.
After waiting an hour I looked at the /tmp/MyDate.txt file and I saw that the MyDate.sh did run at the designated times. The first two entries were from the two times that I manually ran the program.
Original answer from April 2018
The Super User answer that google search returns is out of date. Microsoft has provided native background WSL task support and startup services. This December 4, 2017 article describes how to set it up.
Cron script
Your cron
script contains this line:
@reboot . sudo ./xmr-stak-cpu
- I'm not sure what the first
.
is supposed to do. I think it should be removed. -
sudo
is not required becausecrontab -e
jobs run as root in the first place. - The command
./xmr-stak-cpu
should be something like/usr/local/bin/xmr-stak-cpu
or/home/<your name>/bin/xmr-stak-cpu
.