Using /etc/cron.{daily, weekly, monthly} on Cygwin (/usr/bin/run-parts)

Joe Smith joeinwap@gmail.com
Wed Jun 9 11:28:05 GMT 2021


Please add 'run-parts' to Cygwin.  I have been using the attached program
(copied from Debian) successfully with Vista, Windows-7, and Windows-10.

mintty screen dump

#!/bin/bash
# Name: /usr/bin/run-parts              Modified by Joe Smith (joeinwap,gmail)
# Purpose: Runs jobs sequentially at regular intervals (daily,weekly,monthly)
# Concept taken from Debian, copied from RHEL-5, modified for Cygwin.
# See end for "How to run cron jobs with elevated privs on Cygwin".

### Set ENV to run under Windows Task Scheduler ###
export SHELL=/bin/bash
export PATH=/sbin:/bin:/usr/sbin:/usr/bin
export MAILTO=root      # See also /etc/ssmtp/ssmtp.conf
export HOME=/root
export USER=root

# keep going when something fails
set +e

if [ $# -lt 1 ]; then
        echo "Usage: run-parts <dir>"
        exit 1
fi

if [ ! -d $1 ]; then
        echo "Not a directory: $1"
        exit 1
fi

### Log STDOUT and STDERR since Task Scheduler is not the same as crond.
LOGFILE=/var/log/cron.log
TMPFILE=/var/log/run-parts.log
exec </dev/null >>$TMPFILE 2>&1

# Ignore *~ and *, scripts
start_time=`date`
for i in $1/*[^~,] ; do
        [ -d $i ] && continue
        # Don't run *.{rpmsave,rpmorig,rpmnew,swp} scripts
        [ "${i%.rpmsave}" != "${i}" ] && continue
        [ "${i%.rpmorig}" != "${i}" ] && continue
        [ "${i%.rpmnew}" != "${i}" ] && continue
        [ "${i%.swp}" != "${i}" ] && continue
        [ "${i%,v}" != "${i}" ] && continue

        if [ -x $i ]; then
                $i 2>&1 | awk -v "progname=$i" \
                              'progname {
                                   print progname ":\n"
                                   progname="";
                               }
                               { print; }'
        fi
done
end_time=`date`         # Merge tmp log with /var/log/cron

if [ -s $TMPFILE ]; then
  echo "Started: $start_time"   >> $LOGFILE
  cat   $TMPFILE                >> $LOGFILE
  echo "Finished: $end_time"    >> $LOGFILE
  echo ""                       >> $LOGFILE
  :   > $TMPFILE
fi

exit 0

cat <<'EOM'
"How to run cron jobs with elevated privs on Cygwin".

This procedure has been used with Cygwin running on Vista or Windows 7, 10.

# Here is an example of a Linux-style /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# run-parts
#1  * * * * root run-parts /etc/cron.hourly
02 22 * * * root run-parts /etc/cron.daily
22 22 * * 0 root run-parts /etc/cron.weekly
42 22 1 * * root run-parts /etc/cron.monthly

###########################################

The second column (hours) is 4 for Linux, which is fine for servers, but
for laptops, values like 0 (midnight) or 22 (10pm) may be more appropriate.

Start -> All Programs -> Accessories -> System Tools -> Task Scheduler

Task Scheduler: Create Basic Task (do this 3 times)
  Name: cron-daily, cron-weekly, or cron-monthly
  Description: Runs cygwin programs with elevated privs.
Trigger:
  Daily,   10:02:00pm, recur every 1 days
  Weekly,  10:22:00pm, recur every 1 weeks on Sunday
  Monthly, 10:42:00pm, recur all months on the 1st
Action: Start a program
  Program:   C:\cygwin64\bin\bash.exe
  Arguments: /usr/bin/run-parts /etc/cron.daily
         or: /usr/bin/run-parts /etc/cron.weekly
         or: /usr/bin/run-parts /etc/cron.monthly
  Start in:  C:\cygwin64\bin
Finish: Open the Properties dialog when finished.
General:
  Run whether user is logged on or not
  Do not store password
  Run with highest privileges
Conditions:
  Wake the computer to run this task
Settings:
  Run task as soon as possible after a scheduled start is missed
  Stop the task if it runs longer than: 1 hour

Populate the directories with shell scripts or symlinks to binaries.
        ln -s /usr/bin/updatedb /etc/cron.daily

I used "ln -s /usr/bin/run-parts /etc/cron-parts" so that this
file can be easily found using "ls -l /etc/cron*".


   joeinwap,gmail
EOM


More information about the Cygwin mailing list