8

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.

2

2 Answers 2

3

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.

1

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 because crontab -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.
6
  • I have read that paper and i'm honest enough to say I am a bit uncomfortable with the process because the person writing it assumes a level of familiarity I don't have so if you don't mind I will summarise what I understand are my tasks; 1 install tmux if not installed already. 2 install htop. 3. run both tmux and htop. then recreate the cron -e file I wrote. next run the bash cron on startup. then create the windows task to run the cron. Would you agree that these are the tasks to run in tmux?
    – seanbw
    Apr 19, 2018 at 16:23
  • Looks as if yours is the only available option and therefore the only answer. Prior to marking as such can you please look at my comment above and confirm my steps. before I crash the workstation. Thanks
    – seanbw
    Apr 26, 2018 at 9:51
  • ok. I will wait. Thanks @WinEunuuchs2Unix
    – seanbw
    Apr 27, 2018 at 17:08
  • @ WinEunuuchs2Unix: Hi, I hope you haven't forgotten this task. I am on Insider Privelege and my current windows install is Build 17025; Pre_release 171020-1626: In case this is relevant. I am on the slow lane after coming from the fast lane as my HPZ800 kept crashing permanently. I realised that HPZ800 did not like cutting edge. Definitely waiting to hear from you regarding how you can achieve this because the answer will respond to a lot of needs.
    – seanbw
    May 30, 2018 at 15:29
  • @ WinEunuuchs2Unix : I am on the wrong lane? Which lane should I be for the script to work as I have another PC I can use. An HP Gen 8 1610T I think.
    – seanbw
    Jun 8, 2018 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.