32

I am trying to schedule a bash script to run with Bash on Ubuntu on Windows in Windows 10. Every time that I to write the cron, I get the following error messages in the terminal:

crontab: installing new crontab
/var/spool/cron/: mkstemp: Permission denied
crontab: edits left in /tmp/crontab.4q0z3i/crontab

Here is what the crontab entry looks like:

# m h  dom mon dow   command
27 10 * * * /home/admin/test.sh > /home/admin/logs/test.log 2>&1   

What exactly is going on here?

2
  • Does your use have permissions to write in /var/spool/cron? Does your uses have permissions to use cron?
    – Jdamian
    Commented Dec 22, 2016 at 10:48
  • Hi @Jdamian, how do I check this? There is only one unix account set up in bash on my system.
    – arnpry
    Commented Dec 22, 2016 at 10:49

9 Answers 9

51

You need to add yourself to the crontab group.

usermod -a -G crontab (username)

Once you have done this, you also need to make sure that cron is running. Usually this is started with start cron however upstart does not work on WSL from what I can tell, but sudo cron does the job.

One caveat to this is that once you close all bash windows, cron will stop running even though your computer runs. However, as long as you have a bash window open and cron running, it will perform as expected.

6
  • 4
    This helped, Thanks! One reminder for some people like me - Restart your bash session for this to take effect.
    – Ran Sagy
    Commented Mar 24, 2017 at 14:13
  • 3
    Also, if you want cron and other background jobs to keep running after you close all bash windows, you can use this vb script at start up, and it keeps a hiden instance of bash running. gist.github.com/leonelsr/cde77574519eb1fd672bc9690e01257e
    – jeffpkamp
    Commented Sep 27, 2017 at 15:37
  • I've been using wabash and its daemon for this, but thanks for the heads up!
    – Ran Sagy
    Commented Oct 2, 2017 at 15:11
  • 3
    Using the script mentioned by jeffpkamp works great. To auto-start cron I added sudo cron to my ~/.bashrc file and myuser ALL=(ALL:ALL) NOPASSWD:/usr/sbin/cron to my /etc/sudoers file.
    – 11101101b
    Commented Oct 19, 2017 at 14:42
  • 11101101b it asks for my password while starting bash on windows. Any workaround? Commented May 8, 2021 at 17:54
24

To make sure cron is actually running you can type service cron status. If it isn't currently running then type service cron start and you should be good to go.

3
  • 3
    I think it's service cron status instead of service status cron
    – Umair Khan
    Commented Nov 29, 2019 at 5:59
  • I wonder why even cron need to be started and as a service Commented Jan 25, 2021 at 23:42
  • @YakobUbaidi It likely has to do with background services not being available in general on WSL, such as systemd.
    – catsock
    Commented Jun 18, 2021 at 17:00
5

There is workaround mentioned on the github bug for this one, can't seem to find it now!

sudo crontab -u $USER -e
0
4

Run the command sudo service cron start and give password.

0

Seems that your crontab does have the setgid permission set like this,
-rwxr-sr-x 1 root crontab 39024 May 5 2016 /usr/bin/crontab

you should do sudo chown <username> /usr/bin/crontab

then sudo chmod g+s /usr/bin/crontab

You should be root to do all this.

13
  • Hi @Arduino Sentinel, still getting the same error message.
    – arnpry
    Commented Dec 22, 2016 at 10:55
  • what is the output of this ls -l /var/spool/cron/crontabs Commented Dec 22, 2016 at 10:59
  • ls: cannot open directory /var/spool/cron/crontabs: Permission denied
    – arnpry
    Commented Dec 22, 2016 at 11:00
  • did you run that with sudo? Commented Dec 22, 2016 at 11:04
  • Yes, when run without sudo, I got the following message: chmod: changing permissions of ‘/var/spool/cron/crontabs’: Operation not permitted
    – arnpry
    Commented Dec 22, 2016 at 11:06
0

try to put this powershell command in a windows task and see what happens

bash -c "rsync -acAXvc --delete --numeric-ids /mnt/d/no_movies/* rsync://10.66.6.66/danielle_no_movies"

bash -c "command" means run this command using the linux subsystem bash shell

the command is "rsync --rsync_switches /mnt/windows_drive_letter/source_dir/* rsync://rsync_daemon_address/rsync_module"

i just came up with it and i'm still testing so i can't promise that it will actually work.

i am running it from a powershell window with no admin privileges right now and it is reporting that it is "sending the incremental file list". the task manager reports that there is an rsync process that is doing a lot of stuff with drive d:

0

It looks like this ought to start Crontab once using tmux and have it live even if you close your bash session(see here). I haven't tried this yet (but will soon).

0

crontab does not run under windows wsl. Linux is not started with systemd

1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Mar 3 at 14:09
-5

WSL does not currently support background processes.

3
  • 7
    Can you kindly add a reference ?
    – tutuDajuju
    Commented Mar 5, 2017 at 7:39
  • 4
    It does now "Starting in Windows Insiders Build 17046, WSL supports background tasks (including daemons)" - Background Task Support in WSL
    – benrifkah
    Commented Oct 19, 2018 at 23:21
  • It does, I have enabled cron and installed even MySql Commented Jun 27, 2021 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.