2018年8月31日金曜日

CentOS7のcron

CentOS7は、デフォルトでanacronが入っている。


# vi /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# 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


# vi /etc/anacrontab
# /etc/anacrontab: configuration file for anacron
# See anacron(8) and anacrontab(5) for details.
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22
#period in days   delay in minutes   job-identifier   command
1       5       cron.daily              nice run-parts /etc/cron.daily
7       25      cron.weekly             nice run-parts /etc/cron.weekly
@monthly 45     cron.monthly            nice run-parts /etc/cron.monthly


【noanacrontabに入れ替える。】

# yum -y install cronie-noanacron
# yum -y remove cronie-anacron

【ユーザ毎】
$ crontab -e
00 * * * * /home/aws/aws.sh

/var/spool/cron下にユーザ名毎に作られる。


【システム初期設定】
# cat /etc/cron.d/dailyjobs
# Run the daily, weekly, and monthly jobs if cronie-anacron is not installed
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# run-parts
02 4 * * * root [ ! -f /etc/cron.hourly/0anacron ] && run-parts /etc/cron.daily
22 4 * * 0 root [ ! -f /etc/cron.hourly/0anacron ] && run-parts /etc/cron.weekly
42 4 1 * * root [ ! -f /etc/cron.hourly/0anacron ] && run-parts /etc/cron.monthly
[root@buffy3 ~]#

# systemctl restart crond
確認
# less /var/log/cron


ユーザ単位であれば、
$ crontab -e
05 * * * * /home/aws/aws/app/s3daily.sh
cronの再起動不要。







pythonのインストール

python Ver3.6をインストール
# yum install python36 python36-libs python36-devel
# python36 -m ensurepip # pip を使えるようにする。
【VENV仮想環境の構築】
# cd /usr/local
# python36 -m venv venv36
# chmod -R a+w venv36 ※インストールFIX後戻す。chmod -R go-w vnen36
$ source /usr/local/venv36/bin/activate

※下記環境がセットされる。
VIRTUAL_ENV=/usr/local/venv/venv36
PATH=/usr/local/venv/venv36/bin:$PATH
deactivate () {}

【PATHの確認】$ python36 -c "import sys; print(sys.path)"
['', '/usr/lib64/python36.zip', '/usr/lib64/python3.6', '/usr/lib64/python3.6/lib-dynload', '/usr/local/venv36/lib64/python3.6/site-packages', '/usr/local/venv36/lib/python3.6/site-packages']

※pipインストール確認
$ pip3 install --upgrade pip
$ pip3 install --upgrade pip --proxy=http://192.168.1.254:3128
$ pip3 list
$ pip3 list --outdated
$ pip3 list --outdated | awk 'NR>2 {print $1}' | xargs pip3 install -U
$ pip3 freeze>requirements.txt
$ pip3 install -r requirements.txt

【proxy下】
$ pip3 install --upgrade pip --proxy=http://user:pwss@192.168.25.254:3128


※エラー発生
'missing Apache httpd server packages.' % APXS)
RuntimeError: The 'apxs' command appears not to be installed or is not executable. Please check the list of prerequisites in the documentation for this package and install any missing Apache httpd server packages.
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-zxt5kngl/mod-wsgi/

# yum -y install httpd-devel で解決。

【プロキシ経由でインストール】
pip3  --proxy=http://192.168.x.xxx:3128  install python-dateutil

【Djangoのインストール】
$ pip3 install Django
$ ls -l /usr/local/venv/venv36/lib/python3.6/site-packages/django   # 確認


【excelの読み書き】
# python36 -m pip install openpyxl   # excelの読み書きを使えるようにする。

【oracleと接続】

# python36 -m pip install cx_oracle

ここから該当バージョンのドライバをタウンロード
https://www.oracle.com/technetwork/jp/database/features/instant-client/index-352321-ja.html
instantclient-basic-linux.x64-11.2.0.4.0.zip

# cd /opt/libs
# unzip instantclient-basic-linux.x64-11.2.0.4.0.zip
# ln -s /opt/libs/instantclient_11_2 /opt/libs/oracle

# vi /etc/profile.d/oracle.sh
export LD_LIBRARY_PATH=/opt/libs/oracle:$LD_LIBRARY_PATH
# . /etc/profile
# vi /etc/sysconfig/httpd
LD_LIBRARY_PATH=/opt/libs/oracle:$LD_LIBRARY_PATH

# vi /etc/ld.so.conf.d/oracle.conf
/opt/libs/oracle
# ldconfig
# ldconfig  -p |grep libclntsh

【確認】
$ python36
>>> import cx_Oracle
>>> cx_Oracle.clientversion()
(11, 2, 0, 4, 0)


いつも上手く行くのになぜかエラー
cx_Oracle.DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library: "/opt/libs/oracle/lib/libclntsh.so: cannot open shared object file: No such file or directory". 

# vi /etc/ld.so.conf.d/oracle.conf
/opt/libs/oracle
# ldconfig
# ldconfig  -p |grep libclntsh

# dnf install libnsl
# python3
> import ctypes
> ctypes.cdll.LoadLibrary('/opt/libs/oracle/libclntsh.so.11.1')
→動いた!!

なぜ? いつもは不要なのに!!

# python36 -m pip install --upgrade pip
# python36 -m pip list
Package    Version
---------- -------
cx-Oracle  7.0.0
et-xmlfile 1.0.1
jdcal      1.4 
openpyxl   2.5.12
pip        18.1 
setuptools 39.0.1 


※python Ver2と共存可能。
# python test.py
# python36 test.py

【例1】
# vi excel.py
#!/usr/bin/env python
import openpyxl as excel
wb = excel.Workbook()
ws = wb.active
ws["A1"] = "こんにちは"
wb.save("hello.xlsx")
print("save end")

【例2】
#!/usr/bin/env python
import cx_Oracle

HOST='192.168.xx.xxx'
PORT = '1521'
SID = 'xxxxxxxx'

tns = cx_Oracle.makedsn(HOST, PORT, SID)
conn = cx_Oracle.connect("userID", "password", tns)

print ( conn.version)
cur = conn.cursor()
print ( conn.version)

#cur.execute("""SELECT INSTANCE_NAME,HOST_NAME FROM v$instance""")

#rows = cur.fetchall()

#for r in rows:
#    print("%d : %s" % (r[0], r[1]))


【例3】
# vi test.py
#!/usr/bin/env python
import os
import sys
import glob
import shutil
import datetime
import smtplib
from email.mime.text import MIMEText
from email.utils import formatdate

down_dir = sys.argv[1]
data_dir = sys.argv[2]
store_dir = sys.argv[3]
office_dir = sys.argv[4]
time_report = sys.argv[5]

FROM_ADDRESS = 'xxxxx@xxxx.xx.xx'
MY_PASSWORD = 'yyyyy'
TO_ADDRESS = 'zzzz@zzzzzz.zz.zz'
BCC = 'xxxxx@xxxxxx.xx.xxp'
SUBJECT = 'title'
BODY = 'body'

def create_message(from_addr, to_addr, bcc_addrs, subject, body):
  msg = MIMEText(body)
  msg['Subject'] = subject
  msg['From'] = from_addr
  msg['To'] = to_addr
  msg['Bcc'] = bcc_addrs
  msg['Date'] = formatdate()
  return msg

def send(from_addr, to_addrs, msg):
  smtpobj = smtplib.SMTP('localhost', 25)
  smtpobj.ehlo()
# smtpobj.starttls()
  smtpobj.ehlo()
# smtpobj.login(FROM_ADDRESS, MY_PASSWORD)
  smtpobj.sendmail(from_addr, to_addrs, msg.as_string())
  smtpobj.close()
def maillog(subject , body):
  msg = create_message(FROM_ADDRESS, TO_ADDRESS, BCC, subject, body)
  send(FROM_ADDRESS, TO_ADDRESS, msg)

def utf2sjis( src, dst ):
  with open( dst, 'w', encoding='cp932') as fout:
  with open( src , encoding='utf-8') as fin:
  fout.write(fin.read())


for download_dir, dirs, files in os.walk(down_dir):
  num = len(files)
  maillog( "START Credit daily process %d files downloaded(%s)" % ( num, time_report) , "%8d files downloaded to %-30s(Execution time:%s)" % (num, download_dir, time_report ))

for name in glob.glob( download_dir+"/*" ):
bname = os.path.basename( name )
  start = bname.find( "SERCHCHARACTER_" ) + len( "SERCHCHARACTER_" )
  year = bname[ start : start + 4 ]
  month = bname[ start + 4 : start + 6 ]
  day = bname[ start + 6 : start + 8 ]
  if int(day) < 16:
    term = "A"
    else:
    term = "B"
    folder= data_dir + "/" + year + month + term
    dname = folder + "/" + bname
    sname = store_dir + "/" + bname
    oname = office_dir + "/" + bname
    if os.path.exists( folder ):
      if os.path.exists( dname ):
        shutil.move( dname, dname + ".dup" + datetime.datetime.now().strftime("%Y%m%d%H%M%S.%f"))
        maillog( "WARNING File Duplicate %s" % bname, "%s is allready exist.(data)" % dname )
     else:
       os.mkdir( folder )
     shutil.move( name, folder )
  if os.path.exists( sname ):
     shutil.move( sname, sname + ".dup" + datetime.datetime.now().strftime("%Y%m%d%H%M%S.%f"))
     maillog( "WARNING File Duplicate %s" % bname, "%s is allready exist.(ToSend1)" % sname )
  if os.path.exists( oname ):
    shutil.move( oname, oname + ".dup" + datetime.datetime.now().strftime("%Y%m%d%H%M%S.%f"))
    maillog( "WARNING File Duplicate %s" % bname, "%s is allready exist.(ToSend2)" % oname )

  utf2sjis( dname, oname )
  shutil.copy( oname, sname )


#today = datetime.date.today().day
#yesterday = datetime.datetime.now() - datetime.timedelta(days=1)
#
#if today == 1 or today == 16:
#
# if today == 16:
# term = "A"
# else:
# term = "B"
#
# term_folder = yesterday.strftime( "%Y%m" ) + term
#
# office_folder = office_dir + "/" + term_folder
#
# if os.path.exists( office_folder ):
# shutil.move( office_folder, office_folder + ".dup" + datetime.datetime.now().strftime("%Y%m%d%H%M%S.%f"))
# maillog( 'WARNING File Duplicate', "%s is allready exist.(ToSend2)" % office_folder )
#
# shutil.copytree( data_dir + "/" + term_folder, office_folder )

zabbix7 amazon linux2023 インストール postgres15

【postgres】 dnf -y install postgresql15-server postgresql15-server-devel postgresql-setup initdb passwd postgres vi `find / -name pg_hba.con...