14

I set up some cronjobs a while back using crontab -e. My crontab includes the following line:

* * * * * /usr/bin/touch /home/blah/MADEBYCRON

It's been weeks since I did this. I have never seen /home/blah/MADEBYCRON. I set permissions on my home directory so it should be able to create files in this directory, so why does this file never exist?

/var/log/syslog does not exist.

guninvalid
  • 411
  • 2
  • 5
  • 16
  • 1
    please share the syslog: `grep CRON /var/log/syslog` – Mohammad Fahim Abrar Feb 17 '20 at 06:49
  • 2
    Running cron on WSL seems to require some crazy hacks. There is no indication in your question that you have any such hacks in place. The least wacky I saw from quick google results amounted to turning WSL into a Windows service. (Not particularly precise I'm afraid; my recommended solution is always to ditch Windows.) – tripleee Feb 17 '20 at 07:59
  • 1
    Did you manage to get this to work? I have an exact similar problem; I start atd and cron both manually on every restart, but the cron jobs never execute. In my case I noticed when I was upgrading some packages, the cron started working. However on next reboot it was back to the same situation. – Ali Awais Mar 09 '20 at 18:41
  • Hi, I found this https://scottiestech.info/2018/08/07/run-cron-jobs-in-windows-subsystem-for-linux/ but its a kinda hacking and requires very very big effort .. :/ – xxxvodnikxxx Jul 25 '20 at 12:19

4 Answers4

57

Ensure that the cron service is running. I use WSL with cron every day for my local backups using rsync so this should work.

Use which cron to check its installed, mine says /usr/sbin/cron.

Use crontab -l to list your configured jobs.

Use ps aux | grep cron to look see if cron is running, you should see /usr/sbin/cron if it is.

Use service cron status to check if the service is started.

Use sudo service cron start to start the cron service if it is not running.

Damo
  • 5,698
  • 3
  • 37
  • 55
  • so after running the start command there is still no cron process nor does the status check say anything useful. – Damo Feb 18 '20 at 08:06
  • 5
    This worked for me, figured the service hadn't been started – ThisDude Apr 03 '21 at 14:51
  • @Damo That worked for me, but now it doesn't log anything. The file `/var/log/cron.log` is empty file) and `/var/log/syslog` has no entries about cron. – Dwhitz Jan 12 '22 at 14:01
  • 2
    I solved the log issue updating the conf /etc/rsyslog.d/50-default.conf, see https://serverfault.com/a/470938/321667 and then restarting rsyslog `sudo service rsyslog restart` – Dwhitz Jan 12 '22 at 14:16
  • `Use ps aux | grep cron to look see if cron is running`. And if it isn't running, then go to https://stackoverflow.com/questions/41281112/crontab-not-working-with-bash-on-ubuntu-on-windows. This worked for me with Pengwin (Ubuntu based) in WSL 2. – Stephen Hosking Mar 06 '22 at 23:30
3

Since WSL is not starting services on startup you also need to start rsyslog before starting cron if you want to see the cron logs in /var/log/syslog:

sudo service rsyslog start
sudo service cron start

Then view logs

grep -i cron /var/log/syslog

syslog only has the information which script was run and when. If you want to see the output of the script you need to redirect it to a file, e.g. like this:

* * * * * /usr/bin/touch /home/blah/MADEBYCRON >> /home/blah/cron_output.log 2>&1
Samuli Asmala
  • 1,755
  • 18
  • 24
2

Under recent WSL releases, you can enable Systemd support as mentioned in this Community Wiki answer. With Systemd, the cron service (unit) should automatically be started when you start your WSL distribution.

Note that Systemd is not required under WSL, and may add additional, unneeded overhead. Consider using one of the other existing answers if you don't need Systemd support.

NotTheDr01ds
  • 15,620
  • 5
  • 44
  • 70
1

You need to start the cron service. Services do note start automaticaly on WSL as ist does not use systemd. The easyest way to do this is to add the following line to your .bashrc:

service cron status || sudo service cron start

On the first start you will need to enter the sudo password and you will see somthing like this

start cron service

SePeF
  • 725
  • 7
  • 13