Hello! help a novice understand where to dig: 1. I have a python script that in the console from the environment works out with a bang for launching the script in the console:

cd /root/bill source bin/activate puthon filegen.py 

script text: (logic: run on the table in mongo, get HTML, generate a PDF from it and bang the record as done)

 #!/root/bill/bin/python # coding: utf-8 from __future__ import unicode_literals from mongoengine import * import datetime import time import calendar from io import BytesIO as IO import gzip import pdfkit import pytils from bson import json_util from bson.json_util import dumps from bson.objectid import ObjectId import sys import random reload(sys) # Reload does the trick! sys.setdefaultencoding('UTF8') connect(host='mongodb://main:PWD@HOST/bill?ssl=true&ssl_cert_reqs=CERT_NONE') class Genq(Document): dokstring = StringField() outpdf = StringField() print ("started") foreva = True while foreva: print ("....") reqs = Genq.objects() for doc in reqs: print ("++++++") pdfkit.from_string(doc.dokstring, doc.outpdf) print (">>>>>>") doc.delete() print ("!!!!!!!") 

Trying to run this eternal script in the crown with the command:

 @reboot /root/bill/bin/python /root/bill/filegen.py >>/var/log/syslof 

I receive the following message in the console:

 root@bill2:~# tail 50 /var/log/syslog tail: cannot open '50' for reading: No such file or directory ==> /var/log/syslog <== started .... ++++++ Jul 22 04:26:56 bill2 postfix/pickup[1115]: C16891600F3: uid=0 from=<root> Jul 22 04:26:56 bill2 postfix/cleanup[1263]: C16891600F3: message-id=<20160722082656.C16891600F3@bill2> Jul 22 04:26:56 bill2 postfix/qmgr[1116]: C16891600F3: from=<root@bill2>, size=1221, nrcpt=1 (queue active) Jul 22 04:26:56 bill2 postfix/local[1265]: C16891600F3: to=<root@bill2>, orig_to=<root>, relay=local, delay=0.02, delays=0.01/0.01/0/0, dsn=2.0.0, status=sent (delivered to mailbox) Jul 22 04:26:56 bill2 postfix/qmgr[1116]: C16891600F3: removed Jul 22 04:27:00 bill2 ntpdate[603]: step time server 91.189.89.198 offset -0.344170 sec Jul 22 04:27:16 bill2 ntpdate[1281]: adjust time server 91.189.89.198 offset -0.000024 sec root@bill2:~# 

As I understand the script goes somewhere in a line

 pdfkit.from_string(doc.dokstring, doc.outpdf) 

But why does it work out of the console as needed?

All I understand is that the fact is that the crown has its own environment, from which either the file system is not visible, or the pdfkit library is not visible, although I run everything from the root

Help please with practical advice. thank

  • Maybe there is some exception, it is worth adding a catch and display it. - Vladimir Gamalyan
  • changed the code to `for doc in reqs: print (" ++++++ ") try: pdfkit.from_string (doc.dokstring, doc.outpdf) doc.delete () print (" !!!!!!! " ) except: print (">>>>>>") and received a set in the console >>>>> how to understand now what the exception is? - Sebulba Kastorsky 6:51 pm
  • yeah, found the error output: root @ bill2: ~ # tail 50 / var / log / syslog tail: can’t open '50' for reading: No such file or directory ==> / var / log / syslog <== >>> >>> (u'Unexpected error: ', <type' exceptions.IOError '>) .... ++++++ >>>>>> (u'Unexpected error:', <type 'exceptions.IOError '>) .... ++++++ >>>>>> - Sebulba Kastorsky
  • I played with the creation of files. I tried the os.mknod('/var/www/output/123.pdf') command os.mknod('/var/www/output/123.pdf') when I started it in the console - it creates, I reboot, it starts via kroner and throws again the .IOError rights to / var / www / output / three sevens - Sebulba Kastorsky

1 answer 1

Judging by the line source bin/activate - you are using virtualenv or its fork / clone. virtualenv is a separate environment with different paths in which modules can be installed that are not present in the system python (which is run by default for all UNIX users)

Therefore, create a wrapper-file in which you can set the correct environment:

run_me.sh:

 #!/bin/bash export VIRTUAL_ENV=/root/bill cd $VIRTUAL_ENV source $VIRTUAL_ENV/bin/activate $VIRTUAL_ENV/bin/python /path/to/filegen.py 

and run it from CRONTAB '

  • If I place run_me.sh in / root, then the line in the crown should be: @reboot /root/run_me.sh? and again, do you need to do chmod -x /root/run_me.sh? - Sebulba Kastorsky
  • UPD1: I placed the file sh in var I changed the line in the crown to @reboot /var/fs.sh >> / var / log / syslog / I see in the log: Jul 22 06:36:13 bill2 CRON [982]: (root) CMD (/var/fs.sh >> / var / log / syslog) But nothing happened (( - Sebulba Kastorsky
  • @SebulbaKastorsky, of course you must give permission to run chmod 770 run_me.sh - MaxU
  • @SebulbaKastorsky, if I understand correctly @reboot - CRON will launch your script once after restarting ( reboot ) the machine. At the testing stage, it makes sense to use a scheduled launch - MaxU
  • Very interesting .. I tried to just run the bash fs.sh script bash fs.sh got the following: <br/> root @ bill2: / var # bash fs.sh: No such file or directoryll: No such file or directory <br/> / bin / python: No such file or directory root @ bill2: / var # ^ C It turns out that the system does not see any folder? - Sebulba Kastorsky