I need to stop and start a service when certain parameters are met in a .txt file.
When my app can't connect to a database in our test environment (regular occurence) it generates the following file type in the server folder (file date changes with each occurence)
SelfTest-20141126181000-RED.txt
Within this file is the phrase
RED: Server failed to start: Unable to open database: Unable to connect to database
Every time this is generated, along with this phrase I need to stop and start a specific service.
I have never used findstr command but I think it could be useful here. Textfile needs wildcarding obviously, and i would like a results file generating with the data and time service was stopped and started.
Would the following work?
findstr "RED: Server failed to start: Unable to open database: Unable to connect to databaseā *RED.txt
if %errorlevel%==0 (
sc stop "my service"
sc start "my service"
echo %DATE% %TIME% database >> results.txt
)
Plan is to then have this running through scheduled tasks every 30mins or so
Well, obviously once you have one such file, your script will continue to find it and restart your service every 30 minutes. You'd need to delete or move the RED file.
The second problem is that the sc start command may fail while the service is still being stopped - sc stop returns as soon as the request is delivered, and does not wait. You'd need a repeated sc query "ServiceName" | find "RUNNING" check. And while the service is still running, give it some more time by running ping -n 2 127.0.0.1 > nul. Busy-waiting isn't nice, but there's no clean sleep command so ping is the common hack for that.
Related
I'm working on a Django REST framework API that is built in a docker image and launched/managed with docker-compose. When I launch my app I get the real time log of the Django application in the terminal. I accidentally closed the terminal and I want to re-establish a real time log output in the terminal without restarting my containers.
I tried docker-compose logs which will print a tail of the log but does not re-establish a real time output. I would have to rerun this every time I wanted to see new log information.
I think if you add the --follow flag to your command, you'll get the desired result. So:
docker-compose logs --follow ...
I'm trying to execute a simple Test Connection test in Robotframework, between two IP addresses in the same network. What I'm trying to achieve is:
On one side
Execute Command echo Hello | nc -l 51111
to catch the "Hello" message on the other:
Execute Command 'nc ip 51111'
, using the standard netcat tool.
For this, I run the Test Script.robot:
Library Process
Library SSHLibrary
Suite Setup Open Connection And Log In
Suite Teardown Close All Connections
*** Variables ***
${HOST} ip.my.dev.vm
${USERNAME} user
${PASSWORD} password
${HOST_PDG} ip.server1
${HOST_IDPF} ip.server2
*** Test Cases ***
Step 1 Connect Server1 and send Hello to space
Open Connection And Log In Server1
${output}= Execute Command 'echo Hello | nc -l 51111'
Should Not Be Equal ${output} Hello
Step 2 Connect to Server2 and retrieve Hello
Open Connection And Log In Server2
${output}= Execute Command 'nc ip.server2 51111'
Should Be Equal ${output} Hello
*** Keywords ***
Open Connection And Log In
Open Connection ${HOST}
Login ${USERNAME} ${PASSWORD}
Open Connection And Log In Server1
Open Connection ${HOST_PDG}
Login ${USERNAME} ${PASSWORD}
Open Connection And Log In Server2
Open Connection ${HOST_IDPF}
Login ${USERNAME} ${PASSWORD}
This script stops after the first passed test step, and from the Server2 I never able to catch this Hello message.
The same test over command line with the nc tool works perfectly.
ssh Server1
echo Hello | nc -l 51111
ssh Server2
nc ip.server2 51111
(see the Hello message)
What is the problem to do it over the ROBOT Framework?
Thank you in advance!
The issue you're facing is because the robotframework execution is serial, while your aim implies parallelism - after running the netcat server on Server1, in parallel you want to connect to it from Server2.
This is what you're doing when you did it manually, but this is not what happened in the RF cases: in the framework, you've ran test 1, it did its steps, and finished (with some pass/fail status, but that's not important now). When it was done, the service was not running; so when in test 2 you tried to connect to it - it rightfully failed.
Here's what you can do - make the two steps (bringing up the service, and connecting to it) be executed in parallel. The easiest way is for that to be in the same case; and for the service to be running in the background you have to use not the Execute Command - which waits for its target to finish, but Start Command which starts it in the background immediately returns.
Its usage is a little bit different, in terms of getting the output and communicating with it, you can read how in the doc.
...and I found that...
Without changing the original script much, if the quotes around the both command are deleted, and Start Command is used instead of Execute Command, at the listener side (as Todor suggested, thank you!), the script will works as it is, giving the right results.
I've got a elasticbeanstalk environment that needs to run a powershell script and restart before the application is deployed. According to the documentation this is supported as per the documentation
If the system requires a reboot after the command completes, the system reboots after the specified number of seconds elapses. If the system reboots as a result of a command, Elastic Beanstalk will recover to the point after the command in the configuration file. The default value is 60 seconds. You can also specify forever, but the system must reboot before you can run another command.
However when I add a reboot command to a ebextensions .config file I get the following exception from elasticbeanstalk
Error occurred during build: [Errno 4] Interrupted function call
The logs on the server after it has rebooted show that the command was executed so I assume the error is caused by a restart during the app deploy stage.
If I remove the restart command, deploy, wait for it to be ready then trigger a restart manually it works fine. But this is obviously not acceptable.
I've looked into the deployment hooks file system approach but that doesn't work either, and seems unessesary given it sounds like it should support this requirement out of the box.
Does anybody have any ideas?
We've had the same issue. We needed to disable SSL and TLS < 1.2, which requires registry changes and a reboot. Our workaround is to do the reboot in the container_commands section with a wait of forever. This seems to properly reboot and then trigger success in the deployment. However, it never actually does any of the steps after the reboot, which includes the built-in deployment of the code from the staging location to the actual final file destination (inetpub/wwwroot most likely). To get around this, have a step just before the reboot to copy the files from the local staging directory to the web root yourself.
We also needed to set a registry value and reboot. Our solution was to put the script in the command section and set waitAfterCompletion to foreve. There is a restart-computer --Force in our powershell script to cause the reboot.
disable_secure_time_seeding:
command: powershell.exe -ExecutionPolicy Bypass -File "C:\\scripts\\DisableSecureTimeSeeding.ps1" #This will cause a reboot
waitAfterCompletion: forever
I'm working on a cli script for JBoss EAP 6 (just a bunch of commands in a .bat file).
Now I'm running into issues with commands that rely on each other, where the former sets the server to a "reload-required" state.
For example:
first command: remove default-datasource ExampleDS. Outcome: success, server in state "reload required"
second command: remove h2-driver (required by default datasource). Outcome: failed, since server has not been restarted yet.
I've tried to place a reload command in between, but it seems as if the server is not completely up when the second command gets executed. Outcome still is failed, although if I try it a few seconds later, it works.
/subsystem=datasources/data-source=ExampleDS:remove
reload
/subsystem=datasources/jdbc-driver=h2:remove
Any suggestions how to make the CLI wait until JBoss is completely up again?
Try batching those commands together and doing a reload after you run the batch. For example:
batch
/subsystem=datasources/data-source=ExampleDS:remove
/subsystem=datasources/jdbc-driver=h2:remove
:reload
run-batch
Another option is to run the server to start the server in admin-only mode while configuring it. You should need the reload command in that case, but if you do make sure you use :reload(admin-only=true) until you're done configuring the server.
%JBOSS_HOME%\bin\standalone.bat --admin-only
rem Wait until server is started, then execute the CLI commands
%JBOSS_HOME%\bin\jboss-cli.bat -c --commands="/subsystem=datasources/data-source=ExampleDS:remove,/subsystem=datasources/jdbc-driver=h2:remove"
I am using Mirth connect which uses install4j to launch the program.
I am using the mcservice program and would like to get the pid of the launched application so that I can monitor it. How do I do this? Right now the service only has the standard start, status, etc commands.
Back in 2011 there seemed to be some indication that pid monitoring would be coming soon: http://www.mirthcorp.com/community/forums/showthread.php?t=5509
If you need get the the number of the process pid, you can do it by the terminal command:
$ pgrep mcservice
Also, you can store the pid number into a file and run the next command:
$ kill -9 `cat /path/to/file.pid`
In this case, you choosing the best solution for your issue. If you need something more complex you can view this link: Simple Process Checker To Find Out If A Service Is Running or Not