batch script to kill process if still running after a certain time - batch-file

I need to create a batch script that launches a program, and waits for up to 1 minute for the program to finish, and then do something else. The exact steps are:
Launch program A
Wait for program A to finish OR kill it if not finished within 1 minute.
Launch program B
To emphasise, in step 2, I do not want to wait for the full 1 minute if program A is finished within say, 5 seconds.

Related

How to implement running commands in the background by supporting ‘&’ and wait (for UNIX Shell)?

So my understanding is that if you terminate a command with ‘&,’ it runs the process in the background. If two commands were separated by ‘&,’ they would run simultaneously.
e.g. command1 & command2 //runs simultaneously
To implement this, I would have the shell fork a child process and have the child execute the 1st command I. The background. The parent would not wait and would execute the second command in parallel.
Is this the right approach? And how can I apply this to support wait()?
This is using bash on a generic unix:
BigMac:tmp steve$ sleep 10 & sleep 13 & sleep 20 &
[1] 69168
[2] 69169
[3] 69170
BigMac:tmp steve$ wait %1; wait 69169; kill %3
[1] Done sleep 10
[2]- Done sleep 13
[3]+ Terminated: 15 sleep 20
The first line launches three separate sleeps. The shell assigns them job numbers [1], [2], [3]; whereas the system assigns them process id's 69178,69169,69170 resp. The commands wait and kill can take either identifier, as shown in the second line.
The second line waits for the first job % indicates to the shell that the following number is in its naming scheme, then waits for the second process the lack of % indicates a process identifier, then kills the third job.
There is a lot more to this than such a simple example can convey; particularly the notion of job is quite a bit more involved than this example portrays.
The UNIX Programming Environment ISBN despite being nearly 40 years old, provides a great fundamental understanding of how and why UNIX works the way it does. There are more modern, detailed, and system specific works as well.

After 2 seconds run SIGALRM alarm command in C

I write a C program and my code copies a file from a directory to another. If the process takes more than 2 seconds, every second it has to print "Process is continue". How to delay alarm command 2 seconds?

Batch script for sleep/hibernate

I would like to write a script in batch that forces the computer to enter
sleep (s3) and/or hibernate(s4), for a certain amount of time. I couldn't find answer for this question nowhere.
Example:
Computer enters sleep state.
30 seconds pass.
Computer returns and continues the script.
I managed to use an external program, but after a few cycles of the procedure
the computer for some reason enters a sleep state for 4,294,966,391 seconds
and only continues the script when turned on manually.

make a C program relaunch itself after x seconds

Is it possible to make a program written in C to stop and then relaunch itself after x seconds In windows ?? And if yes, how to make it happen ??
You can accomplish that goal by having your program launch a second program, whose only function is to wait a while and then launch your first program again. In pseudocode, the idea would be:
Program A:
Do whatever the program is supposed to do
Launch program B
exit.
Program B:
Wait predetermined time
Launch program A
exit.
I hope this answers your question adequately.
The way I do this kind of thing is with a command-line option 'startDelay=xx'.
If there is no such command, my app just starts up as normal. If there is, its first action , before attempting to open any files, DB, construct GUI, start threads, start server etc. is to sleep for 'xx' seconds.
If my app needs to restart itself, it copies its own command-line, adds the 'startDelay=xx' to it and launches a new copy of itself, which then immediately sleeps. The original then has plenty of time to shut down normally before the new copy starts the bulk of its run-up.
No need for any other app or Windows scheduler and/or cron crap:)

Is there a way to stop and resume a C program

I am doing a project in C and in that I need to process a lot of data . Is there a way to stop the program automatically after 30 minutes and then resume whenever I run the program again?
There is no automatic way to do this. If you're on a Unix system though, you can press Ctrl+Z while your program is running to send it a STOP signal. It will wait until you continue again by sending it a CONT signal (the easiest way is with the fg shell command). A stopped process is still resident in memory, but is not running.
There are more sophisticated ways to take a "snapshot" of a running program and save it to disk. Later, you can load the snapshot and continue execution of the program. For example, see CryoPID.
You could automate it. The Crtl-Z and fg are, as already mentioned, nothing more than signals. In Unixes you can send them with the kill command. So kill -19 $PID will stop your process (when $PID is its PID) and kill -18 will continue it.
You just have to write a wrapper script, that looks like:
#!/bin/sh
PIDFILE=wherever/myprogram.pid
if [ -e $PIDFILE ]; then
kill -18 `cat $PIDFILE`
else
myprogram &
echo $! > $PIDFILE
fi
sleep 30
kill -19 `cat $PIDFILE`
(very short, untested, you have to take care to remove the pidfile when you kill the process). First time you run it, it will start the execution (for 30 seconds), the later times it will just revoke your program. You could improve it e.g by checking in the beginning if there is a process of your program in the process list, and if not remove the pidfile in case it is already there. So it would even behave correct after reboot or termination of your process.
Short answer: Without putting in some effort to have your program 'hibernate itself', no.
If you're running on UNIX then I'm sure you can set up a cron job to suspend / resume the program as and when needed. Under Windows you'll be afforded no such luxury.

Resources