Windows Subsystem for Linux (WSL) Daemons Including Cron

This blog post contains information about using Linux daemons (background processes) under Windows Subsystem for Linux (WSL). This is a repost of

An example of using daemons involves cron and crontab, which let users configure the system to run programs on a schedule. The cron command is a daemon that starts on system initialization and runs persistently in the background. The crontab command lets the user edit the “table” that controls what cron invokes and when.

WSL is mainly a shell. The wsl.exe or bash.exe commands, with or without Windows Terminal, do not perform full Linux initialization including invoking the cron daemon. In fact, it would not make sense for wsl.exe or bash.exe to invoke cron, as you can invoke wsl.exe and bash.exe any number of times in a single Windows session (and any number of users could have sessions on a single machine…).

If you need cron, then you can configure Windows to invoke cron, for example by configuring bash.exe or wsl.exe to run cron as a Windows service. If you only need cron when you are logged in to Windows, then you can configure Windows to invoke cron when you log in to Windows, which will halt cron when you log out.

First, configure sudo to allow cron to run as root without a password. If you don’t do this, then a command window will prompt you for your WSL password every time you log in to Windows:

sudo visudo

Add this line:

%sudo ALL=NOPASSWD: /etc/init.d/cron start

Run explorer.exe shell:startup to access your Windows Programs Startup directory, which is typically something like C:\Users\\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup. Windows invokes the programs in this folder when you log in, and those programs terminate when you log out. In your Startup directory, create a Windows shortcut with the Target property set to the following:

C:\Windows\System32\wsl.exe sudo /etc/init.d/cron start
Properties dialog for Windows shortcut that starts cron

Log out of Windows and log back in.

If you run ps -ef under a bash.exe or wsl.exe window, you should see the /usr/sbin/cron process running under the root user.

Use the crontab -e command to edit the crontab for a user. If it prompts you to select an editor, the /bin/nano editor appears to be similar to pico, which is relatively straightforward. Don’t worry if you appear to be editing a temporary file with a random name.

A relatively simple crontab entry to confirm that cron works is as follows:

* * * * * date >> /tmp/cron.lst

Once each minute, this appends a datestamp to the /tmp/cron.lst file owned by the user responsible to the cron entry.

One thought on “Windows Subsystem for Linux (WSL) Daemons Including Cron

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: