I have made a script in python to perform FTP transfers that will run in crontab every minute.
The script queries the MySQL database and translates the cron language with the croniter library. If the current date and time match the programmed date, the ftp transaction will be performed.
It seems to work correctly except when the cron scheduled is in minutes and only asterisk (*). If the hour or date are in (*) it seems that there is no problem.
The crontotime function receives the variable in crontab language: "* * * * *" or "05 17 * * *" and translates it to date and time:
For exemple https://crontab.guru/:
* * * * * -> “At every minute.” 2019-03-27 09:36:00, 2019-03-27 09:37:00, 2019-03-27 09:38:00,...
05 17 * * * -> “At 17:05.” 2019-03-27 17:05:00, 2019-03-28 17:05:00, 2019-03-29 17:05:00,...
========================================================
Now: 2019-03-25 16:56
Scheluded: #* 16 * * *# 2019-03-25 16:00
========================================================
Now: 2019-03-25 16:56
Scheluded: #56 16 * * *# 2019-03-25 16:56
========================================================
Now: 2019-03-25 17:05
Scheluded #* * * * *# 2019-03-25 16:06 ERROR NO TRANSFER
========================================================
Now: 2019-03-25 17:05
Scheluded: #05 17 * * *# 2019-03-25 17:05
========================================================
Now: 2019-03-27 10:00
Scheluded: #* * * * *# 2019-03-27 09:01 ERROR NO TRANSFER
========================================================
The script queries the MySQL database and translates the cron language with the croniter library. If the current date and time match the programmed date, the ftp transaction will be performed.
It seems to work correctly except when the cron scheduled is in minutes and only asterisk (*). If the hour or date are in (*) it seems that there is no problem.
The crontotime function receives the variable in crontab language: "* * * * *" or "05 17 * * *" and translates it to date and time:
For exemple https://crontab.guru/:
* * * * * -> “At every minute.” 2019-03-27 09:36:00, 2019-03-27 09:37:00, 2019-03-27 09:38:00,...
05 17 * * * -> “At 17:05.” 2019-03-27 17:05:00, 2019-03-28 17:05:00, 2019-03-29 17:05:00,...
# #!/usr/bin/env python # -*- coding: utf-8 -*- # -*- coding: 850 -*- import sys print("Usando codificación", sys.stdout.encoding) print("Inicio") import time import croniter import datetime import mysql.connector import paramiko from itertools import chain class ConexionMySQL(object): def conexion(self): hostname = 'XXX.XXX.XXX.XXX' username = 'admin' password = 'admin' database = 'transfer' connxdb = mysql.connector.connect( host=hostname, user=username, passwd=password, db=database ) datoscron = connxdb.cursor() datoscron.execute("SELECT * FROM transfer.transfer") # a = [x[0] for x in datoscron.description] ahora = datetime.datetime.now().strftime("%Y-%m-%d %H:%M") Crontask().crontask(datoscron, ahora) connxdb.close() print("Close connection") class Crontask(object): def crontask(self,datoscron,ahora): for row_task in datoscron: programada = (' '.join(i for i in row_task[2:7]).strip()) nuevatarea = Crontask().crontotime(programada) if nuevatarea == ahora: FTPtask().ftptask(row_task) LogTransfer().log_tareas(row_task) else: print("========================================================") print("///////////////// NO TRANSFER /////////////////////////") print("========================================================") return def crontotime(self,programada): crontask = croniter.croniter(programada) nextcrontask = crontask.get_next(datetime.datetime).strftime("%Y-%m-%d %H:%M") return nextcrontask class FTPtask(object): def ftptask(self,row_task): ip = row_task[7] usuario = row_task[8] contrasena = row_task[9] origen_path = row_task[10] destino_path = row_task[11] transporteFTP = paramiko.Transport(ip, 22) transporteFTP.connect(username = usuario, password = contrasena) sftp = paramiko.SFTPClient.from_transport(transporteFTP) sftp.put(origen_path, destino_path) return class LogTransfer(object): def log_tareas(self,row_task): log_file = "log_contrabFTP.log" timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") row_task_FINAL = chain(row_task[1:9],row_task[10:12]) log = (' '.join(i for i in row_task_FINAL)) task = timestamp+" "+log print(task) with open(log_file, mode="a", newline='') as logfile: logfile.write(task + "\n") return if __name__ == "__main__": myConexion = ConexionMySQL() myConexion.conexion()Results:
========================================================
Now: 2019-03-25 16:56
Scheluded: #* 16 * * *# 2019-03-25 16:00
========================================================
Now: 2019-03-25 16:56
Scheluded: #56 16 * * *# 2019-03-25 16:56
========================================================
Now: 2019-03-25 17:05
Scheluded #* * * * *# 2019-03-25 16:06 ERROR NO TRANSFER
========================================================
Now: 2019-03-25 17:05
Scheluded: #05 17 * * *# 2019-03-25 17:05
========================================================
Now: 2019-03-27 10:00
Scheluded: #* * * * *# 2019-03-27 09:01 ERROR NO TRANSFER
========================================================