Cron Jobs Linux row deletion - linux

I'm running this script;
$query = "SELECT * FROM XXX WHERE email='$Email'";
if($count==1) // fails
if($count==0) // succeeds
If successful
mysql_query ("INSERT INTO XXX (email) values ('$Email'");
Then proceeds onto the next script.
So, it checks to see if you have already ran this script in the past on that account, if you have your email is stored then you can't run this script ever again on that same email.
However, after this script has been processed I want it to delete the row created for the email after 6 hours.
So that after 6 hours they may run the script again.. I've been enlightened that I need to use Cron jobs for this, But I'm not sure how.. Any help is highly appreciated!
Many regards, and thanks in advance.

0 0,6,12,18 * * * /path/to/mycommand
This means starting from hour 0, 6, 12, and 18 the cron job would run. That would be the cron needed to do what you want.
Depending on which linux version you are running you will need to see how to actually create the cron job.

I would think at now +6 hours is a better choice here.

Related

How to run a python script between a particular times every single day (on Linux)?

I am looking for a way to be able to run a python script at a particular times of day and then have it auto terminated at another time of day. Ideally, I would want this to not be done within the script itself.
For example: I would want the script to start at 08:00 and end at 10:00 then start again at 11:30 and then terminate at 15:00 and I would need this to happen every day automatically.
I have browsed through many suggestions online, and many of them suggested to use cron, however, as far as I can see, cron does not natively offer the functionality of automatically terminating an application.
Others have suggested using cron to start the application at a particular time and then use another cron instance to create a "terminate" file that the program will search for at every loop iteration and if the file is present then the python script will terminate via a sys.exit() function or something, however, this seems quite janky and more of a workaround than a real solution.
You may use Jobber. You will be able to start scripts whenever you want and for the time you want.
Warning : Jobber is not free. You can try it for free though.
Here is the link to Jobber's website.
You could write a script that creates a lockfile with cron (https://unix.stackexchange.com/questions/12815/what-are-pid-and-lock-files-for), and use the lockfile to know what the process ID is, then terminate the process with that id using cron as well
After you have determined that the process name is uniquely identifiable you could do something like this (that's indeed also using cron).
0 8 * * * /path/to/unique_name.py& ( echo "pkill unique_name.py" | at 10:00 )
30 11 * * * /path/to/unique_name.py& ( echo "pkill unique_name.py" | at 15:30 )
Edit 1:
And "name safe" versions (using kill).
0 8 * * * /path/to/unique_name.py& echo kill $! | at 10:00
30 11 * * * /path/to/unique_name.py& echo kill $! | at 15:30

Can you confirm my crontab line is right

I want to set a cron job to run at 00h15 every Friday. Is this the correct way to do this:
15 0 * * 5
Use this kind of website to validate your crons:
http://crontab.guru/#15_0___5

CRON expression to schedule on hourly basis like e.g. 04:00, 10:00, 16:00, 22:00

I tried with * 4,10,16,22 * * * but didnt worked out throwing error while parsing the CRON expression.
I'm using CronScheduleBuilder.cronSchedule(CRON_EXPRESSION) method to schedule the job.
Please let me know is there anyother way apart from scheduling separately.
According to the below reference CronScheduleBuilder cant do that. Use the expression you wrote directly in the cron command without using CronScheduleBuilder.
Reference: https://groups.google.com/forum/m/#!topic/quartznet/P4m4X_uuhEM
See cron man page for details: http://www.unix.com/man-page/linux/5/crontab/

cron job for running a script based on db value

I had a sctipt runthisapp.sh and I am having some dates in my db table holiday . My problem is every day it should check the db ,if the date is present in holiday table runthisapp.sh should run at 10'o clock
Else
It should run at 8'o clock.
I had tried but can't find the solution .Can you.help me.on this please
You can have two crontab entries, one at 8am, one at 10am, passing different options to your script, e.g. the former takes --holiday=0, and the latter --holiday=1, and your script should just return doing nothing if in the wrong "holidayness".

Cron Job run between a range of minutes?

Ok so I see all these questions about crons running every 10 minutes, every whatever minutes, but it's SET. However, I want mine to RANDOMLY run every 10-15 minutes. Between those ranges so sometimes it'll pick 13 min, 12 min, etc.
Is there any possible way you can do that on a shared server? Or would I have to program it via PHP and have that script run everytime someone visits the page I want to refresh?
Thanks for your input!
I usually let the cronjob run every 10min and in the script itself that cron executed I place at the header of the script a sleep+random number of the extra random "window" I need - i.e: in your case 5min => 300sec
Something like:
#!/usr/bin/php
<?
sleep( rand(1, 300) );
print "starting job ! \n"
?>

Resources