7 August 2018

If you’re running Ubuntu or another linux shell in Windows 10 via WSL (Windows Subsystem for Linux), you’ve probably wondered about using cron.

Cron is super-handy for doing things, like, running a backup.

You see, as useful as WSL is, it stores all the linuxy files in a way that is not exactly easily backup-able by File History or anything else… as far as I can tell.

Worse yet, even when you try to set up a cron job to run a backup, it doesn’t work!

What to do, what to do?

Set up cron as usual

The first thing you’ll need to do is set up a script and cron job so that theoretically, the backup will happen inside linux.

I’m using Ubuntu 16.04 at the moment, so the stuff below may need to change a bit if you’re using another flavor of linux on Win 10.

For example, to back up the entire /root dir where all your git and other goodies are stored, you might have a backup script /root/backup.sh like so:

# This script makes a backup of Ubuntu WSL stuff every day
cd /root
dpkg --get-selections | grep -v deinstall >> /root/packages.txt
tar czvf /opt/backup/WSLbackup.tar.gz /root/*
cp -f /opt/backup/WSLbackup.tar.gz /mnt/e/Backups/WSLbackup.tar.gz
rm /opt/backup/WSLbackup.tar.gz
touch /var/log/backup_success

Right, so that’s pretty straightforward:

  1. Change dir to /root
  2. Save the installed packages to the file /root/packages.txt so that if WSL gets hosed, you’ll remember what you need to reinstall.
  3. Create a temporary compressed file of everything in /root
  4. Copy the archive to your E:\Backups (recall that all your Windows drives are available as /mnt/X/, where X is the Windows drive letter)
  5. Delete the temporary compressed archive
  6. Touch /var/log/backup_success so that you have a date-stamped file that indicates if the backup ran recently

Next, create a file \etc\cron.d\run_backup with something like the following contents:

# Run backup every day at 5am
# m h dom mon dow       user    command
0 5    *    *    *   root   /root/backup.sh

Grrrrrreat…  That just runs your backup script as root every morning at 5am.

All set, right? Not so fast!

So, you might think that you’re done, but you’d be wrong.

It appears that at the time of writing anyway, Microsoft “doesn’t allow” background linux tasks.

You can use Task Scheduler and run a bash command with 75,000 crazy parameters from the Windows’ side of things, and apparently that works. But holy cow, it’s crazy.

Now, when I say MS doesn’t allow background linux tasks, that’s not entirely true… It turns out you just need to run:

service cron start

Done? Nope!

You see, the problem is that if you close your WSL window, it’s like shutting down linux. So, no cron jobs.

So here we have a bit of a hack. Edit /root/.bashrc, and add the same line at the very end:

service cron start

Now, every time you fire up Ubuntu on Bash on Windows on Puter on Earth, the cron service will be automatically started.

As long as you leave the bash window open, your cron job(s) will run nicely.

So, adjust your backup (or whatever) times accordingly.

If you shut down your puter at night, for example, you could just run the backup script once every hour. While you work, the backup will run.

And you’re done

The beauty of this solution is that:

  • You get to use cron to do whatever you would normally do in linux
  • It starts automagically when you open WSL
  • The backup file is saved to a Windows drive, and is fully accessible via Explorer or the Windows command prompt

That’s it!

One final note

It seems that while you can find the linux system’s files in your Users folder, this is not recommended. In fact, the location seems to have changed yet again.

The reason for this is that modifying any of those linuxy files from Win 10 apparently causes things to break nicely on the linux end of things.

So, if you need a backup, make a compressed archive in linux, and dump that archive onto the Windows file system. DONE!

Don’t miss:

Get Scottie Stuff!
Run cron jobs in Windows Subsystem for Linux
Tagged on:                         

9 thoughts on “Run cron jobs in Windows Subsystem for Linux

  • Pingback: "Linux on Windows" missing after a Win 10 upgrade? No problem! | Scottie's Tech.Info

  • 1 March 2019 at 10:02

    Thank you very much for this article.

    One interesting improvement I have found is to create a VBS script that I launch when Windows starts (for instance by storing it under shell:startup). This scripts launches an invisible bash session which in turn launches the cron service. An since it’s invisible, you are less likely to kill it by mistake. This means you can close all visible bash windows and it will still run in the background, leaving cron jobs operational. You can always kill it in task manager if needed.

    A script example can be found here: https://gist.github.com/leonelsr/cde77574519eb1fd672bc9690e01257e

    And FYI, “shell:statup” refers to this location:
    C:\Users\my-user-name\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

    • 1 March 2019 at 13:46

      That looks WAY simpler than the other examples I found for the “background bash” method. Thanks!

  • 10 October 2019 at 15:27

    Eureeka! Many thanks Scottie.

    service cron status
    * cron is running

    Thats more like it.

  • 16 February 2020 at 05:33

    In latest updates to win10 services stays running in the background even after I close the terminal.
    Also the ‘touch …success’ command is not good because it will run even if any of the previous commands failed. You should check exit status for each of the commands instead and only then run it.

  • 27 September 2020 at 15:59

    On the last tip, I’ve symlinked a folder to my desktop in windows from my debian instance. very easy to just mv files to my desktop and use whenever!


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.