How to setup to use local time in cron schedule? - cron

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.

Related

How to update the crontab and check if its running correctly?

I've modified the crontab with:
sudo crontab -e
and added a cron for a script to run every minute so I can test if it works:
1 * * * * /scripts/backup-script.sh > /scripts/backup-script.logs
Afterwards, I tried to restart the cron service to restart the server but the cron doesn't seem to be working, I tried to use:
crontab -l
and it appears to have the old content as if I didnt even modify it. However, going into crontab -e does show the updated content.
To make your script run every minute your cron record must be:
* * * * * /scripts/backup-script.sh > /scripts/backup-script.logs
What you enter will run every 1st minute every hour
And if you add record via crontab command you do not need to touch cron daemon
To see the cron record you add with sudo crontab -e you must check it with command sudo crontab -l. Otherwise you list cron record of different user

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.

Ansible creating cronjob every 10sec

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.

Why does this cron job run every minute?

I ran a cron job as ec2 user, in AWS server.
I set the cron command, like this
*/5 * * * * ec2-user bash ./dailyMailSend.sh
in crontab -e file.
It was set to run after every 5 minutes. But it runs every minute. Don't know why ?
For Amazon Linux use "0/5" instead of "*/5". This expression means every 5th minute from 0 through 59.
Next, do not specify relative paths in your crontab. Use only absolute paths.

Is there any way to commit changes in git automatically at specific time

I want to know if there is any way to auto-commit changes made in git based on specific time.
Suppose if I set the configuration, it should commit whatever the code present in repository at exactly 12:00 AM every day or at specific time in a day.
From what I found after searching, there is a way to commit on every time we save a file. But not timely auto-commits.
As Nic5300 suggested, an easy way to do this is to write a simple script that is called by cron at a specific time:
auto_commit.sh
=======================================
#!/bin/bash
MESSAGE="Auto-commit: $(date)"
REPO_PATH="/home/user/repo"
git -C "$REPO_PATH" add -A
git -C "$REPO_PATH" commit -m "$MESSAGE"
Just update the REPO_PATH and MESSAGE with whatever you'd like. Now, you add the script to your crontab by running crontab -e.
To run it every night at midnight, your crontab would look like this:
* 0 * * * auto_commit.sh > /dev/null 2>&1
Obviously, you'd have to update that path to wherever your script is saved. Just make sure you have cron running (depends on what init system you're using), and you should be good to go. Check out https://crontab-generator.org if you want to fiddle more with your crontab.
Your crontab example would start executing the auto_commit.sh script at midnight every minute for 1 hour. To make it run only once every midnight, you need:
0 0 * * * auto_commit.sh > /dev/null 2>&1

Resources