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.
Related
I want to run an .exe multiple times (lets say 1000 times), sequentially, using a batch file.
The problem is that everytime the program finishes, a window pops saying that the program finished and it needs us to press the "Exit" button to close everything and, through batch files, I don't know how to "press buttons".
In sum:
I want to run a program 1000 times sequentially, having to press buttons in-between them.
I hope I was clear enough and thanks in advance!
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:)
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.
I have a method that runs when I call a specific URL on my application. It processes rows in a database queue. The interval is set as lowest interval possible with Cron, ie. 1 minute. This needs dropping to 30 seconds, so I'm wondering how best to achieve this.
I was thinking I could build a loop into my script that runs the code twice, with a sleep of say 30 seconds in between, but I'm wondering if there's a cleaner way than this.
Also, is there a way of running this method from the command line, without the need to call an actual URL?
This answer is strongly inspired by this one
You can leave your code as it is. To call it twice a minute you can add a second cronjob and let one cronjob sleep 30 seconds before executing the task.
* * * * * /path/to/executable param1 param2
* * * * * ( sleep 30 ; /path/to/executable param1 param2 )
This has two advantages
Your code doesn't need to worry about when and how many times it is executed (e.g. if you call it manually it doesn't need to run twice!)
If your code takes 10 seconds to execute everything you would have a delay when using sleep inside of your function. This way you don't.
As in similar threads it was stated, cron is not usable for this, at least not directly. IMHO the most sane approach is to write a shell script, that does your task every 30 seconds, then set up a cron job to check if your script is running, and if not, it should start it.
By the way, a stored procedure would not be a good solution in your case?
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).