Search code examples
batch-filejenkinscmdftpwinscp

WinSCP in Jenkins fails with "Cannot open file "...\WinSCP.ini". The process cannot access the file because it is being used by another process"


We have some cmd scripts who need to access remote FTP directories to transfer files on Jenkins and we get occasionally errors in the console output with WinSCP.

----------------------------------------------------------     
START SCRIPT - 20.08.2021  8:50:02                                          
----------------------------------------------------------   
                                                                              
**************                                                           
START FTP Operation - 20.08.2021  8:50:02                               
***                                                                     
 Volume in drive \\localdata is DATA
 Volume Serial Number is 52EF-B4C7

 Directory of \\local-directory

06.05.2020  20:07    <DIR>          .
06.05.2020  20:07    <DIR>          ..
06.05.2020  20:07    <DIR>          folder1
14.06.2021  14:17    <DIR>          folder2
17.08.2021  11:15    <DIR>          error
               0 File(s)              0 bytes
               5 Dir(s)  153.174.507.520 bytes free
batch           on        
confirm         off       
include         |archive;archiv;processed
reconnecttime   10        
Searching for host...
Connecting to host...
Cannot open file "C:\Program Files (x86)\WinSCP\WinSCP.ini". The process cannot access the file because it is being used by another process
No session.
No session.
No session.
transfer        ascii     
\\localdirectory
No session.
No session.
No session.
No session.
No session.
No session.


***                                                       
END FTP Operation   - 20.08.2021  8:50:03                 
**************                                              
    
----------------------------------------------------------       
END SCRIPT - 20.08.2021  8:50:03                                            
----------------------------------------------------------  
                                                                            
                                                                            
Build step 'Execute Windows batch command' marked build as failure
Sending e-mails to:
Finished: FAILURE

It seems to appear while the script is trying to opening a new FTP-session:

 C:\"Program Files (x86)"\WinSCP\winSCP.com /console /script="%FTPScriptDir%%FTPScriptFile%" 

The batch script calls actually a .ftp file to open a new session:

#option config
option batch on
option confirm off
option exclude "archive;archiv;processed"
option reconnecttime 10

##
# open SFTP session
# WinSCP Session (local installation => C:\"Program Files (x86)"\WinSCP\)
##        

open sftp:/// -hostkey="" -rawsettings ProxyPort=0

The Jenkins job is configured to run one batch command after another:

Jenkind Batch Command

Both batch script files do the same thing. There is just a difference regarding the local/remote directories path.

Could you help me to find a way to avoid theses casual errors? Thanks!


Solution

  • Your WinSCP script relies on a shared INI configuration file, what is against the guidelines. It's likely that you run multiple WinSCP instances/transfers in parallel and they fight each other for write access to the file.

    There are two solutions:

    • Quick and dirty solution is to mark the INI file as read-only, so that WinSCP will not try to update it when finishing.

    • A proper solution is to avoid using shared configuration. Instead use /ini=nul commandline switch to make WinSCP use the default configuration. Though that might need some tweaks to the script, in case it really relies on some configuration in the INI file.

    See https://winscp.net/eng/docs/scripting#configuration