简体   繁体   中英

Windows 8.1 Task Scheduler Won't Execute Rsync/SSH Without being Logged On

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!)

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:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM