I have been trying to configure a cron job to run my ansible playbook every hour. I could not find any relevant examples on how to start the configuration . I have tried the below task in a separate ansible playbook and it shows the below output for crontab -l, but not execution seems to be happening. Help will be high appreciated.
root#ubuntu:/etc/ansible/# crontab -l
57 10 * * * ansible-playbook crontest.yaml
- name: Run an ansible playbook"
cron:
name: "Run play"
minute: "57"
hour: "10"
job: "ansible-playbook crontest.yaml"
Cron Logs:
Nov 24 07:35:01 ABC30VDEF290021 CRON[13951]: (root) CMD (echo "testing" > /etc/ansible/automation/logs/test.txt)
Nov 24 07:35:01 ABC30VDEF290021 CRON[13948]: (root) CMD ( /usr/bin/ansible-playbook /etc/ansible/automation/crontest.yml)
Nov 24 07:35:01 ABC30VDEF290021 CRON[13949]: (root) CMD (/usr/local/bin/ansible-playbook /etc/ansible/automation/main.yaml)
Crontab -e
#Ansible: Run play
*/1 * * * * /bin/sh -c '. ~/.profile; /usr/local/bin/ansible-playbook /etc/ansible/automation/crontest.yml
*/1 * * * * echo "testing" > /etc/ansible/automation/logs/test.txt
35 7 * * * /usr/local/bin/ansible-playbook /etc/ansible/automation/main.yaml
The following code will run the script every 30th minute of every hour
- name: Run CRON job to load data at every 30th minute of every hour.
become: yes
become_method: sudo
cron:
name: "load_data"
user: "root"
weekday: "*"
minute: "30"
hour: "*"
job: "python3 /path/to/my/script/loadScript.py > /home/ec2-user/loadData_result 2>&1"
state: present
Related
When I start a linux server with Cloud-init, I have a few scripts in /etc/cloud/cloud.cfg.d/ and they run in reverse alphabetical order
# ll /etc/cloud/cloud.cfg.d/
total 28
-rw-r--r-- 1 root root 173 Dec 10 12:38 00-cloudinit-lifecycle-hook.cfg
-rw-r--r-- 1 root root 2120 Jun 1 2021 05_logging.cfg
-rw-r--r-- 1 root root 590 Oct 26 17:55 10_aws_yumvars.cfg
-rw-r--r-- 1 root root 29 Dec 1 18:22 20_amazonlinux_repo_https.cfg
-rw-r--r-- 1 root root 586 Dec 10 12:38 50-cloudinit-tomcat.cfg
-rw-r--r-- 1 root root 585 Dec 10 12:40 60-cloudinit-newrelic.cfg
The last to execute is 00-cloudinit-lifecycle-hook.cfg, in which I complete the lifecycle for the Auto Scaling Group with a CONTINUE. The ASG fails if it doesn't receive this signal after a given time out.
The issue is that even if there's an error in 50-cloudinit-tomcat.cfg, it still runs 00-cloudinit-lifecycle-hook.cfg instead of stopping
How can I ensure cloud-init stops and never reaches the last script? I would like the ASG to never receive the CONTINUE signal if there's any error.
Here are the files:
EC2 instance user-data:
#cloud-config
bootcmd:
- [cloud-init-per, once, "app-volume", mkfs, -t, "ext4", "/dev/nvme1n1"]
mounts:
- ["/dev/nvme1n1", "/app-volume", "ext4", "defaults,nofail", "0", "0"]
merge_how:
- name: list
settings: [append]
- name: dict
settings: [no_replace, recurse_list]
50-cloudinit-tomcat.cfg
#cloud-config
merge_how:
- name: list
settings: [append]
- name: dict
settings: [no_replace, recurse_list]
runcmd:
- "#!/bin/bash -e"
- set +x
- echo ' '
- echo '# ===================================='
- echo '# Tomcat Cloud Init '
- echo '# /etc/cloud/cloud.cfg.d/'
- echo '# ===================================='
- echo ' '
- echo '#===================================='
- echo '# Run Ansible'
- echo '#===================================='
- echo ' '
- set -x
- ansible-playbook /opt/init-config/tomcat/tomcat-config.yaml
when I run ansible-playbook /opt/init-config/tomcat/tomcat-config.yaml directly in the instance I get an error, and I know it returns 2
ansible-playbook /opt/init-config/tomcat/tomcat-config.yaml #shows errors
echo $? # shows "2"
00-cloudinit-lifecycle-hook.cfg
#cloud-config
merge_how:
- name: list
settings: [append]
- name: dict
settings: [no_replace, recurse_list]
runcmd:
- "/opt/lifecycles/lifecycle-hook-continue.sh"
An alternative I can think of, is to send a ABANDON signal instead of CONTINUE as soon as there's en error in one of the cloud-init config. But I can't find in the documentation on to define if there's an error
SHELL=/bin/bash
...
30 05 * * * sh script.sh
or
SHELL=/bin/bash
...
30 05 * * * script.sh
is enough ?
I did the following cron task with sudo crontab -e:
0 * * * * /usr/local/bin/docker_build/backup.sh
Cron works well (launch backup to each hour).
In this bash script, I write a backup sql file based on this pattern :
DATE_NOW=$(date +%Y-%d-%m-%H-%M)
$DATE_NOW.sql
But the hour return by %H variable is not the hour of my current timezone.
I check the following commmand in my shell :
date //return good date
cat /etc/timezone //return good timezone
UPDATE :
It was a very stupid mistake. To be short, my bash script run in a docker container. The container timezone wasn't set with my current timezone. I had the following in my image build :
RUN echo America/Montreal > /etc/timezone && \
dpkg-reconfigure --frontend noninteractive tzdata
You could specify the timezone in the crontab entry
0 * * * * env TZ='Europe/Madrid' /usr/local/bin/docker_build/backup.sh
Or, I suppose, in the crontab itself
TZ='Europe/Madrid'
0 * * * * /usr/local/bin/docker_build/backup.sh
You can find out what the timezone is in root's cron environment
* * * * * date "+\%F \%T \%Z \%z" > /tmp/root.cron.date.txt
I have the following command in my crontab:
* * * * * root /home/amith/m.sh >dev/null 2>&1
where m.sh consists of:
#!/bin/sh
curl -0 "http://www.google.com" > /home/amith/Desktop/h2
but the command in shell script is not executing at all.
Can anyone please tell me the solution?
First be sure that your script /home/amith/m.sh is running correctly!
Your crontab entry is wrong you dont need root before script. Also your redirection to /dev/null is not good you are missing / before dev
You can set env varibale for SHELL in crontab with this
crontab -e
SHELL=/bin/sh
Then add your script:
* * * * * /home/amith/m.sh >/dev/null 2>&1
* * * * * cd /full/path; sh m.sh;
First, change the directory into your file location (use cd). Then, bash it with sh command.
Change * * * * * with the time schedule. To run the file every minute * * * * * To run every hour (1:00, 2:00, 3:00 and so on) use 00 * * * * To run everyday at 6 AM use 00 06 * * * To run the file on 9th March at 6 AM use 00 06 09 03 * The structure is minute hour date month day of week If you want to schedule it every Monday at 06 AM use this 00 06 * * 1 See this link http://www.pantz.org/software/cron/croninfo.html
I want to write a cron task to record the ntpdate synchronization info into the system log, but there's no such info printed in the /var/log/messages after this cron task is done, where did I do wrong?
The followings are what my crontab looks like.
*/1 * * * * ntpdate 192.168.100.97 | logger -t "NTP"
*/1 * * * * echo "log test" | logger -t "TEST"
*/1 * * * * whoami | logger -t "WHO"
When I do tailf /var/log/messages and wait some time I only got the following lines, the NTP lines are missing.
Oct 29 15:22:01 localhost TEST: log test
Oct 29 15:22:01 localhost WHO: root
Oct 29 15:23:01 localhost TEST: log test
Oct 29 15:23:01 localhost WHO: root
Oct 29 15:24:01 localhost TEST: log test
Oct 29 15:24:01 localhost WHO: root
Oct 29 15:25:01 localhost TEST: log test
Oct 29 15:25:01 localhost WHO: root
Oct 29 15:26:01 localhost TEST: log test
Oct 29 15:26:01 localhost WHO: root
But when I do the ntpdate 192.168.100.97 | logger -t "NTP" in the command line, I could see there's message Oct 29 15:28:39 localhost NTP: 29 Oct 15:28:39 ntpdate[11101]: adjust time server 192.168.100.97 offset 0.000043 sec print out in the system log. What am I missing here?
Thanks in advance for your kind help.