Cron in Cygwin for Windows

Setting up services like Cron are not the way you are used to. In Linux and Unix its pretty straight forward since you are running in your own OS instance. With Cygwin it is tightly coupled with Windows so services like SSHD and CRON actually run in Windows and not Cygwin. The steps below are what needs to be done in order to get it working properly.

Getting Started

Firs things first, make sure Cygwin is updated. Just find the folder with the “setup-x86_64.exe” file is. It should be in the folder where you obtained the mirror base from: “http%3a%2f%2fcygwin.mirror.constant.com%2f”. Mine was located in the Desktop. When you open this you will go through similar steps that you did when you installed in. The difference is it will default to “pending” which will be any available updates.

If you run into any issues installing even when the cygwin windows are closed, its because services are actually still running in Windows. You need to go to task manager and stop the services associated with Cygwin. See this Post for more information: https://coolperspective.com/2020/02/21/cygwin-unable-to-update/

Here is a screenshot of the Cygwin setup and Cron package:

Okay assuming everything went well we need to make sure cron is installed. The easiest way is to open a Cygwin window and enter: “cron-config”. If it pops up and runs then your good its installed. Now lets configure it. Its pretty self explanatory but we will go through it.

If you start it as yourself you will see this message [WinLaptopAndy ~] : is the Cygwin prompt.

[WinLaptopAndy ~]: cron-config

WARNING: You are not currently in the Local Administrators group.
This script will fail to set file ownership and permissions, to create users,
and to create or start services.
It is recommended that you execute the script from a shell running as Administrator

Do you want to continue? (yes/no) no

Easy fix just right click on your Cygwin64 Terminal executable and run as Administrator:

OK lets try it again, in my example I had it installed already so it will ask if I want to reinstall. Yours will be different if you don’t have in configured yet. The setup is the same:

[WinLaptopAndy ~]: cron-config

The cron daemon can run as a service or as a job. The latter is not recommended.
Cron is already installed as a service under account HOME\Andy.
Do you want to remove or reinstall it? (yes/no) yes
OK. The cron service was removed.
Do you want to install the cron daemon as a service? (yes/no) yes
Enter the value of CYGWIN for the daemon: [ ] cron
You must decide under what account the cron daemon will run.
If you are the only user on this machine, the daemon can run as yourself.
This gives access to all network drives but only allows you as user.
To run multiple users, cron must change user context without knowing
the passwords. There are three methods to do that, as explained in
http://cygwin.com/cygwin-ug-net/ntsec.html#ntsec-nopasswd1
If all the cron users have executed "passwd -R" (see man passwd),
which provides access to network drives, or if you are using the
cyglsa package, then cron should run under the local system account.
Otherwise you need to have or to create a privileged account.
This script will help you do so.
Do you want the cron daemon to run as yourself? (yes/no) yes
Please enter the password for user 'Andy':
Please enter the password for user 'Andy':
Reenter:
Running cron_diagnose …
… no problem found.
Do you want to start the cron daemon as a service now? (yes/no) yes
OK. The cron daemon is now running.
In case of problem, examine the log file for cron,
/var/log/cron.log, and the Windows event log (using /usr/bin/cronevents)
for information about the problem cron is having.
Examine also any cron.log file in the HOME directory
(or the file specified in MAILTO) and cron related files in /tmp.
If you cannot fix the problem, then report it to cygwin@cygwin.com.
Please run the script /usr/bin/cronbug and ATTACH its output
(the file cronbug.txt) to your e-mail.
WARNING: PATH may be set differently under cron than in interactive shells.
Names such as "find" and "date" may refer to Windows programs.

Lets check if its running. In Cygwin enter:

ps -ef |grep cron
ad752h 1073 1072 ? 07:56:12 /usr/sbin/cron

OK its running if it isn’t check the service on Windows side it may be stopped. Below you see mine is running if yours is Stopped then try and start it.

Assuming everything is running we can now create a cron job. Below is mine that I use to backup my Cygwin directories. It runs a backup.sh script that has rsync commands within it:

[WinLaptopAndy ~]: crontab -e
00 12,4 * * 1-5 /home/Andy/backup.sh

I’m not going to get into the crontab specifics there is allot of information on the Web, but in a nutshell here is what my entry isbelow. If you want more info on crontab go here: https://www.geeksforgeeks.org/crontab-in-linux-with-examples/

00 = Minutes
12,16 = Hours (24hr clock) in which they will run.  I have 12pm & 4pm
* = Day of Month (* means every)
* = Month
1-5 = Day of Week (1-5 is weekdays)
/home/Andy/backup.sh

Enter your path to your script or command and save. To check if its in the cron:

[WinLaptopAndy ~]: crontab -l
00 12,4 * * 1-5 /home/ad752h/backup.sh

That’s it, thanks for playing.