Python Forum
Problem with croniter in python script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with croniter in python script
#1
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,...

# #!/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
========================================================
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,185 Jun-29-2023, 11:57 AM
Last Post: gologica
  Script stop work after 3 actioins - PLEASE WHERE IS THE PROBLEM? rondon442 0 1,557 Sep-27-2021, 05:40 PM
Last Post: rondon442
  Problem executing a script on a remote host tester_V 3 2,446 Sep-26-2021, 04:25 AM
Last Post: tester_V
  problem with sphinx and file directory in script kiyoshi7 0 2,284 Mar-11-2021, 03:52 PM
Last Post: kiyoshi7
  problem about slope in python script for bitcoin trading fisher_garry 1 2,500 Sep-02-2020, 01:39 PM
Last Post: fisher_garry
  How to kill a bash script running as root from a python script? jc_lafleur 4 5,877 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 2,291 May-28-2020, 05:27 PM
Last Post: micseydel
  Problem running script within console koepjo 3 9,895 Mar-26-2020, 07:11 AM
Last Post: koepjo
  Beginner problem in python script Cedmo 3 2,767 Jul-04-2019, 08:22 PM
Last Post: Cedmo
  Problem with this script MaxwellCosta 5 2,957 Jun-21-2019, 07:13 AM
Last Post: MaxwellCosta

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020