Ansible creating cronjob every 10sec - cron

I want to know if there is any way to create a ansible playbook able to run a cronjob every 10sec.
Thanks all for your answers

Cron only has resolution down to minutes. The best you can get out of cron is once per minute. To get once every ten seconds, you'll need a script that will run your task and then wait ten seconds, and repeat it 6 times. If you have that script, you can use cron to run it every minute. And of course, you can use the Ansible cron job to set it up on your servers.

Task to create a Cron Job
- name: Sets cron job to run script per minute
cron:
name: Sets cron job to run script per minute
minute: "*"
hour: "*"
day: "*"
month: "*"
weekday: "*"
user: "centos"
job: sh sample.sh > sample.out 2>&1
cron module parameters explained:
minute, hour, day, month, and weekday are configured to specify the cronjob interval at which the job should be executed
user: the user for which the cronjob should be configured
job: the operation to be performed
As we can see that the minimum we can set is per minute. So, to run the operation per 10 seconds, we need to add that support in the script.
Sample script sample.sh
#!/bin/bash
while sleep 10; do
echo "Running after a sleep of 10s"
# Add the business logic here
done
Now, this script will get triggered every minute by the cronjob and the script in turn will run the business logic every 10 sec.

Related

How to setup to use local time in cron schedule?

Cron by default uses UTC time zone. How to set it up to use Local time (say CST) in the cron expression (for cron schedule).
You can set your system's time zone to the intended time zone and then state the time for that zone in the cron job:
sudo timedatectl set-timezone America/New_York
and confirm typing timedatectl, or do
sudo dpkg-reconfigure tzdata
After changing the time zone, make sure to restart cron:
sudo service cron restart
Cron job:
30 5 * * * echo "run at half past 5" >> ~/logfile.log 2>&1
Cron doesn't care about time zone. It compares the current time to the cron string and executes the job if they match.

I want the cron job to run the script weekly basis

I am trying to setup a cron job on a Ubuntu server. I want the cron job to run the script on weekly basis. Problem is - It should be a working day, If im mentioning it with time interval, it fails during weekoffs - Need an Schedular Exp which has to work weekly only on working days at office hours.(9am to 9pm)max.
Want to Execute the script every week #6 pm during the weekdays. It Can be Mon to Fri.
Step1
sudo apt-get install cron
systemctl status cron
Step2
Configure the cron job:
crontab -e
Select an editor of your choice
0 0 * * 0 /path/to/command
Syntax: minute hour day-of-month month day-of-week command.
Day-of-week goes from 0-6 where 0 is Sunday.
Save it.

How to configure cron to run at multiple random intervals?

I'm working on a new project and I would like to setup a cron to run every 6-8 hours at a random minute. Any suggestions on the best way to achieve this would be greatly appreciated.
Let's run the cron every 6 hours:
0 */6 * * * /path/to/script.sh
Now, in your bash script:
#!/bin/bash
maxdelay=$((2*60)) # 2 hours converted to minutes
delay=$(($RANDOM%maxdelay)) # a random delay
(sleep $((delay*60)); /path/to/script.sh) & # background a subshell to wait, then run the script
You can also use anacron for RANDOM_DELAY feature.

Wait 60 Seconds to Run Cron Job After Reboot Then Run Job Every 10 Minutes

I have a script that I would like to run 60 seconds after initial system reboot and then every 10 minutes after that. I currently need two cron job listings to achieve this:
*/10 * * * * php myscript.php
#reboot /bin/sleep 60; php myscript.php
The first listing will run my cron job immediately after system boot and so I need to have the second listing to account for the on start wait time.
Is there anyway to combine the above two cron listings into one?

Is this the correct way to run a script at certain hours?

I'd like a cron job to run a command at:
8am
10am
5pm
7pm
9pm
11pm
I've tried this, but when I check my logs, I see that the command is run too many times.
* 8,10,17,19,21,23 * * * COMMAND >> mylog.txt
Your command means "run on every minute of the given hours". If you want to run it just once, you need to set the minute field, e.g.:
0 8,10,17,19,21,23 * * * COMMAND >> mylog.txt
This way the command only runs at the start (minute 0) of each of the given hours

Resources