Home > Uncategorized > Running cron on Cygwin

Running cron on Cygwin

August 13th, 2010 subogero

While setting up the ssh-agent on Cygwin, I run into a small problem. After reboot, the ssh-agent is not running yet, but the /tmp/.ssh* files are still there from the previous session. When starting a Cygwin shell, the start of the ssh-agent fails, if these files have not been manually removed before.

Task: delete these files automatically at boot time. I googled it, and the Windows way of doing it seemed extremely complicated. As usual, as it springs to the lips of smug Linux geeks. Not me. Then I ran across the “cron” scheduling daemon somehow, and there it was: insert the line below into the “/etc/crontab” file.

@reboot SYSTEM rm -f /tmp/.ssh*

The rest of this post is a distilled version of my adventures with setting up cron on Cygwin 1.7.5.

cron is the Unix-equivalent of “Scheduled Tasks” in Windows, just better. For instance, you can schedule something to run at boot time (I may have mentioned that before) and, the scheduled commands don’t throw up a scary cmd-window on your desktop. So in the meantime I’ve moved all my scheduled tasks to cron.

Start the cygwin-setup and add the “cron” and “cygrunsrv” packages from the “Admin” category.

We’ll run cron as a service by user SYSTEM. Poor SYSTEM therefore needs a home directory and a shell. The “/etc/passwd” file will define them.

$ mkdir /root
$ chown SYSTEM:root /root
$ mcedit /etc/passwd
SYSTEM:*:......:/root:/bin/bash

The start the service:

$ cron-config
Do you want to remove or reinstall it (yes/no) yes
Do you want to install the cron daemon as a service? (yes/no) yes
Enter the value of CYGWIN for the daemon: [ ] ntsec
Do you want the cron daemon to run as yourself? (yes/no) no
Do you want to start the cron daemon as a service now? (yes/no) yes

Local users can now define their scheduled tasks like this (crontab will start your favourite editor):

$ crontab -e  # edit your user specific cron-table
HOME=/home/foo
PATH=/usr/local/bin:/usr/bin:/bin:$PATH
# testing
* * * * *   touch ~/cron
@reboot     ~/foo.sh
45 11 * * * ~/lunch_message_to_mates.sh

Domain users: it does not work. Poor cron is unable to run scheduled tasks on behalf of domain users on the machine. But there is another way: cron also runs stuff found in the system level cron table in “/etc/crontab”. So insert your suff there, so that SYSTEM does it on its own behalf:

$ touch /etc/crontab
$ chown SYSTEM /etc/crontab
$ mcedit /etc/crontab
HOME=/root
PATH=/usr/local/bin:/usr/bin:/bin:$PATH
* * * * *   SYSTEM touch ~/cron
@reboot     SYSTEM rm -f /tmp/.ssh*

Finally a few words about crontab entries. They are either environment settings or scheduled commands. As seen above, on Cygwin it’s best to  create a usable PATH. Home dir and shell are normally taken from “/etc/passwd”.

As to the columns of scheduled commands see the manual page.

If certain crontab entries do not run, the best diagnostic tool is this:

$ cronevents

It will print out details on all successful and failed commands.

Categories: Uncategorized Tags: ,
  1. James Yuan
    April 17th, 2013 at 10:14 | #1

    Hi,

    Thank you for guide on the cron in Cygwin. It works well for me.

    Now I have another problem that cron a job (as SYSTEM for domain users) to git pull/push from github.com. But it is always failed because “Host key verification failed. fatal: The remote end hung up unexpectedly” even I have uploaded the ~/.ssh/id_rsa.pub to github.com.

    Did you encounter this or any guide for me?

    Thanks,
    James.

  2. April 17th, 2013 at 11:06 | #2

    First test whether you are able to do git push/pull manually.
    If so, it means that the connection/authentication works and you have a cron-specific problem.

    cron jobs run in a limited environment, unlike your login shell.
    If you’re using ssh-agent, you have to set the SSH_AUTH_SOCK variable in the crontab manually.

    SSH_AUTH_SOCK=/home/xxxxx/.ssh/.ssh-socket
    * * * * * ssh foo@bar ls > logfile 2>&1
    
  3. James Yuan
    April 18th, 2013 at 11:59 | #3

    @subogero

    It still doesn’t work after
    a) Open a Cygwin window (as a user) and run ssh-agent. It tells me
    SSH_AUTH_SOCK=/tmp/ssh-cklVtBKR3bJZ/agent.6500; export SSH_AUTH_SOCK;
    SSH_AGENT_PID=6980; export SSH_AGENT_PID;
    echo Agent pid 6980;

    2) Add the above environments into the cron script (as a SYSTEM), and a “ssh-add” command.

    3) It outputs after ‘git pull origin master’
    Host key verification failed.
    fatal: The remote end hung up unexpectedly

    Also tried to add these two lines, same errors.

    /usr/bin/keychain $HOME/.ssh/id_rsa
    . ~/.keychain/`/bin/hostname`-sh

    I am sure /root/.ssh/id_rsa.pub (SYSTEM $HOME is /root) is uploaded into the github.com.

  4. April 18th, 2013 at 20:23 | #4

    I’ve written another post about using ssh-agent:
    http://linux.subogero.com/corporate-git/ssh/

    ssh-agent basically stores the plain decrypted versions of encrypted passphrase-protected private keys in memory and sends in to ssh through the ssh-socket upon request. The goal is to avoid having to enter the passphrase upon every usage of ssh.
    In order to achieve this, it’s not enough to start ssh-agent. You also have to add you private key once to the agent by entering the passphrase. That’s what the ssh-add command is for.

  5. James Yuan
    April 19th, 2013 at 05:52 | #5

    Thanks.
    Actually no paraphrase on my private keys. And use your script, it still fails in Cygwin.

  6. April 19th, 2013 at 07:53 | #6

    If you don’t use passphrases, you don’t need ssh-agent at all.
    You mentioned you’re running this cron job in SYSTEM’s name.
    Does SYSTEM have a home directory and a .ssh folder in it?
    It’s necessary.

  7. James Yuan
    April 22nd, 2013 at 03:30 | #7

    Yes, the HOME is /root:
    drwxr-xr-x+ 1 SYSTEM root 0 Apr 19 18:30 root

    And its .ssh:
    drw——-+ 1 SYSTEM root 0 Apr 17 14:32 .ssh

    Also the /etc/passwd file:
    SYSTEM:*:……:/root:/bin/bash

  8. James Yuan
    May 15th, 2013 at 04:33 | #8

    I resolve it by
    a) Add “StrictHostKeyChecking no” in /etc/ssh_config
    b) Copy .ssh to / , that is the cron job reads /.ssh folder.

Comments are closed.