How to automatically run command line program several times? - command-line

I have a c# program, integrated with a command line program. I want to run the command line program twice(start, finish, start again, finish again). Now I use a timer to set a special time period for every run, for example, give first run 10 seconds, no matter it is finished or not, after 10 seconds, the program starts the second run.
I want the second run can run automatically after the fist run finshed, How to do it? How to detect the first run is finished, and then take a trigger to start the second run?

Assume you run the command line as a process, see this answer to check if the process has finished:
Find out if a process finished running
if (process.WaitForExit(timeout))
{
// user exited
} else {
// timeout (perhaps process.Kill();)
}

In a command line you can launch this command:
start "" /w will execute the command and wait until it is finished before proceeding.
for %a in (1 2) do start "" /w "programA.exe"

Related

Mac Terminal to run a bash script that starts a swift program & restarts every hour

I am looking for some support on creating some way of running a swift command in terminal to run a program and then stop it after 1 hour then restart.
Example of manual process:
Open Termain.
cd my app
swift run my program --with-parameters
ctrl+c (after 1 hours)
Restart with step 3
I am sure there must be some way using a bash script maybe to start the program by command, kill it after 60min and restart it with a continuous loop like that.
Thanks :-)
You can set up a cron job to do this. Basically, you'll have a bash script, say it's located at /Users/ben/scripts/run_my_program.sh that will, at every hour:
Terminate the current running process (kill pid)
Execute the swift run my program --with-parameters and spit out the process ID
you can get the PID of the swift process you launch with echo $!, and then use sleep 1h to sleep for 1 hour and then kill the process with kill -9 and the PID you got in the first step.

How to track the progress of command executed using Start command

Consider an command which has to be executed more than once at the same time, I have created a bat file in which Start command is used to execute the command multiple times at once. But there are couple of problem I am facing,
After starting all the commands the main batch file is closed. It is not waiting until the new command window has done its job.
We are not able to keep track of the progress in new windows, for example consider the commands are executed from team city(CI) then the progress is not tracked.
Please help me on this, Thanks in Advance!
1) - Use START "" /WAIT, than start command wait till started process terminate, than continue running batch file.

Forgot to use & after command, need to send process to background

I have been running a program using nohup but I forgot to add & after the command so the terminal is stuck on the process that has been running for hours. the script I am running in python generates 5 processes each time.
Is there anyway I can make the entire script to continue in the background (get the same effect as an &) without killing and rerunning the process.
Hit Ctrl-Z to suspend the process.
Then bg to tell it to run again as a background process.

Returning handle to calling script perl

I have an executable which can run perl scripts using the following command at the prompt:
blah.exe Launch.pl
The way we have our tests setup is that we call the Launch.pl from Parent.pl like this "blah.exe Launch.pl" - script within script. However, when executing the command with backticks/system command the parent .pl script execution waits till I get the handle back by closing and exiting out of the application (blah.exe). At this point the code in parent.pl continues to execute.
How do I return the handle back to the parent .pl script after I get done running the code that is contained in the Launch.pl
So, parent.pl calls "blah.exe Launch.pl"; but after running the code inside Launch.pl inside the application (blah.exe) it just sits there waiting to be exited out of so that the code in parent.pl can continue running. I need to keep the application (blah.exe) open till I am done running a bunch of scripts one after another.
Run blah.exe in the background. When you are done with the Parent.pl, terminate the application with kill.

How do you stop a Windows Batch file from exiting early?

I have a windows batch file that looks similar to:
C:\DoStuff.cmd
move output.bak C:\newfolder\output.bak
The problem i have is that DoStuff.cmd executes a java program that once complete exits the batch run back to the command prompt. Line 2 never gets hit.
i have tried the following instead to execute the command in a new window:
start "My program" /WAIT C:\DoStuff.cmd
move output.bak C:\newfolder\output.bak
What happens with the above is that the new command window spawns the cmd file runs and exits back to a waiting command prompt and the window never closes, leaving the first command window waiting and the second doing nothing stuck after finishing step one.
How do i execute the first command without it having control of the batch run somehow?
many thanks in advance
You can use DOS call command:
#echo off
call C:\DoStuff.cmd
echo Exit Code = %ERRORLEVEL%
After getting error code you can proceed for example with:
if "%ERRORLEVEL%" == "1" exit /B 1