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:)
Related
I have a C pthread running a non-blocking event loop which executes lua scripts. A lua script runs io.popen to execute a shell script which takes some time to complete.
I am only interested in capturing the first few characters from the output of the script even though the script takes time to complete. As the lua script reads those first few characters and reaches its end it returns execution to C as expected. The problem is that the shell script that was opened with io.popen keeps running in the background until it completes. Calling f.close() in lua after reading the first few characters does not seem to halt its execution (it appears to only close the read side of the pipe?). Unfortunately, it starts blocking my C thread and displays "write error: broken pipe" messages until the shell script ends. This would make sense if the read end of the pipe is closed (and garbage collected) as the lua script ends execution, and the write end is still being written to by the shell script. The question is, how do I either kill the shell script since i am no longer interested in the rest of its output/execution, or silence it in a way that will not block my parent process?
I have tried redirecting 2>/dev/null on the io.popen command but that only silenced the "write error: broken pipe" messages - the blocking is still there.
If there is a way to get the child process (from C) of what lua openes with io.popen and kill that? (I'm thinking that might also help).
Any thoughts/suggestions are greatly appreciated!
I made a simple client-server program in c using sockets and now i want to test it ,by simulating many clients connecting to the server at the same time!I wrote a script to execute the client: ./client 20 times but it didn't work for me since it waited for each client to finish.
Also i wrote another program in c ,this time with threads so it could execute each client with system(./client) and then detach the thread ,but again i had the same problem!
So what is the correct way to implement this?
The easiest solution is to do your shell script, but put an & after the ./clientk call, which will put it in the background and run the next command immediately
Here's a really simple way to launch a number of clients without waiting for each to complete:
#!/bin/bash
for i in $(seq 0 20)
do
./client &
done
wait
I have a problem.I've written a program in C for armbian.
I am using RTKLIB software for GPS data conversion from ubx to RTCM3.
I get some data from serial port and start str2str(rtklib software).
It creates this command to run
str2str -in tcpsvr://:2101#ubx -out serial://ttyS2:115200#rtcm3
and call system function to run this command. It is successful, but when I send a new command, I want it to stop the str2str software.
I've tried the exit(0) and it stops my software. I don't want to stop my software. I want to stop str2str and create a new command and run it again.
How can I do it? I am not good with the linux environment.
Thanks
I suggest you find out how to search for the str2str process you want to kill, and get the PID. A stackoverflow search will reveal this and then use the PID to kill the process. Unless RTKLIB has a process to do this directly.
If you run a .bat or .cmd file with %0|%0 inside, your computer starts to use a lot of memory and after several minutes, is restarted. Why does this code block your Windows? And what does this code programmatically do? Could it be considered a "bug"?
This is the Windows version of a fork bomb.
%0 is the name of the currently executing batch file. A batch file that contains just this line:
%0|%0
Is going to recursively execute itself forever, quickly creating many processes and slowing the system down.
This is not a bug in windows, it is just a very stupid thing to do in a batch file.
This is known as a fork bomb.
It keeps splitting itself until there is no option but to restart the system.
http://en.wikipedia.org/wiki/Fork_bomb
What it is:
%0|%0 is a fork bomb. It will spawn another process using a pipe | which runs a copy of the same program asynchronously. This hogs the CPU and memory, slowing down the system to a near-halt (or even crash the system).
How this works:
%0 refers to the command used to run the current program. For example, script.bat
A pipe | symbol will make the output or result of the first command sequence as the input for the second command sequence. In the case of a fork bomb, there is no output, so it will simply run the second command sequence without any input.
Expanding the example, %0|%0 could mean script.bat|script.bat. This runs itself again, but also creating another process to run the same program again (with no input).
%0 will never end, but it never creates more than one process because it instantly transfers control to the 2nd batch script (which happens to be itself).
But a Windows pipe creates a new process for each side of the pipe, in addition to the parent process. The parent process can't finish until each side of the pipe terminates. So the main program with a simple pipe will have 3 processes. You can see how the bomb quickly get's out of control if each side of the pipe recursively calls the parent batch!
It's a logic bomb, it keeps recreating itself and takes up all your CPU resources. It overloads your computer with too many processes and it forces it to shut down. If you make a batch file with this in it and start it you can end it using taskmgr. You have to do this pretty quickly or your computer will be too slow to do anything.
I have written a program which calculates the amount of battery level available in my laptop. I have also defined a threshold value in the program. Whenever the battery level falls below threshold i would like to call another process. I have used system("./invoke.o") where invoke.o is the program that i have to run. I am running a script which runs the battery level checker program for every 5 seconds. Everything is working fine but when i close the bash shell the automatic invocation of invoke.o is not happening. How should i make the invoke.o to be invoked irrespective of whether bash is closed or not??. I am using UBUNTU LINUX
Try running it as: nohup ./myscript.sh, where the nohup command allows you to close the shell without terminating the process.
You could run your script as a cron job. This lets cron set up standard input and output for you, reschedule the job, and it will send you email if it fails.
The alternative is to run a script in the background with all input and output, including standard error output, redirected.
While you could make a proper daemon out of your program that kind of effort is probably not necessary.
man nohup
man upstart
man 2 setsid (more complex, leads to longer trail of breadcrumbs on daemon launching).