Goedemorgen,

Ik weet niet of deze vraag hier past maar zit er mee in mijn maag. Ik kom maar niet verder.
Ik heb een python script geschreven die mijn pakbonnen en verzendlabels print via het lezen van een email van een imap connectie. Als ik dit script draai vanuit mijn terminal werkt het goed: "pyhton3 pi/home/printmailattachment/printmailattachments.py". Er komt een geluidje bij een bestelling en de bonnen printen uit.

Alleen als ik dit script in de sudo crontab -e (als root) aanroep loopt hij door het script heen, alleen print hij alles 2 x en speelt die geen geluid af. Ik heb het bestand schrijfbaar gemaakt,

wat zou het kunnen zijn? Ik heb geen idee.. Alvast bedankt


#!/usr/bin/python3
# prints and stores E-Mail Attachments

import datetime
import email
import getpass, imaplib
import os
import sys
import cups
import time
import locale
import shutil
import pprint
from bs4 import BeautifulSoup


### Set your IMAP Serverdata and the printer name (you need a running cups server on your machine)
userName = ''           # Imap Username
passwd = ''  # Imap Password


imap_server = 'imap.gmail.com'        # Imap server address (without port)
printer_name='HP_LaserJet_M14-M17'         # Name of the printer - you can get the names of the printers by running the printer.py script
Imapfolder='Afdrukken'      # Imap Folder


detach_dir = '.'
if 'attachments' not in os.listdir(detach_dir):
    os.mkdir('attachments')


try:
    imapSession = imaplib.IMAP4_SSL(imap_server)
    typ, accountDetails = imapSession.login(userName, passwd)
    if typ != 'OK':
        print ('Not able to sign in!')
        raise
    imapSession.select(Imapfolder)
    typ, data = imapSession.search(None, 'UnSeen')
    if typ != 'OK':
        print ('Error searching Inbox.')
        raise

    # Iterating over all emails

    for msgId in data[0].split():

        typ, messageParts = imapSession.fetch(msgId, "(RFC822)")

        if typ != 'OK':
            print ('Error fetching mail.')
            raise

        emailBody = messageParts[0][1]
        mail = email.message_from_bytes(emailBody)


        for part in mail.walk():
            if part.get_content_maintype() == 'multipart':
                def get_text(msg):
                    if msg.is_multipart():
                        return get_text(msg.get_payload(0))
                    else:
                        return msg.get_payload(None, True)
            string=get_text(mail)  # string = E-Mail Text - maybe for later use...
          #  print (string)


        if part.get('Content-Disposition') is None:
            continue
        fileName = part.get_filename()
        
        
            
        if bool(fileName):
            filePath = os.path.join(detach_dir, 'attachments', fileName)
            if not os.path.isfile(filePath) :
                fp = open(filePath, 'wb')
                fp.write(part.get_payload(decode=True))
                fp.close()

               # if ('Inpaklijst' in fileName) or ('Pakbon' in fileName):    # Prints attachments, if filename contains 'invoice' or 'order'
                    #os.system("mplayer "+ soundfile)       # comment in, if a sound should play, when a attachment gets printed
        
                if  ('OFF' in fileName) or ('verzendlabel' in fileName):
                    #os.system("mplayer /home/pi/printmailattachment/applause.wav")
                    filePath1='attachments/'
                    conn = cups.Connection()
                    printers = conn.getPrinters()
                    conn.printFile (printer_name, filePath1+fileName, "", {})
                    print ('Attachment gets printed')
                if  ('Nieuwe-order' in fileName):    
                    os.system("mplayer /home/pi/printmailattachment/applause.wav")
                    
                os.remove(filePath1+fileName)     
        
    imapSession.close()
    imapSession.logout()

except :
    print ('There was an error during getting mails from the server.')
En wat gebeurt er als je samen met je cronjob de uitvoer naar een bestand schrijft? Wordt het script ook dan twee keer uitgevoerd?

dat heb ik niet getest, hoe zou ik dat kunnen testen? de cronjob is
/2* * * * * pyhton3 pi/home/printmailattachment/printmailattachments.py"
Pyhton3? Je bedoelt python3 als executable. ;-)


/2* * * * * python3 pi /home/printmailattachment/printmailattachments.py >> /home/printmailattachment/log.txt
Dit is de versie van uit terminal handmatig
MPlayer 1.3.0 (Debian), built with gcc-8 (C) 2000-2016 MPlayer Team
do_connect: could not connect to socket
connect: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control.

Playing /home/pi/printmailattachment/applause.wav.
libavformat version 58.20.100 (external)
Audio only file format detected.
Load subtitles in /home/pi/printmailattachment/
==========================================================================
Opening audio decoder: [pcm] Uncompressed PCM audio decoder
AUDIO: 44100 Hz, 2 ch, s16le, 1411.2 kbit/100.00% (ratio: 176400->176400)
Selected audio codec: [pcm] afm: pcm (Uncompressed PCM)
==========================================================================
AO: [pulse] 44100Hz 2ch s16le (2 bytes per sample)
Video: no video
Starting playback...
A: 4.0 (04.0) of 4.0 (04.0) 0.1%


en deze vanuit de log

MPlayer 1.3.0 (Debian), built with gcc-8 (C) 2000-2016 MPlayer Team
Terminal type `unknown' is not defined.

Playing /home/pi/printmailattachment/applause.wav.
libavformat version 58.20.100 (external)
Audio only file format detected.
Load subtitles in /home/pi/printmailattachment/
==========================================================================
Opening audio decoder: [pcm] Uncompressed PCM audio decoder
AUDIO: 44100 Hz, 2 ch, s16le, 1411.2 kbit/100.00% (ratio: 176400->176400)
Selected audio codec: [pcm] afm: pcm (Uncompressed PCM)
==========================================================================
AO: [alsa] 44100Hz 2ch s16le (2 bytes per sample)
Video: no video
Starting playback...
A: 0.0 (00.0) of 4.0 (04.0) ??,?%
A: 0.0 (00.0) of 4.0 (04.0) 0.1%
A: 0.0 (00.0) of 4.0 (04.0) 0.1%
A: 0.1 (00.0) of 4.0 (04.0) 0.1%
A: 0.1 (00.0) of 4.0 (04.0) 0.0%
A: 0.1 (00.1) of 4.0 (04.0) 0.0%
A: 0.2 (00.1) of 4.0 (04.0) 0.0%
A: 0.2 (00.1) of 4.0 (04.0) 0.0%
A: 0.2 (00.2) of 4.0 (04.0) 0.0%
A: 0.2 (00.2) of 4.0 (04.0) 0.0%
A: 0.3 (00.2) of 4.0 (04.0) 0.0%
A: 0.3 (00.3) of 4.0 (04.0) 0.0%
A: 0.3 (00.3) of 4.0 (04.0) 0.0%
A: 0.4 (00.3) of 4.0 (04.0) 0.0%
A: 0.4 (00.3) of 4.0 (04.0) 0.0%
A: 0.4 (00.4) of 4.0 (04.0) 0.0%
A: 0.5 (00.4) of 4.0 (04.0) 0.0%
A: 0.5 (00.4) of 4.0 (04.0) 0.0%
A: 0.5 (00.5) of 4.0 (04.0) 0.2%
A: 0.5 (00.5) of 4.0 (04.0) 0.2%
A: 0.6 (00.5) of 4.0 (04.0) 0.2%
A: 0.6 (00.6) of 4.0 (04.0) 0.2%
A: 0.6 (00.6) of 4.0 (04.0) 0.2%
A: 0.7 (00.6) of 4.0 (04.0) 0.2%
A: 0.7 (00.6) of 4.0 (04.0) 0.2%
A: 0.7 (00.7) of 4.0 (04.0) 0.2%
A: 0.7 (00.7) of 4.0 (04.0) 0.1%
A: 0.8 (00.7) of 4.0 (04.0) 0.1%
A: 0.8 (00.8) of 4.0 (04.0) 0.1%
A: 0.8 (00.8) of 4.0 (04.0) 0.1%
A: 0.9 (00.8) of 4.0 (04.0) 0.1%
A: 0.9 (00.9) of 4.0 (04.0) 0.1%
A: 0.9 (00.9) of 4.0 (04.0) 0.1%
A: 1.0 (00.9) of 4.0 (04.0) 0.1%
A: 1.0 (00.9) of 4.0 (04.0) 0.1%
A: 1.0 (01.0) of 4.0 (04.0) 0.1%
A: 1.0 (01.0) of 4.0 (04.0) 0.1%
A: 1.1 (01.0) of 4.0 (04.0) 0.1%
A: 1.1 (01.1) of 4.0 (04.0) 0.1%
A: 1.1 (01.1) of 4.0 (04.0) 0.1%
A: 1.2 (01.1) of 4.0 (04.0) 0.1%
A: 1.2 (01.1) of 4.0 (04.0) 0.1%
A: 1.2 (01.2) of 4.0 (04.0) 0.1%
A: 1.2 (01.2) of 4.0 (04.0) 0.1%
A: 1.3 (01.2) of 4.0 (04.0) 0.1%
A: 1.3 (01.3) of 4.0 (04.0) 0.1%
A: 1.3 (01.3) of 4.0 (04.0) 0.1%
A: 1.4 (01.3) of 4.0 (04.0) 0.1%
A: 1.4 (01.3) of 4.0 (04.0) 0.1%
A: 1.4 (01.4) of 4.0 (04.0) 0.1%
A: 1.5 (01.4) of 4.0 (04.0) 0.1%
A: 1.5 (01.4) of 4.0 (04.0) 0.1%
A: 1.5 (01.5) of 4.0 (04.0) 0.1%
A: 1.5 (01.5) of 4.0 (04.0) 0.1%
A: 1.6 (01.5) of 4.0 (04.0) 0.1%
A: 1.6 (01.6) of 4.0 (04.0) 0.1%
A: 1.6 (01.6) of 4.0 (04.0) 0.1%
A: 1.7 (01.6) of 4.0 (04.0) 0.1%
A: 1.7 (01.6) of 4.0 (04.0) 0.1%
A: 1.7 (01.7) of 4.0 (04.0) 0.1%
A: 1.8 (01.7) of 4.0 (04.0) 0.1%
A: 1.8 (01.7) of 4.0 (04.0) 0.1%
A: 1.8 (01.8) of 4.0 (04.0) 0.1%
A: 1.8 (01.8) of 4.0 (04.0) 0.1%
A: 1.9 (01.8) of 4.0 (04.0) 0.1%
A: 1.9 (01.8) of 4.0 (04.0) 0.1%
A: 1.9 (01.9) of 4.0 (04.0) 0.1%
A: 2.0 (01.9) of 4.0 (04.0) 0.1%
A: 2.0 (01.9) of 4.0 (04.0) 0.1%
A: 2.0 (02.0) of 4.0 (04.0) 0.1%
A: 2.0 (02.0) of 4.0 (04.0) 0.1%
A: 2.1 (02.0) of 4.0 (04.0) 0.1%
A: 2.1 (02.1) of 4.0 (04.0) 0.1%
A: 2.1 (02.1) of 4.0 (04.0) 0.1%
A: 2.2 (02.1) of 4.0 (04.0) 0.1%
A: 2.2 (02.1) of 4.0 (04.0) 0.1%
A: 2.2 (02.2) of 4.0 (04.0) 0.1%
Zo te zien zijn er dus nog wat fouten op te lossen.
En wat betrefd de dubbele print functie.. hij voert hem 2 keer uit ofzo maar wel een tussen van die 2 min, misschien bij de uitvoering van de cron maak het hij mailtje niet ongelezen in dat systeem terwijl via de handmatige terminal wel?

Attachment gets printed
Attachment gets printed > hier zou dit moeten stoppen
Attachment gets printed
Attachment gets printed


[size=xsmall]Toevoeging op 17/02/2022 11:06:54:[/size]

Terminal type `unknown' is not defined.
Hoe zou ik dit op kunnen lossen en heeft die ook te maken met de dubnbele print functie?

1 * * * * export TERM=vt100; /

ik vind hier iets over, maar snap dit niet wat ik er mee kan
Geluidjes afspelen werkt niet als je de opdracht middels een cronjob aanstuurt. Gaat fout misschien daarom die TERM melding. Verwijder alle uitvoer audio omdat die niet naar de logging kan. Verander print statements naar write(logfile) statements, van uit waar print de cron naartoe immers??, de cron heeft geen scherm.
Het verwerken in de cron kent geen paden. Je moet dus steeds voor alles het volledige pad opgeven. Ook voor een eigen logfile. Dus /usr/bin/python3 ipv python3. Heb je echt een pi/home ?? Misschien zo:

*/2 * * * * /usr/bin/pyhton3 /home/pi/printmailattachment/printmailattachments.py" >>/home/pi/errorlog.log

*/2 is elke 2e minuut en wellicht is dat te snel en gaat de timing fout en daarom 2x printen?

Check foutlogging in /home/pi/errorlog /var/log/syslog
Weet je zeker dat 1 cronjob binnen 2 minuten klaar is?
Hallo Aad,

Dank je wel.
Ja hij is snel klaar..

Heb nu logging aangepast, maar in de cronjob leest de mail zie ik en print de bijlage uit. maar hij zet de mail niet op gelezen, en de 2de keer wel.

Als muziek afspelen niet werk, haal ik dat wel weg.

output is:
Attachment gets printed
Attachment gets printed

maar deze blijven open in de mailbox vandaar dat ze weer geprint worden en dan staan ze wel op gelezen

.---------------- Minuut (0 - 59) 
|  .------------- Uur (0 - 23)
|  |  .---------- Dag van de maand (1 - 31)
|  |  |  .------- Maand (1 - 12) of januari, februari, maart... 
|  |  |  |  .---- Dag van de week (0 - 6) (zondag is 0 of 7) of zondag, maandag, dinsdag... 
|  |  |  |  |
*  *  *  *  *  commando dat uitgevoerd moet worden


Dat zijn 5 posities: jij hebt volgens mij een * te veel:
*/2* * * * *

bedoel je niet */2 * * * *
hij voert hem wel uit, maakt dat uit dan?

Reageren