14

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 every time 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.

In addition, that particular question was specific for SSH servers which requires that the security aspect if considered and dealth with resulting in overcomplication of steps. Irrespective, the steps explored in that qusetion have been tried and they did not work. In addition, that question is highly specific to SSH servers whilst this question deals with a general enviromental requirement i. I need to know HOW to run services in WSL (which may include but is not limited to SSH serrvers)

In effect - A more simplified solution is required than How to run Ubuntu service on Windows (at startup)? provides.

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

I have a workstation with 96GB RAM and as such I will prefer to use this as the dual Linux/Windows machine and not my puny laptop.

I have tasks on both Linux and Windows and really need/prefer the Linux solution provided by Windows.

I have reviewed the other question and there is a package called Mysys that seems to provide a solution however this departs from the integration provided by Microsoft which was a great way forward.

  • 1
    Possible duplicate of How to run Ubuntu service on Windows (at startup)? – Biswapriyo Jul 29 '18 at 6:18
  • 1
    It is NOT a possible duplicate but an extension of it. The routines explored in that question are way beyond what I need. In addition, this is a specific quation atht also refers to that question. You did not read that question, did you? – seanbw Jul 30 '18 at 1:18
  • 1
    Checking your question, you are running sudo ... xmr-stak-cpu using a scheduled task. -- Have you ran visudo and included the %sudo ALL=NOPASSWD: /xmr-stak-cpu/bin/xmr-stak-cpu to avoid problems with the command asking for a password? Do you (really) need the sudo? can you run the miner without sudo privileges? – Jaime Jul 30 '18 at 15:32
9

In WSL, the linux distributions run only after a first linux command is invoked. If you wanna run a linux deamon (a service) you must configure the server in linux and run any command in that linux distribution.

There are many pages and answers that show how to create a script to start a WSL linux when your computer starts.

  • There is a wsl-autostart VBS script that you can use. You can install the script and change the commands.txt with custom linux commands.
  • There is a step by step tutorial to start automatically an ssh server on WSL.
  • There are also options to create policies to run the program as an initial tasks (using the gpedit.msc command) or a scheduled task that run at startup (using the taskschd.msc).

NOTE: If your program must be executed with sudo, you must configure it to start the program without asking a password.

  • Run visudo in the linux and add a line at the end of the file: %sudo ALL=NOPASSWD: /full/path/to/program
  • This may the the solution I am looking for. I will try and see if I can hack it – seanbw Jul 30 '18 at 1:20
  • Why was this penalised? – seanbw Aug 2 '18 at 12:47
  • +1 for the wsl-autostart script. Only downside is this triggers a UAC prompt on every start up... :/ – Inigo Sep 23 '18 at 17:49
  • 1
    ...so the way I did this was to use the wsl-autostart script, but skip the instructions that tell you to create a startup item in the registry. Instead, use Task Scheduler to create a task the runs the start.vbs file on login with maximum user priviliges. I can now run this to quietly start the cron service in WSL every time I start my machine- works like a dream ;) – Inigo Sep 23 '18 at 18:23
4

Thank you for your question, it guided me towards this solution. This is my complete, generalized- and particular solution. It consist of 3 steps:

  1. Create a cronjob
  2. Run/enable the cronjob service at startup
  3. Remove prompting for password to start the cronjob service automatically.

In reality your problem is already solved with just step 2 and 3, but since you tried to do it with a cronjob, I also added that step for completeness.

1. Creating a functioning cronjob:

  1. Browse to folder /etc/
  2. Then in folder /etc/ enter:sudo nano crontab
  3. In that file named crontab enter your command.
  4. E.g.: */1 * * * * root touch /var/www/myFile
  5. To create a file named myFile in location /var/www/ every minute.
  6. For completeness: */1 * * * * root touch /var/www/myFile would mean: create that file every 1st minute of the hour.

An example of the crontab file could look like (I only added the last line, the rest was already there in my setup):

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
*/2 * * * * root touch /var/www/myFile

2. Enabling cronjob service

To run a command automatically at startup of WSL Ubuntu 16.04 you can:

  1. cd to /home/<your ubuntu user name>
  2. sudo nano .bashrc
  3. The text editor nano then creates/opens a file .bashrc
  4. In that file a lot of examples can be shown already, to just execute your command upon startup of the WSL ubuntu 16.04, write your command on the first line of the .bashrc file.
  5. For example:echo "hello world" as shown in the picture below.
  6. For your particular problem, the particular solution would be to enter the line:sudo ./xmr-stak-cpu
  7. Close the editor with: ctrl+x
  8. Save the file with Y
  9. Exit ubuntu
  10. Restart ubuntu and verify indeed the hello world is printed before your username.

![An example command in .bashrc that is executed upon boot of WSL ubuntu.]1

For example this could be what your .bashrc looks like after you edited it: (I only added the first line on top, the rest was already there in my setup.)

sudo -i service cron start
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
#shopt -s globstar

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi

# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
    xterm-color|*-256color) color_prompt=yes;;
esac

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
        # We have color support; assume it's compliant with Ecma-48
        # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
        # a case would tend to support setf rather than setaf.)
        color_prompt=yes
    else
        color_prompt=
    fi
fi

You can replace the hello worldcommand with sudo service cron start to enable cronjob service. However then you are still required to enter your password manually.

3. Removing prompt for password: Using: https://askubuntu.com/questions/147241/execute-sudo-without-password

  1. Open WSL ubuntu 16.04 (terminal)
  2. sudo visudo
  3. At the bottom of the file add line: <your WSL ubuntu username> ALL=(ALL) NOPASSWD: ALL
  4. E.g with username zq you would add the following line to the bottom of the file:
  5. zq ALL=(ALL) NOPASSWD: ALL
  6. ctrl+x to exit
  7. y followed by <enter> to save.
  8. Then again, close ubuntu and re-open it and verify
  9. The cron service is running automatically when you boot/open WSL ubuntu 16.04 without prompting for password.
  10. (you can check with command: sudo service cron status.)

The code to prevent prompting for password at boot would for example look like (I only added the last line, the rest was already there in my setup):

#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults        env_reset
Defaults        mail_badpass
Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/s$
# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL:ALL) ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d
%sudo ALL=NOPASSWD: /etc/init.d/cron
zq ALL=(ALL) NOPASSWD: ALL

Working towards this solution, I learned cronjobs are intended for things to run periodically rather than at specific events such as startup. To run things at startup in WSL you can use the file /home/<username>/.bashrc.

  • 1
    Copy a section from .bashrc file in your answer so that reader get to know how it looks like after editing. – Biswapriyo Oct 12 '18 at 5:30
  • @Biswapriyo, thank you for your feedback. Initially I added a screenshot of that code but could not get it working. So I pasted the actual code. – a.t. Oct 12 '18 at 5:43
  • 1
    (1) Your answer is very confusing.  As far as I can tell, your bottom line is to use .bashrc and not crontab.  So why is approximately 60% of your answer talking about crontab?  (2) The question says “I need to run a program in WSL every time I start my PC”.  Your answer doesn’t run the program until the user starts WSL and logs in.  This seems not to answer the question.  (3) There’s no reason for a user to use sudo to edit their own .bashrc.  If anything, that can cause problems.  … (Cont’d) – Scott Jan 5 at 3:45
  • 1
    (Cont’d) …  (4) Your answer describes how to disable password verification of sudo across the board.  This is widely regarded as a bad idea.  Note that other answers have showed how to do this for only the required program.  (5) When cron was written, >40 years ago, it handled only scheduled, periodic tasks.  Since then, it has been extended to handle commands at boot time. – Scott Jan 5 at 3:45
2

Download this -> https://github.com/troytse/wsl-autostart

And instead of following the instructions to set up in the registry/group policy/taskschd, just drop a shortcut for start.vbs in your startup folder:

"C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"

Or the system startup folder

"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp"

Works great!

0

My example with mysql service

  1. Create line in /etc/sudoers.d/ (at WSL to prevent asking password):

    %sudo   ALL=(ALL) NOPASSWD: /usr/sbin/service mysql *
    
  2. Create .bat file in Windows startup directory with this line (dir find here: Win+R and shell:startup):

    wsl sudo service mysql start
    

After restarting the service, it will start automatically.

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.