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/file.sh\" # Whit a different user psexec -accepteula \\<HOSTNAME> -u <HOSTNAME>\<USERNAME> -p <password> D:/cygwin-64/bin/run.exe /usr/bin/bash -lic \"/home/user/file.sh\"
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/file.sh\"
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/file.sh\"
Shell file
#!/usr/bin/bash 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.
how to display shell script execution details on Jenkins console when running the shell script in cygwin by calling in jenkins pipeline as below
bat’call D:\\cygwin64\\bin\\mintty.exe /usr/bin/bash -lic \»/home/confmgt/dsimport.sh \» ‘
The output of any script displayed inside Jenkins normally depend of two things:
1) The type of Job created in Jenkins (you can be sure that using FreeStyle Pipelines is highly probable to display the output of any executed script on the job’s output after an execution) other types of jobs have different management for the outputs of job executions.
2) Verify that the script is writting to the standard output.
Cheers.
M.