I want to schedule 2 cron jobs using my chef recipe. Here are my entries
#Rotate the error logs
cron "logrotate every 5 minutes" do
action :create
user "root"
minute "*/5"
command "<my command>"
end
#Rotate the Quantico error logs
cron "logrotate every 5 minutes" do
action :create
user "root"
minute "*/5"
command "<my second command>"
end
When I run this chef recipe, I always see the only one entry in my crontab -l output. Am I missing anything?
Well, after going through the chef logs, I found this out. Cron creates the schedules based on the name you specify in the recipe and it doesn't create two separate jobs with the same name, instead, it updates them, which made sense (after looking at the logs). This is what I found in the logs:
[2019-05-31T15:27:02-07:00] INFO: cron[logrotate every 5 minutes] added crontab entry
.
.
[2019-05-31T15:27:03-07:00] INFO: cron[logrotate every 5 minutes] updated crontab entry
So, the only thing I had to do was rename the second cron job to make my life easier and get back my Friday evening.
#Rotate the error logs
cron "logrotate every 5 minutes" do
action :create
user "root"
minute "*/5"
command "<my command>"
end
#Rotate the Quantico error logs
cron "logrotate another file every 5 minutes" do
action :create
user "root"
minute "*/5"
command "<my second command>"
end
Related
I am pretty new to the VPS running on Linux 19.04.
I have already had a at job which will be executed on January 27 13:00:00 2020.
I am just wondering if there is any possible way to add a new crontab job after the at job is executed?
Let say I want to add a crontab job 0 0 * * * /usr/bin/pm2 restart Bot, which will restart the Bot running with pm2 at 00:00 every day.
If I want to manually add the crontab job is no problem, I just crontab -e and edit it in nano. But I want it to automatically add the crontab job after the at job is executed, or after 1 minute of at job is executed.
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.
How do you write a cron job which immediately run, then run on every hour divisible by 4? Say I started the script at 13:25, the job fires up right away, then the next run would be at 16:00, 20:00, 00:00 and so on.
For the first immediate run, just execute the command manually. Then set your cron up like this to have it execute continuously every 4th hour
0 */4 * * * yourCommand
This will run yourCommand every 4 hours (00:00, 04:00, 08:00......)
I am using the cron cookbook to run every 30 minutes in the following way:
cron_d 'logrotate_check' do
minute "*/30"
command "logrotate -s /var/log/logstatus /etc/logrotate.d/consul_logs"
user 'root'
end
Please let me know if it is correct?
Yes, that is fine. In the future, please just try it yourself rather than asking the internet and waiting 10 hours.
I want to get the details of the last run cron job. If the job is interrupted due to some internal problems, I want to re-run the cron job.
Note: I don't have superuser privilege.
You can see the date, time, user and command of previously executed cron jobs using:
grep CRON /var/log/syslog
This will show all cron jobs. If you only wanted to see jobs run by a certain user, you would use something like this:
grep CRON.*\(root\) /var/log/syslog
Note that cron logs at the start of a job so you may want to have lengthy jobs keep their own completion logs; if the system went down halfway through a job, it would still be in the log!
Edit: If you don't have root access, you will have to keep your own job logs. This can be done simply by tacking the following onto the end of your job command:
&& date > /home/user/last_completed
The file /home/user/last_completed would always contain the last date and time the job completed. You would use >> instead of > if you wanted to append completion dates to the file.
You could also achieve the same by putting your command in a small bash or sh script and have cron execute that file.
#!/bin/bash
[command]
date > /home/user/last_completed
The crontab for this would be:
* * * * * bash /path/to/script.bash
/var/log/cron contains cron job logs. But you need a root privilege to see.
CentOs,
sudo grep CRON /var/log/cron