Change Cygwin’s user

I’ve tried looking for an option like «login» but didn’t work for me. As a workaround I use this:

#using RUNAS
runas /user:<HOSTNAME>\<USER> D:\cygwin-64\Cygwin.bat

#using PSEXEC
psexec \\computername -u <HOSTNAME>\<USERNAME> -p <password> "D:\cygwin-64\Cygwin.bat"

Execute commands or scripts silently in Cygwin

If you want to run a command or an script in Cygwin silently use «run.exe». Do not forget to scape commas.

# Whit the actual user
D:/cygwin-64/bin/run.exe /usr/bin/bash -lic \"/home/user/\"
# Whit a different user
psexec -accepteula \\<HOSTNAME> -u <HOSTNAME>\<USERNAME> -p <password> D:/cygwin-64/bin/run.exe /usr/bin/bash -lic \"/home/user/\"

Execute a Cygwin command /script silently using Jenkins

Just create a new project in Jenkins with the option «Run Windows batch» and add:

D:/cygwin-64/bin/run.exe /usr/bin/bash -lic \"/home/user/\"

Execute a Cygwin command/script silently as a different user using Jenkins

For this one you’ll need to:

  • Create a file.bat
  • Optionally if you want to run a shell script create a .sh file
  • A Windows Task
  • A Jenkins Project

In this example I’m running a .bat which executes silently a shell script in cygwin as other user in Jenkins to copy all /etc/hosts files from many servers (don’t ask me why I had to do this horrible thing).

Bat file

@echo off
psexec -accepteula \\<HOSTNAME> -u <HOSTNAME>\<USERNAME>-p <password> D:/cygwin-64/bin/run.exe /usr/bin/bash -lic \"/home/user/dir/\"

Shell file

while read g; do
    ssh $g -l <USERNAME> -i /home/user/dir/pass_$g cat "/etc/hosts" >> /cygdrive/g/dir/outputfiles/hosts_$g
done </home/user/dir/hosts_ip.txt

The windows task I’ve created is called «get_hosts_on_demand»

And finally in Jenkins just a job calling the windows task:

schtasks /Run /TN "get_hosts_on_demand"

*The Job will return a false positive all the time.