Run cron on Amazon Linux (set on Linux)

things to do

On AmazonLinux, set cron and execute AWS commands. As a prerequisite, the IAM Role of that command is required for EC2. I will not write the details this time.

OS Linux/Unix, Amazon Linux 2018.03

#cat /etc/os-release
NAME="Amazon Linux AMI"
VERSION="2017.09"
.
.

How to write a cron file

# cat /etc/crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed

I referred to here. https://kb.iu.edu/d/afiz

Create a file to execute

Let's execute the AWS command that outputs the EC2 Arn list in Tokyo

!/bin/sh
/usr/bin/aws ec2 describe-instances --region ap-northeast-1 |grep Arn |awk -F \" '{print $4}' >> /root/ec2-file

Be sure to try running the file here and check for any errors.

cron settings

Edit with ** crontab -e ** I want to test the operation, so let it run every minute

* * * * * bash /root/awscron

** Setting completed **

Log check

tail -f /var/log/cron

You can check the execution log with

Delete settings

Don't forget to delete the settings when you no longer need them ** Delete settings with crontab -r **

※ -r deletes everything set with crontab -e, so if it's already in operation You can comment out with -e

Tips Running crontab -e as root user edits / var / spool / cron / root Therefore, in this case, it is not necessary to specify the user as before.

Not editing with crontab

As mentioned above, if you make an operation, the cron file will disappear, so I will describe how to not use crontab

Create a file under /etc/cron.d/ for the cron setting file.

For example /etc/cron.d/Crontest

* * * * * root sh /root/awscron

Summary

This time, I only described the simple setting. I will summarize in more detail in the future.

Other sites that I referred to

cron - wikipedia https://en.wikipedia.org/wiki/Cron

Dangers of crontab -r https://qiita.com/NACK/items/c0c0feda4e7a8030346f

Recommended Posts

Run cron on Amazon Linux (set on Linux)
[Note] Run Django on Amazon Linux 2
Run docker-compose on Amazon Linux2 on ARM64
Install tomcat 5.5 on Amazon Linux.
Use sshpass on Amazon linux2
Install Homebrew on Amazon Linux 2
Run FreeBSD on Linux + qemu
Install oracle java8 on amazon linux2
Try installing OpenAM on Amazon Linux
Install pyenv on EC2 (Amazon Linux)
[Note] Install Imagick on Amazon Linux2
Introduce Python 3.5.2 environment on Amazon Linux
I'll install Ruby on EC2 (Amazon Linux2) 2020
Use Numpy, Scipy, scikit-learn on Amazon Linux
How to update php on Amazon linux 2
Build an LNPP environment on Amazon Linux 2
Learn sshd_config and authorized_keys (on Amazon Linux 2)
Upgraded mysql on Cloud9 (Amazon Linux) (5.5 to 5,7)
How to install Anisble on Amazon Linux 2
Run bootgen on Debian GNU / Linux, Ubuntu
On Ubuntu Linux, set Tab to q
Install Python 3.8, Pip 3.8 on EC2 (Amazon Linux 2)
Run Linux on ARM architecture with QEMU
Run a batch of Python 2.7 with nohup on Amazon Linux AMI on EC2
Install Python3 and Django on Amazon Linux (EC2) and run your web server
Run Debian (Linux) & LXQt on Athlon 200GE (AMD)
Set up golang with goenv on GNU / Linux
Set up an Objective-C 2.0 development environment on Linux
Install PHP 7 series on Amazon Linux 2 with Amazon Linux Extras
Set the startup script on Linux (RasPi, Edison)
Daemonizing processes on Linux
Run Kali Linux on Windows with GUI (without VirtualBox)
jblas on Arch Linux
Linux (WSL) on Windows
NAT router on Linux
Run Django on PythonAnywhere
Set sitecustomize.py on CircleCI.
Publish your Django app on Amazon Linux + Apache + mod_wsgi
Run mysqlclient on Lambda
Set up Docker on Oracle Linux (7.x) with Vagrant
Create an environment for MkDocs on Amazon Linux (attempted)
Develop .NET on Linux
[Part 1] Let's set up a Minecraft server on Linux
Wake on lan on Linux
OpenVPN Summary + Amazon Linux2
Compile and install MySQL-python for python2.7 on amazon linux
Run OpenMVG on Mac
Monitor traffic on Linux
Install Docker on Arch Linux and run it remotely
Update vscode on linux
[AWS EC2] How to install Maven on Amazon Linux 2
Try NeosVR on Linux
Check capacity on Linux
How to build a Python environment on amazon linux 2
[AWS] How to expand disk space on Amazon linux
Linux operation on Win10
Install LAMP on Amazon Linux 2 and build a WordPress environment.
Try running Amazon Linux 2 on-premises (VM on your local PC).
Run Lima Driver on Debian GNU / Linux for Ultra96 / Ultra96-V2
[AWS EC2] Settings you want to do on Amazon Linux 2
Launch NEM Symbol testnet node on AWS EC2 (Amazon Linux2)