问题
I've created and scheduled a batch file in Windows 8.1 (to perform rsync offsite backups through an SSH tunnel) and I can run it:
- manually
- through task scheduler manually when I right click -> Run
- through task scheduler automatically when I am logged on and it's scheduled to run at a given time
... but when I'm not logged on, it just sits at "Running" even though I've set it as follows:
- user=me (I'm a member of admin group)
- run whether logged on or not
- run with highest privileges
I can see in Task Manager that rsync.exe and ssh.exe are running, so it must be hanging on the rsync/ssh call. That leads me to believe the problem is that, even though I've explicitly set the user name, something isn't really running as me? (sidebar: I've also tried running the task as SYSTEM user - no luck there either)
The command I'm issuing looks like this:
rsync --archive --verbose --human-readable --hard-links --delete --exclude '*.log' --exclude '*log*.*' -e "%CWRSYNCBIN%\ssh -p 22103 -i C:\Users\test\.ssh\id_rsa" "/cygdrive/c/Users/test/Downloads" admin@1.2.3.4:/path/to/remote 1> %REPORTLOG% 2> %ERRORLOG%
Any ideas? (thanks!)
回答1:
This seems to be problem with home directory and non-interactive environment in scheduled jobs in newer Windows version (starting from Server 2008 and Vista?). I find it in few discussions, but with not much info to solve it:
- http://answers.google.com/answers/threadview/id/528721.html
- Rsync for Windows cannot run on Task Scheduler
- https://serverfault.com/questions/207118/cwrsync-on-server-2008-r2-as-a-scheduled-task
- https://superuser.com/questions/368828/rsync-on-windows-xp-works-fine-on-demand-but-hangs-when-run-as-a-scheduled-task
My solution was:
- set task to run under user that is proven to be able to finish rsync, set run whenever user are logged on or not, I also set to does not save password,
- find location of .ssh folder under this user (in his home directory),
- copy .ssh to simplier path to use it in HOME variable (
SET HOME=D:\CWRSYNC\HOME
), - use following script to run rsync:
@ECHO OFF
SETLOCAL
SET CWRSYNCHOME=D:\CWRSYNC
SET HOME=D:\CWRSYNC\HOME
SET CWOLDPATH=%PATH%
SET PATH=%CWRSYNCHOME%;%PATH%
rsync.exe -rtvz --delete /cygdrive/b/ user@192.168.1.2:/volume1/NetBackup/ >> D://nas-backup-log.txt
回答2:
When I experienced this problem on Windows Server 2012 R2, I found SSH.exe was experiencing a permissions error when attempting to create a known_hosts file, despite the task running with an admin's credentials and with highest privileges.
The solution was to point to the user's existing known_hosts file using the UserKnownHostsFile
SSH option:
rsync -e "ssh -i sshkey -o UserKnownHostsFile path/to/known_hosts" source dest
来源:https://stackoverflow.com/questions/26273547/windows-8-1-task-scheduler-wont-execute-rsync-ssh-without-being-logged-on