Convert batch script to sh - batch-file

I am new with ubuntu, please help me convert these batch codes to sh, I am trying to run the files mentioned below and run as a service after installing. I already searched for answers but failed to find one.
#echo off
echo Installing services
.\Central\GunBoundBroker3 -install
.\Server8360\GunBoundServ3 -install
.\Server8361\GunBoundServ3 -install
.\BuddyCenter\BuddyCenter2 -install
.\BuddyServ\BuddyServ2 -install
#echo off
echo Initiating Services
net start "GunBoundXPBroker[8372]"
net start "GunBoundXPServ[8360]"
net start "GunBoundXPServ[8361]"
net start "Buddy Central Service 2"
net start "Buddy Service 2"
pause

Firstly you do not need #echo off; this is dos specific.
Secondly, create your script with the first line saying:
#!/bin/bash
Thirdly, you will need to use forward slashes (/) rather than backslashes (\) in your filenames
Fourthly there isn't really an analogue to net start in Linux - This question on Ask Ubuntu for details of how to add custom init scripts if this is what you're after
I hope this helps - unfortunately without you ellaborating on what these files you're running do and where they're from, how you've ported them from Windows to Linux, etc I cannot be more specific

Related

How do I stop my batch file and pushd from referencing the unc when opening cmd.exe?

I'm trying to run a batch file from a local Windows server that calls on computers in my domain to pull from the shared folder and run an exe. I'm not sure if my script is trying to do too much or too little.
So I run the below batch locally
X:\pstools\psexec.exe \\Computer -d -u DOMAIN\user -p password -i \\SERVER\test\testfile.bat
and testfile.bat:
#echo off
pushd \\SERVER\test\
call program.exe
popd
When I run the script, psexec runs and I get a confirmation that testfile.bat was started on target computer. On the targeted computer nothing happens. If I navigate to the share on the targeted computer and run testfile.bat, I get "CMD.EXE was not started with the above path as the current directory.UNC paths are not supported. Defaulting to Windows directory." From there the computer runs the called .exe with no issues.
If I target this towards another server in my domain it executes perfectly, but not on domain computers. I thought maybe a GPO issue, but I can't find a solution.
Thanks for any knowledge or help provided!
Thanks for all the tips everyone! This is how I ended up getting it working for anyone who might have the same issue.
Script on Server:
x:\pstools\psexec.exe \\Computer(or text file with computers listed) -d -s cmd /c (batchfile.bat)
Similiar to what I was trying before, but to ensure you run the command line as System on the remote PC you have to specify "-s cmd". The /c copies the batch to the remote system to run locally. You can include a "-f" to overwrite if you've already copied it previously. I just put the batchfile in the pstools folder.
Batchfile.bat:
pushd \\networkdrive
call (.bat/.cmd/.exe/etc.)
popd
I disabled the firewall for testing, but I believe you have to open TCP-445/UDP-137 for PSEXEC. Otherwise, that's it. Super simple solution.

PsExec and invalid handles

I am trying to use a windows batch script that uses PsExec to execute commands on a remote machine. Periodically it has "invalid handle" and the script then fails.
The script has not altered or indeed either machine.
Does anybody know why this happens as sometimes the scripts runs without a hitch.
Alternatively does anybody know how to run a script on a machine as the local user for that machine with a more reliable technology.
PS Sometimes the first PsExec works and the others fail.
EDIT
The script is just on line (apart from setting the appropriate variables)
PsExec %HOSTNAME% -I -u %USERNAME% -p %PASSWORD% CMD /C RMDIR /S /Q e:\SomeDir
This sometimes works but sometimes fails with "invalid handle"
You need to debug the situation.
You have a script, then something (what is Jenkins?) launch it on a remote PC, sometime it works, sometime it fail.
Is it deterministic?
When it fail does it always fail?
How does it fail?
You need to acquire better knowledge of how/when the script fail.
Here is what I would do to gather better understanding of these fails.
Can you run the script multiple time?
From the comments it seem that you run the script every hours, can you run it 3/4/5 time in a row, for each hours?
This will help you to determine how it fail: if you run it 5 time, does it works every time? it it fail, does it fail 5 times in a row?
Can you try to use different script?
You can create some more similar, but simpler, scripts.
So you can try your script with the RMDIR, then another script with a simple DIR command (just to se if the script launching/connection mechanism works) then another script with a simple ECHO command (so it doesent need to access any files/folder)
Run debug scripts on the local PC
Then, you can simultaneously run other scripts that run on the LOCAL PC (not the remote one where you need to execute the RMDIR) that try to access the remote PC, with a PING, or by copying a file from/to a network share...
Sniff the network
You can even set up a Wireshark instance that log all the packet sent between the 2 PC, this can be helpful to analyse/exclude networking issue.
You clearly need to track/log everything.
With this kind of information maybe you/we can have a better understanding of where the issue is.
=====================================
UPDATE 1 - Record some log
=====================================
Maybe you can try to use the following modified scripts to have some log files.
These script will create 2 log files, one on the remote PC (containing the message of the remotely executed command) and one on the local PC (containing any message from PsExec)
(you'll need to tweak the path where the log file are saved)
psexec %HOSTNAME% -I -u %USERNAME% -p %PASSWORD% CMD /C "RMDIR /S /Q e:\SomeDir >>c:\RemoteComputer.log 2>&1" >>c:\LocalComputer.log 2>&1
or the following one without the /I
Are you sure you need the /I parameters for CMD? On my Pc it doesn't works if I use the /I parameters...
psexec %HOSTNAME% -u %USERNAME% -p %PASSWORD% CMD /C "RMDIR /S /Q e:\SomeDir >>c:\RemoteComputer.log 2>&1" >>c:\LocalComputer.log 2>&1
After some testing on my PCs, I've seen that PsExec install a service on the remote PC to run the command remotely. (It's called PsExecSvc.exe, installed in c:\windows\ on the WinXP PC I'm using for this test)
The remote installation/uninstallation of this temporary service for the command execution can surely be one of the possible "failure point" that generate the error.
If this is the case, then you should be able to track this down by looking at the LocalComputer.log, that will contain the message/error from PsExec.
As stated in my previous advice, I would also try to schedule simpler script like
psexec %HOSTNAME% -u %USERNAME% -p %PASSWORD% CMD /C "dir c:\ >>c:\RemoteComputerDir.log 2>&1" >>c:\LocalComputerDir.log 2>&1
and
psexec %HOSTNAME% -u %USERNAME% -p %PASSWORD% CMD /C "echo SuperEchoTest >>c:\RemoteComputerEcho.log 2>&1" >>c:\LocalComputerEcho.log 2>&1
===================================
UPDATE 2 - Try to use WMI
===================================
You can try to run the remote command by using WMI
wmic /node:%HOSTNAME% /user:%USERNAME% /password:%PASSWORD% process call create "CMD /C RMDIR /S /Q e:\SomeDir"
When you use WMI you need to be sure that windows firewall is not blocking your command. (when I tried to run a remote command with WMIC the windows firewall notification popped up on my Win 7 PC)
(I've the instruction to use WMIC here)
Yes, there is a more reliable technology for executing commands on a remote machine and is called powershell. For example, you can run :
test-connection -computername server01, server02, server12
pings from local computer to several remote computers.
Another very useful command is:
invoke-command -filepath c:\scripts\test.ps1 -computerName Server01
runs the Test.ps1 script on the Server01 computer.
A tutorial gives several examples on how to Run PowerShell Commands on Remote Computers.
A different technology can be found mimicking the Linux world, and using ssh. It's very common with clusters and I have personally used it with Windows Server 2008 R2, so I don't expect any difference on windows 7.
This task is commonly performed with ssh and password-less public key authentication. With it, the only needed information is the IP of the remote server and the public key of the client, stored on the server: only the client with the corresponding private key can connect to it (the keys must be created with ssh-keygen, on the client. The public key is copied to the server)
The server must have the TCP port 22 accessible from outside, in case there are firewalls, NATs,...
In my case I used the ssh server included in Windows SUA, but I suggest you forget them (they are deprecated, and quite cumbersome actually) and give a try to the OpenSSH cygwin server, sshd - even if not officially Microsoft, there is a large community supporting it at least - and occasionally I have used it reliably.
The client ssh command is included in SUA, in cygwin, or you can use putty if you want a lightweight solution on the client (not that cygwin is heavy - just the burden of having a sort of linux emulation that's not needed)
Giving a search for example I have found this post, explaining well the needed steps.

How to start two servers (mysqld and thin) in one batch file?

I want to create a batch file to start MySQL server (mysqld) and Rails Thin server (rails s thin). But only MySQL starts, rails s thin doesn't:
#ECHO OFF
cd C:\path\to\app
CMD /C "mysqld --console"
REM exit <-- with or without it
REM cd C:\path\to\app <-- with or without it
CMD /C "rails s thin"
I also tried to create three files for this. The main one runs other two. Doesn't help either, only MySQL runs. And I need to start MySQL before Rails. Rails needs running MySQL. How to write such a batch?
UPD:
How to start 2 programs simultaniously in windows command prompt
use start
e.g.
start notepad.exe
start calc.exe
if you want to run one after another with some tests, you may need to write your own program.

Execute windows batch command from Jenkins fails but runs fine in cmd.exe

I am trying to run this command in jenkins after a MSbuild
xcopy "C:\Program Files (x86)\Jenkins\workspace\trunk\Projects\results\results\obj\Debug\Package\PackageTmp" "Y:\Extraction_Zone\Jenkins\" /E
Y: is a mapped network drive. This runs fine in cmd.exe but when trying to run it in Jenkins, I am getting the error Invalid drive specification.
Here is the output from jenkins:
Time Elapsed 00:00:04.03
[trunk] $ cmd /c call C:\Windows\TEMP\hudson3389873107474371072.bat
C:\Program Files (x86)\Jenkins\workspace\trunk>xcopy "C:\Program Files (x86)\Jenkins\workspace\trunk\Projects\results\results\obj\Debug\Package\PackageTmp" "Y:\Extraction_Zone\Jenkins\" /E
Invalid drive specification
0 File(s) copied
C:\Program Files (x86)\Jenkins\workspace\trunk>exit 4
Build step 'Execute Windows batch command' marked build as failure
Finished: FAILURE
Any help would be appreciated.
I too had a similar issue once. Try granting the Jenkins service "Logon as This account" right under services.msc and make sure the account you type there is the same as the one you use for running cmd.exe.
These commands based on Java JAR files worked for me:
cmd
net use x: \\
xcopy "dist\" x:\ /Y
And that's it! I spent lot of time figure out this issue and nothing worked until I wrote CMD and NET USE!
Neither I didn't need to change permission on jenkins service nor use runas command.
But I must mention that everyone had read and write access to the network drive.
I had the same issue with my Windows Task running a batch file (I know it is not exactly same) where I tried to copy file to network location i.e. shared drive. I used the UNC path and mapped drive as well but the error was same. For me it was error number 4 - MS DOS error code.
The solution was to use net use command! Hope that it helps.
Easy fix for most things.
Make a batch command with what your trying to run, filename.bat with the command prompt text inside.
Make a normal windows shortcut for the batch command, edit the shortcuts advanced properties and check the "Run as admin" (tricky tricky).
Now run the filename.lnk shortcut from jenkins command line call, this will get you around all the jazz.
:)
The solution of adarshr (i.e., modifying the log on credentials of the service) has worked for me for a part of the problem: in my case, this allowed me to successfully check out a mercurial repository (using ssh protocol), which I could not do when using 'Local System account'.
However, I still have different behavior between running a command-line script or running the same script from a jenkins 'execute shell' script in the build section. In my case, I compile a Python extension. In Jenkins, I cannot import the extension (I don't see any error, but the execution simply stops, so I suspect it crashes).
If I uninstall the service and run the slave agent as a Java Web Start, I do get the same behavoir. It is a temporary fix for me, but it means that when I reboot the windows build machine, I have to manually re-start the Java Web Start application.
So -at least in my case- it is clear that this is a credential problem.
Credentials usage documentation: https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+CLI
I've solved my issue with the CIFS plugin.
Faced similar issue and found two ways to solve.
Type 1:
Tell Jenkins about mapped drive.
1.Goto -> Manage Jenkins -> Script Console (Groovy Script).
2.Run below command
def mapdrive = "net use Y: \\\\copy_nework_address"
mapdrive.execute();
println "net use".execute().getText()
Type:2
1.Goto -> cmd -> run "net use" to know network address
xcopy "C:\Program Files (x86)\Jenkins\workspace\trunk\Projects\results\results\obj\Debug\Package\PackageTmp" "Copy_Network_Address\Extraction_Zone\Jenkins\" /E
Conclusion:- I prefer 2nd types as after every restart i should run Groovy Script.

batch file to wait for a installation to complete before moving to the next line

I have a batch file that will detect if the user has the .Net framework installed by going to the directory and checking if the CONFIG directory exists.
If the directory doesn't exist then the user doesn't have the .Net framework installed. The batch file will then continue on to install the .Net framework. However, there is a problem as the .Net frameworks need to be installed before running the setup to install my dialer. So I have put a PAUSE statement so the user will press any button to continue after the framework has been installed.
However, our client doesn't like this as some of their customers don't understand and they press a key before the framework has finished installing. This then causes the setup to fail as the framework haven't been installed first.
I am using the PAUSE that will wait for the user input. However, is there a way that the batch will wait until the framework is finished automatically instead of using the PAUSE command?
Many thanks for any suggestions,
#ECHO OFF
REM Copy the configuration file
copy config.xml "%AppData%\DataLinks.xml"
REM Search for the CONFIG file, if this doesn't exit then the user doesn't have the .Net framework 2.0
SET FileName=%windir%\Microsoft.NET\Framework\v2.0.50727\CONFIG
IF EXIST %FileName% GOTO INSTALL_DIALER
ECHO.You currently do not have the Microsoft(c) .NET Framework 2.0 installed.
ECHO.This is required by the setup program for MyApplication.
ECHO.
ECHO.The Microsoft(c) .NET Framework 2.0 will now be installed on you system.
ECHO.After completion setup will continue to install MyApplication on your system.
ECHO.
REM Install the .Net framework and wait for the user to input before install the dialer
PAUSE
ECHO Installing... Please wait...
SET FileName =
Start .\NetFx20SP2_x86.exe
ECHO Once the .Net Framework has completed. Press any key to continue to install the dialer.
PAUSE
Start .\setup.exe
ECHO ON
EXIT
REM .Net framework has been skipped contine to install the dialer.
:INSTALL_DIALER
ECHO *** Skiped Dotnet Framework 2.0.50727 ***
ECHO Installing... Please wait...
SET FileName=
Start .\setup.exe
ECHO ON
EXIT
you can use
START /WAIT NetFx20SP2_x86
too.
remember that
REM comment
is equal to
::comment
and that using .\ is unnecessary, and file extensions are too, unless there are dirs/files with conflicting names.
You also do not need cleaning the "filename" variable twice (the "=" pointing nothing twice) and
ECHO.something
is equal
ECHO something
only except for blank lines
you could also use
START /WAIT NetFx20SP2_x86.exe
the slash is to indicate that it is an option to the start command, not the target file. to see more of these options, look here (i would refer to it as a 'forward slash')
Just remove the START line from the call, like so:
.\NetFx20SP2_x86.exe
Start .\setup.exe
This will make the installation blocking, as in the batch file will stop processing until the NetFx20SP2_x86.exe program terminates.
Ok I optimized your script, sort of:
#echo off
rem Copy the configuration file
copy config.xml "%AppData%\DataLinks.xml"
rem Search for the CONFIG file, if this doesn't exist then the user doesn't have the .Net framework 2.0
if not exist "%windir%\Microsoft.NET\Framework\v2.0.50727\CONFIG" (
echo You currently do not have the Microsoft(c) .NET Framework 2.0 installed.
echo This is required by the setup program for MyApplication.
echo.
echo The Microsoft(c) .NET Framework 2.0 will now be installed on you system.
echo After completion setup will continue to install MyApplication on your system.
echo.
Start /w .\NetFx20SP2_x86.exe
)
Start /w .\setup.exe
1: As you only use the CONFIG file for test purpose once, using a variable for it is useless. moreover, the "=" sign must be stuck to the variablename, otherwise you will create a variable with a space in it. "set filename =" and "set filename=" are two different things. Yes, a variable name can contaion multiple words and spaces, but one need to be extremely cautious about it, it can be hard to handle.
2: I would not recomment using '::' as a comment, sorry Camilo, because it does not work well within parenthesis. It would here, but if you take this track for all your batches, you might get into a problem later and you will wonder why your code is broken if you don't know this.
3: You don't need to end a script with exit, nor resetting to echo on, when a script reaches the end, it does reset the echo state and exit by itself.
Hope this helps.

Resources