OK, so here's the scenario.
CruiseControl.NET (Version : 1.5.7256.1) runs a project to rebuild a database. The CruiseControl.NET windows service is running as a windows user we created for CC.NET. There are several tasks in the project but they all run fine. However, one of the tasks that runs is a console based utility I wrote in C# that generates a batch file and then runs it via an instance of the .NET Process class.
The batch file is generated properly and the process runs fine in C#. However the batch file that runs, runs the sqlcmd command line utility for SQLServer (Version 2005 SP3). sqlcmd runs as expected, but one of the options for sqlcmd never gets applied... -I (turning quoted identifiers on).
e.g.
sqlcmd -U %1 -P %2 -S %3 -d %4 -i someScript.sql -k -b -I >> %LogFileName%
The odd thing is if I run this from a command prompt on the continuous integration server, it runs with -I being applied.
I'm very confused. I'd understand if the sqlcmd failed because the user executing the process didn't have enough privileges and I'd understand if something went wrong on the SQLServer side if the SQLServer authentication failed, but for it to work but not apply an option to sqlcmd is mystifying.
Could it be it's because -I is the last parameter? Perhaps there's a missing space there or something.. have you tried switching parameter order?
(also, why are you running a C# console app that create a batch file and the invokes it? Can't cc.net run this file itself?)
Related
All the solutions I can find on this topic are very old and none of them appear to answer my question...
I am trying to create a windows service that can self update (or auto update by some external trigger). In the past, I had created a windows service that was installed with InstallShield and we were able to update auto update the service in a hacky way by making the service write a batch script to the local machine and then run the batch script, which would stop the service, overwrite the service executable and other files with the new ones, and restart the service. This surprisingly worked.
However, I have updated the service to use InstallUtil.exe and this auto update script no longer works... I assume it's something to do with the way InstallShield handles the service install vs how InstallUtil does it... but I can only make guesses as I don't fully understand what each is doing to the registry.
Since I can't just overwrite the files and restart the service with the InstallUtil method, I thought I'd write a batch script that runs sc.exe to stop the service, uninstall it entirely, write the new files, install the new service files, and then start it... unfortunately, I can't seem to get sc.exe to run from a windows service automatically because it requires admin permissions... I tried to force it to self-elevate to admin using this snippet, but it doesn't appear to work as a service (it works fine if I run it from command line not as a service)
if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit /b)
Does anyone know how I can cause a windows service to self update? I can look into updating to a .NET Core Worker service if there is some method of self update in .NET Core that I'm unaware of... Any ideas are much appreciated... it really shouldn't be this hard to accomplish...
For reference, here is the batch script I am currently using (ignore odd variables and such as I am dynamically replacing some of them, it works great when launched manually, just doesn't work when the service tries to run it):
#echo off
setlocal enableextensions enabledelayedexpansion
::make sure to run whole script as admin (this restarts scripts as admin if not already in admin mode)
if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit /b)
pushd %networkDirectory%
::stop running service
for /F "tokens=3 delims=: " %%H in ('sc query %serviceName% ^| findstr " STATE"') do (
if /I "%%H" NEQ "STOPPED" (
net stop %serviceName%
if errorlevel 1 goto :stop
)
::delete existing service after stopping
sc delete %serviceName%
)
:: install updated service files
set "releaseDir=%networkDirectory%\Release"
set "programFilesCopyDir=%ProgramFiles%\{_companyDirectory}\%serviceName%\Release"
:: copy service Release dir to local system program files
xcopy "%releaseDir%" "%programFilesCopyDir%" /S /Y /Q
::execute the install
pushd "%programFilesCopyDir%"
CALL %serviceName%.exe --install
::start service
sc start %serviceName%
For anyone else trying to accomplish this that stumbles on this... I ended up finding a solution. I use the same script posted in my question above, but I wrote code to set up a scheduled task with Windows Task Scheduler. The scheduled task runs the above script as a one time scheduled task. This works like a charm.
I used this NuGet package to write the Task Scheduler code I needed:
https://www.nuget.org/packages/TaskScheduler/2.8.20?_src=template
I have a c# code that installs rabbitmq 3.7.4, erlang 20.2 on windows server 2012 R2 and I need to know when the application (not the service) has started. After running rabbitmq-service install and rabbitmq-service start I'm looking for a command line that will indicate that the application is running. I'm aware of the wait pid_file, wait --pid pid command but can't locate the pid file on my machine. The documantaion says:
This command will wait for the RabbitMQ application to start at the node. It will wait for the pid file to be created if pidfile is specified
specified where?
rabbitmq-echopid.bat returns:
The system cannot find the path specified.
On Windows, RabbitMQ does not create a PID file by default, so you have to discover the PID and then pass it as an argument: rabbitmqctl.bat wait -P PID
To discover the PID, you can run the following using the name of your RabbitMQ node:
.\rabbitmq-echopid.bat rabbit#my-hostname
At this time, there is a bug where The system cannot find... will be echoed before the PID is echoed. I filed this bug and will have a fix in soon, but in the meantime you can edit the rabbitmq-echopid.bat script to change !TDP0! to %TDP0%.
You can also use any other Windows tool to find the PID of the erl.exe process running RabbitMQ - see the script for an example of wmic.exe, or you could use tasklist, or Powershell, etc.
On windows, you can run the following batch script:
START /B rabbitmq-server
START /wait cmd /c "rabbitmq-echopid.bat -n rabbit#`hostname` > rabbitmq_pid.txt"
set /p PID=<rabbitmq_pid.txt
echo %PID%
del rabbitmq_pid.txt
cmd /c "rabbitmqctl wait -P %PID%"
Note that for the rabbitmq-echopid command to work, you have to add an -n before the nodename.
Moreover, in the above batch script, the nodename is dynamically generated by combining "rabbit#" withe the hostname windows command (inside backticks).
I have a Raspberry Pi 2 with Raspbian. I am trying to start a Mono program called StartBrowser.exe automatically when the system boots. I added the following line to the /etc/rc.local file:
sudo mono /home/pi/Desktop/StartBrowser.exe
I also tried adding:
sudo /home/pi/Desktop/StartBrowser.exe
The program does not start on boot. When I run the same commands in the terminal the expected program starts. What do I need to do to start "StartBrowser.exe" on system boot ?
There are few steps to accomplish this:
Make sure your application can be run as Windows Service (check the documentation)
On RPi install mono-service with sudo apt-get install mono-4.0-service
Now you can add, at the end of the rc.local file, just before exit 0 line this call:
/usr/bin/mono-service -p:/directory/of/mono/app /directory/of/mono/app/app.exe
-p switch is mandatory, otherwise no additional DLLs will be loaded.
You can use "mono-service" to run programs in the background.
You can run your compiled code like so:
mono-service /home/pi/Desktop/StartBrowser.exe
By default, this creates a lockfile in /tmp. You can change this by using the -l: option.
In this way your service is running in the background
I created Windows Service problem which invokes psexec and run .exe in the remote Server. Every thing work fine, when i try to do run in debug mode of invoking pscommand code # console application, when i try to run pscommand from Windows service, It hangs out without any reason. Is there any reason or alternative.. psexesvc doesnot start on remote server
Here is the pscommand i used
psexec -accepteula \\XYZServer -u -p -h \ShareCompName\Component.exe.
There is no error message.
I found reason for hang state, basically psexec command is calling .exe application, where that exe aplication is performing Admin activity. When PSEXEC command is having -h parameter , then it asking psexec command to execute run as administrator. Since target server where psexec gets executed is having UAC enabling, It is waiting for user input , until user click Yes or NO. application remain in hang state. SO i added extra parameter -i along with -h which automatically takes care of above explained situation.
psexec -accepteula \XYZServer -u -p -h -i \ShareCompName\Component.exe.
I have a little .exe written in c# .net that I want to run on the server every 24 hours. So naturally I would just use the Windows Task Schedular rather then doing the math myself. I have created the program, but I would like to create an installer that just set everything up. Is there a way to do this with like the Visual Studio set-up projects? If not is there like a powershell / batch script that could be used to run after installation?
Bottom Line: Automate the creation of the task.
You can use a powershell script or batch file to execute schtasks which is a command line interface to the task scheduler.
Then you simply need to run the script in order to setup the scheduled task.
There is also a managed wrapper that allows you to create schedules tasks in C#, if you would rather go that way.
I know this is an old question, but I figure this may help someone else:
You can use the following to run in cmd.exe
FOR /F %1 IN ("path to text file containing list of servers") do psexec.exe \\%1 -u
"username to execute schtasks under" -p "password" schtasks /Create /S %1
/RU "username that will run the task" /RP "password" /XML "xml file of
the task to install" /TN "name of the task"
This will loop through the list of servers in a text file (1 server per line) and use psexec to call schtasks on each server and install your task.