How to make WSL run services at startup

118,955

Solution 1

My example with mysql service

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

     %sudo   ALL=(ALL) NOPASSWD: /usr/sbin/service mysql start
    
  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.

Solution 2

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

Solution 3

With the release of Windows 11, a new feature has been added to WSL to address this. To run any arbitrary command when WSL starts, create or edit (as sudo) /etc/wsl.conf with the following contents:

[boot]
command="command to run as root; other command to run as root"

For instance:

[boot]
command="service ssh start; service cron start"

These commands run as root, so no need to use sudo with a password.

Note that (currently, at least) if you attempt have multiple command= lines, only the last will be executed. If you need multiple commands to run at boot, separate them with a semicolon as above.

If you need these services to start at Windows login, simply create a scheduled task at login for wsl true. This will start your WSL instance, triggering the boot commands above. Because the services are running, the WSL instance will stay up in the background.

Solution 4

Another option:

1) Create a startup file in Linux at /etc/init-wsl:

#!/bin/sh
echo booting
service ssh start
and make the script executable

chmod +x /etc/init-wsl

2) Schedule executing this file at windows boot or log on

test

Since wsl.exe is able to run commands inside the wsl distro, we simply schedule to run this file through the command wsl -u root /etc/init-wsl. If you have multiple distros, you might want to specify which one with a -d flag, for instance wsl -d Ubuntu-20.04 -u root /etc/init-wsl

Solution 5

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.

Share:
118,955
seanbw
Author by

seanbw

Updated on September 18, 2022

Comments

  • seanbw
    seanbw over 1 year

    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.

    • Biswapriyo
      Biswapriyo almost 6 years
    • seanbw
      seanbw almost 6 years
      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?
    • Jaime
      Jaime almost 6 years
      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?
  • Inigo
    Inigo over 5 years
    +1 for the wsl-autostart script. Only downside is this triggers a UAC prompt on every start up... :/
  • Inigo
    Inigo over 5 years
    ...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 ;)
  • Biswapriyo
    Biswapriyo over 5 years
    Copy a section from .bashrc file in your answer so that reader get to know how it looks like after editing.
  • a.t.
    a.t. over 5 years
    @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.
  • Scott - Слава Україні
    Scott - Слава Україні over 4 years
    (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 - Слава Україні
    Scott - Слава Україні over 4 years
    (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.
  • elig
    elig about 4 years
    why * and not just start ?
  • Andrew Koster
    Andrew Koster about 4 years
    This is the simplest solution by far, and still flexible enough to start anything.
  • pabouk - Ukraine stay strong
    pabouk - Ukraine stay strong over 3 years
    Instead of creating a .bat script you can alternatively create a task in Windows Task Scheduler and set the command wsl sudo service mysql start there.
  • kanlukasz
    kanlukasz over 3 years
    Important note: Do not name the .bat file like the command you are using inside (eg. wsl.bat)
  • jales cardoso
    jales cardoso over 3 years
    Create line in sudo nano /etc/sudoers
  • Neurotransmitter
    Neurotransmitter almost 3 years
    This is a good approach, since you have all control whether it should be started at startup or at user's logon, specify a delay etc.
  • Chris
    Chris over 2 years
    @jalescardoso, no, don't edit the file that way. Always use visudo to edit /etc/sudoers. (This will use your system default editor, which might be nano or vi or anything else.)
  • felipecrs
    felipecrs over 2 years
    Changes to the sudoers are not needed, neither sudo to call service, simply use wsl -u root.
  • Admin
    Admin about 2 years
    This is great, thanks. Shame that it's yet another feature that isn't backported to Windows 10.