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?
Related
I created step-scripted that print for me the name of function the debugger calls.
Now I want to automate the part that I need to type:
thread step-scripted -C MyTrace.Trace
How can I run the above command from a script?
so I will do something like this:
script
while True:
thread step-scripted -C MyTrace.Trace
First off, there's no reason that a step plan has to do just one step. If you want to step forever, then just have the step plan do it - never set the plan to complete, and return false from should_stop. Even more convenient, if you are using a recent lldb, you can pass arguments to your scripted step plan using the -k <key> -v <value> arguments. So you could also have your plan take a "count" input, and step that many times.
Otherwise, the easiest way to do this is to use the Python interface to implement a custom command that automates this step. SBThreads are the things you step. If you use the command form that takes an SBExecutionContext, described here:
https://lldb.llvm.org/use/python-reference.html#id6
you can get the thread from SBExecutionContext.thread then use SBThread. StepUsingScriptedThreadPlan to call your thread plan to do the step. Once you are in python, writing a loop to do this forever or till some condition, etc. should be easy. Your command could also take number of times to step, etc.
Note, you can also run commands in the script interpreter using SBCommandInterpreter.HandleCommand if that seems easier to you.
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.
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 wrote some C code to switch on and off some LEDs.
Actually, I want to trigger them accurate to music but haven't found a better way than using usleep() in between yet.
Anyway, turning on the LEDs, waiting at usleep() and turning off the LEDs again works pretty accurate when I call the program on the command line.
Now I'd like cron to execute the program let's say every five minutes. Therefore I added following cronjob with crontab -e:
*/5 * * * * bash ~/startShow.sh >~/log 2>&1
Same problem without the shell script.
*/5 * * * * ~/projects/startLEDShow >~/log 2>&1
startShow.sh
date
/usr/sbin/i2cset -y 1 0x40 0x00 0x21 # Preparation for communication via I2C
cd projects
./startLEDShow
The program is triggered every five minutes, but at some point (not always the same point) the execution seems to stop for a moment and resumes about a second later. That is only when the program is ran by cron, not when I call it from the commandLine.
Why is that so, is this perhaps due to the usleep() used in the program, and how can I make sure that the program execution isn't suspended for some time?
Update 1: Here is an extract with the usleep-section from the program
// Start reading
while(NULL != (word = readToChar(fp, wordBuffer))) {
// Values for the LEDs are stored in a struct
updateValuesForLEDs(next, word); // Update struct "next"
usleep(((next->time/timeFactor - lastTime)*1000000)); // Wait
lastTime = next->time / timeFactor; // Set lastTime for next iteration
setLEDs(i2cConnection, next, buffer); // Set the LEDs to the brightness values stored in struct "next" via I2C
}
Update 2:
After ensc's comment I found way to solve my issue.
The cronjob was started with a nice value of 10 whereas most other processes' values circled around 0. Setting my job to a lower nice value (which requires root privileges) gives it a higher priority and prevents it from being paused.
00 20 * * * sudo nice -n -20 ~/projects/startLEDShow >~/log.txt 2>&1
I doubt you're running a real-time operating system, so usleep is not guaranteed to wake up your process with any guarantee of performance. For that matter, even if your process is woken up at the right time, it's always possible for the kernel to interrupt it and do somehing else for an arbitrary amount of time.
I have to implement a linux command, called DCMD, which has the following function: It must execute another default linux command to a certain date and time, both specified in input.
In short, I should write like this: dcmd "command" "date and time".
Well the problem is not date or hour, in fact I can manage it properly, if it is looking into the future, if the day, month and year are correct, etc. ..
Also the command I think I've figured out how to handle it: I used the system call "execlp" and it run properly.
Well, at this point I don't know how to merge command and data, that is, run the following command at the time indicated.
Could someone explain to me how to do?
On linux, use cron or at to schedule jobs for later running.
cron: Specify a cron job with your specific date. Format your command as minute hour day month ? year command and add it to your crontab file. cron will then run your job just once. Use crontab to handle your crontab file. Man page for crontab
at command: Syntax: at [-V] [-q queue] [-f file] [-mldbv] TIME to run the script on stdin at TIME. Alternatively, run script in a file with the -f flag. Man page for at
Additional information:This is a Operating System assignment in which I have to re-implement some of the features of "at" or "crontab".
I have found a way of how to solve this problem.
First of all I should call a "fork", then in the child process I should call the "execlp", while the parent process goes on.
If I want to delay the command, I'll call a "sleep" in the child process (I asked about this point to the professor a few days ago, and he said that it's fine).
But I have this question: is it a valid method? Does this method create zombie processes?