AWS EC2 Ubuntu server: set crontab

AWS EC2 Ubuntu server でcrontab設定した。 無事に私のPythonスクリプトを定時起動できるようになった。 cronにも色々あり、今回起動したAWS EC2 Ubuntu serverに用意されているcronでは 以下の操作で無事使えたのでメモしておく。

Deploy my python script to AWS EC2 Ubuntu server

on AWS EC2 Ubuntu server

  • (Done) Install python
  • (Done) Install python packages
  • (Done) Put my python scripts
  • Test my scripts
  • Set crontab

Test my scripts

Create shell script to excecute my Python script

example

my_ping.sh

    #!/bin/bash

    cd $(dirname $0)
    . ~/venv/python3.9/myproject1/bin/activate
    python my_ping.py

add execute permission

    $ chmod +x (path)/my_ping.sh 

Test my shell script

example

    $ (path)/my_ping.sh 

Set crontab

test

show crontab list

    ubuntu@ip-172-XX-XX-XX:~$ crontab -l
    no crontab for ubuntu

edit crontab

set echo 'hello' every 1 min.

    ubuntu@ip-172-XX-XX-XX:~$ crontab -e

    * * * * * echo 'hello'

show crontab list

    ubuntu@ip-172-XX-XX-XX:~$ crontab -l
    # Edit this file to introduce tasks to be run by cron.
    # 
    # Each task to run has to be defined through a single line
    # indicating with different fields when the task will be run
    # and what command to run for the task
    # 
    # To define the time you can provide concrete values for
    # minute (m), hour (h), day of month (dom), month (mon),
    # and day of week (dow) or use '*' in these fields (for 'any').
    # 
    # Notice that tasks will be started based on the cron's system
    # daemon's notion of time and timezones.
    # 
    # Output of the crontab jobs (including errors) is sent through
    # email to the user the crontab file belongs to (unless redirected).
    # 
    # For example, you can run a backup of all your user accounts
    # at 5 a.m every week with:
    # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
    # 
    # For more information see the manual pages of crontab(5) and cron(8)
    # 
    # m h  dom mon dow   command
    * * * * * echo 'hello'

show system log

    ubuntu@ip-172-XX-XX-XX:~$ less /var/log/syslog | grep CRON
    (snip)
    Mar 26 13:58:01 ip-172-XX-XX-XX CRON[35425]: (ubuntu) CMD (echo 'hello')
    Mar 26 13:58:01 ip-172-XX-XX-XX CRON[35424]: (CRON) info (No MTA installed, discarding output)
    Mar 26 13:59:01 ip-172-XX-XX-XX CRON[35430]: (ubuntu) CMD (echo 'hello')
    Mar 26 13:59:01 ip-172-XX-XX-XX CRON[35429]: (CRON) info (No MTA installed, discarding output)

for my shell script

edit crontab

example

set my shell script 'my_ping.sh' at 8 minuet every hour

    ubuntu@ip-172-XX-XX-XX:~$ crontab -e


    08 * * * * /home/ubuntu/venv/python3.9/myproject1/my_ping/my_ping.sh

show crontab list

    ubuntu@ip-172-XX-XX-XX:~$ crontab -l
    # Edit this file to introduce tasks to be run by cron.
    # 
    # Each task to run has to be defined through a single line
    # indicating with different fields when the task will be run
    # and what command to run for the task
    # 
    # To define the time you can provide concrete values for
    # minute (m), hour (h), day of month (dom), month (mon),
    # and day of week (dow) or use '*' in these fields (for 'any').
    # 
    # Notice that tasks will be started based on the cron's system
    # daemon's notion of time and timezones.
    # 
    # Output of the crontab jobs (including errors) is sent through
    # email to the user the crontab file belongs to (unless redirected).
    # 
    # For example, you can run a backup of all your user accounts
    # at 5 a.m every week with:
    # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
    # 
    # For more information see the manual pages of crontab(5) and cron(8)
    # 
    # m h  dom mon dow   command
    08 * * * * /home/ubuntu/venv/python3.9/myproject1/my_ping/my_ping.sh


    ubuntu@ip-172-XX-XX-XX:~$ 

show system log

    ubuntu@ip-172-XX-XX-XX:~$ less /var/log/syslog | grep CRON
    (snip)
    Mar 26 14:08:01 ip-172-XX-XX-XX CRON[35518]: (ubuntu) CMD (/home/ubuntu/venv/python3.9/myproject1/my_ping/my_ping.sh)
    Mar 26 14:08:02 ip-172-XX-XX-XX CRON[35517]: (CRON) info (No MTA installed, discarding output)
    ubuntu@ip-172-XX-XX-XX:~$ 

over

Comments

Popular Posts