Python file fail on a crontab job [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
-2
down vote

favorite
1












I'm using RPI zerow to run a python code (Raspbian Strech-Lite).



I need to run 2 file during startup: MQTTlogger.py and Sched_MQTT.py.
Both files run OK using command line, but when I added 2 job on crontab only one of them get executed ( the second file ). file name and path are OK.



Searching in all log files in /var/log did not show aany evidence for failure during boot.



Any idea why ?



@reboot /usr/bin/python3 /home/guy/github/SmartHome_SoC/MQTTswitches/MQTTlogger.py
@reboot /usr/bin/python3 /home/guy/github/SmartHome_SoC/MQTTswitches/Sched_MQTT.py



EDIT1: running only MQTTLOGGER.PY




leaving only MQTTlogger.py in crontab shows that it is a problem with running in crontab rather than running one after the other.



what can be the reason for file fail to run on a crontab job, but succeed when executed on a command line ? how can I debug it ?




EDIT2 : trying to catch crontab error




@reboot /usr/bin/python3 /home/guy/github/SmartHome_SoC/MQTTswitches/MQTTlogger.py >> /home/guy/crontab.log 2>&1


yields :



File "/home/guy/github/SmartHome_SoC/MQTTswitches/MQTTlogger.py", line 1, in <module>
import paho.mqtt.client as mqtt
ImportError: No module named 'paho'


so I'm guessing it has something to do with path to the MQTT package










share|improve this question















closed as too broad by Rui F Ribeiro, RalfFriedl, John WH Smith, schily, thrig Sep 9 at 15:14


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • Does one have to run before the other? Cron will run them pretty much simultaneously. Did you get any form of error mailed to you (or to the user whose crontab this is)? Do they depend on certain environment variables having certain values?
    – Kusalananda
    Sep 7 at 19:46











  • @Kusalananda - no, each one stands for itself. I did not find any error posted ( as said )
    – Guy . D
    Sep 7 at 19:48










  • Answered in :enter link description here
    – Guy . D
    Sep 8 at 11:12














up vote
-2
down vote

favorite
1












I'm using RPI zerow to run a python code (Raspbian Strech-Lite).



I need to run 2 file during startup: MQTTlogger.py and Sched_MQTT.py.
Both files run OK using command line, but when I added 2 job on crontab only one of them get executed ( the second file ). file name and path are OK.



Searching in all log files in /var/log did not show aany evidence for failure during boot.



Any idea why ?



@reboot /usr/bin/python3 /home/guy/github/SmartHome_SoC/MQTTswitches/MQTTlogger.py
@reboot /usr/bin/python3 /home/guy/github/SmartHome_SoC/MQTTswitches/Sched_MQTT.py



EDIT1: running only MQTTLOGGER.PY




leaving only MQTTlogger.py in crontab shows that it is a problem with running in crontab rather than running one after the other.



what can be the reason for file fail to run on a crontab job, but succeed when executed on a command line ? how can I debug it ?




EDIT2 : trying to catch crontab error




@reboot /usr/bin/python3 /home/guy/github/SmartHome_SoC/MQTTswitches/MQTTlogger.py >> /home/guy/crontab.log 2>&1


yields :



File "/home/guy/github/SmartHome_SoC/MQTTswitches/MQTTlogger.py", line 1, in <module>
import paho.mqtt.client as mqtt
ImportError: No module named 'paho'


so I'm guessing it has something to do with path to the MQTT package










share|improve this question















closed as too broad by Rui F Ribeiro, RalfFriedl, John WH Smith, schily, thrig Sep 9 at 15:14


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • Does one have to run before the other? Cron will run them pretty much simultaneously. Did you get any form of error mailed to you (or to the user whose crontab this is)? Do they depend on certain environment variables having certain values?
    – Kusalananda
    Sep 7 at 19:46











  • @Kusalananda - no, each one stands for itself. I did not find any error posted ( as said )
    – Guy . D
    Sep 7 at 19:48










  • Answered in :enter link description here
    – Guy . D
    Sep 8 at 11:12












up vote
-2
down vote

favorite
1









up vote
-2
down vote

favorite
1






1





I'm using RPI zerow to run a python code (Raspbian Strech-Lite).



I need to run 2 file during startup: MQTTlogger.py and Sched_MQTT.py.
Both files run OK using command line, but when I added 2 job on crontab only one of them get executed ( the second file ). file name and path are OK.



Searching in all log files in /var/log did not show aany evidence for failure during boot.



Any idea why ?



@reboot /usr/bin/python3 /home/guy/github/SmartHome_SoC/MQTTswitches/MQTTlogger.py
@reboot /usr/bin/python3 /home/guy/github/SmartHome_SoC/MQTTswitches/Sched_MQTT.py



EDIT1: running only MQTTLOGGER.PY




leaving only MQTTlogger.py in crontab shows that it is a problem with running in crontab rather than running one after the other.



what can be the reason for file fail to run on a crontab job, but succeed when executed on a command line ? how can I debug it ?




EDIT2 : trying to catch crontab error




@reboot /usr/bin/python3 /home/guy/github/SmartHome_SoC/MQTTswitches/MQTTlogger.py >> /home/guy/crontab.log 2>&1


yields :



File "/home/guy/github/SmartHome_SoC/MQTTswitches/MQTTlogger.py", line 1, in <module>
import paho.mqtt.client as mqtt
ImportError: No module named 'paho'


so I'm guessing it has something to do with path to the MQTT package










share|improve this question















I'm using RPI zerow to run a python code (Raspbian Strech-Lite).



I need to run 2 file during startup: MQTTlogger.py and Sched_MQTT.py.
Both files run OK using command line, but when I added 2 job on crontab only one of them get executed ( the second file ). file name and path are OK.



Searching in all log files in /var/log did not show aany evidence for failure during boot.



Any idea why ?



@reboot /usr/bin/python3 /home/guy/github/SmartHome_SoC/MQTTswitches/MQTTlogger.py
@reboot /usr/bin/python3 /home/guy/github/SmartHome_SoC/MQTTswitches/Sched_MQTT.py



EDIT1: running only MQTTLOGGER.PY




leaving only MQTTlogger.py in crontab shows that it is a problem with running in crontab rather than running one after the other.



what can be the reason for file fail to run on a crontab job, but succeed when executed on a command line ? how can I debug it ?




EDIT2 : trying to catch crontab error




@reboot /usr/bin/python3 /home/guy/github/SmartHome_SoC/MQTTswitches/MQTTlogger.py >> /home/guy/crontab.log 2>&1


yields :



File "/home/guy/github/SmartHome_SoC/MQTTswitches/MQTTlogger.py", line 1, in <module>
import paho.mqtt.client as mqtt
ImportError: No module named 'paho'


so I'm guessing it has something to do with path to the MQTT package







cron






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 8 at 9:54









Kusalananda

107k14209331




107k14209331










asked Sep 7 at 18:54









Guy . D

1053




1053




closed as too broad by Rui F Ribeiro, RalfFriedl, John WH Smith, schily, thrig Sep 9 at 15:14


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






closed as too broad by Rui F Ribeiro, RalfFriedl, John WH Smith, schily, thrig Sep 9 at 15:14


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.













  • Does one have to run before the other? Cron will run them pretty much simultaneously. Did you get any form of error mailed to you (or to the user whose crontab this is)? Do they depend on certain environment variables having certain values?
    – Kusalananda
    Sep 7 at 19:46











  • @Kusalananda - no, each one stands for itself. I did not find any error posted ( as said )
    – Guy . D
    Sep 7 at 19:48










  • Answered in :enter link description here
    – Guy . D
    Sep 8 at 11:12
















  • Does one have to run before the other? Cron will run them pretty much simultaneously. Did you get any form of error mailed to you (or to the user whose crontab this is)? Do they depend on certain environment variables having certain values?
    – Kusalananda
    Sep 7 at 19:46











  • @Kusalananda - no, each one stands for itself. I did not find any error posted ( as said )
    – Guy . D
    Sep 7 at 19:48










  • Answered in :enter link description here
    – Guy . D
    Sep 8 at 11:12















Does one have to run before the other? Cron will run them pretty much simultaneously. Did you get any form of error mailed to you (or to the user whose crontab this is)? Do they depend on certain environment variables having certain values?
– Kusalananda
Sep 7 at 19:46





Does one have to run before the other? Cron will run them pretty much simultaneously. Did you get any form of error mailed to you (or to the user whose crontab this is)? Do they depend on certain environment variables having certain values?
– Kusalananda
Sep 7 at 19:46













@Kusalananda - no, each one stands for itself. I did not find any error posted ( as said )
– Guy . D
Sep 7 at 19:48




@Kusalananda - no, each one stands for itself. I did not find any error posted ( as said )
– Guy . D
Sep 7 at 19:48












Answered in :enter link description here
– Guy . D
Sep 8 at 11:12




Answered in :enter link description here
– Guy . D
Sep 8 at 11:12










2 Answers
2






active

oldest

votes

















up vote
1
down vote













Most likely the issue is that cron is trying to run these jobs (one or both) before a service has become available. The easy way to fix this is to add a sleep delay to wait for the required services to get started:



@reboot sleep 10; /usr/bin/python3 /home/guy/github/SmartHome_SoC/MQTTswitches/MQTTlogger.py && /usr/bin/python3 /home/guy/github/SmartHome_SoC/MQTTswitches/Sched_MQTT.py


Your issue is similar to the one in this question, so you may wish to refer to it for additional information.



You may also want to redirect your stderr to a file to capture any output for debugging purposes. Do that by adding this to the and of the crontab line I've listed above:



> ~/cronjoblog 2>&1






share|improve this answer




















  • thank you for your answer. I'm changing my original question - since trying to run only MQTTlogger.py fails. so it has nothing to do with the fact of sleep period between - BUT can you explain more about stderr and how to redirect errors to a file
    – Guy . D
    Sep 8 at 3:57










  • Did you try running with sleep?
    – Seamus
    Sep 8 at 13:23










  • Yes. See edit2- it was a path issue. 🙏🏾
    – Guy . D
    Sep 8 at 13:59










  • So - all is well then... your script runs??
    – Seamus
    Sep 8 at 14:02










  • yes, by the way- since MQTT needs some time until network is UP, sleep was necessary!
    – Guy . D
    Sep 8 at 14:06

















up vote
0
down vote













two things that solved my problem:



1 ) adding sleep to crontab- as Seamus suggested:



@reboot sleep 10; /usr/bin/python3 /home/guy/github/SmartHome_SoC/MQTTswitches/MQTTlogger.py >> /home/guy/crontab.log 2>&1`


2) fixing path inside code to fix path ( as asked in stackover flow ), reffer






share|improve this answer



























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    Most likely the issue is that cron is trying to run these jobs (one or both) before a service has become available. The easy way to fix this is to add a sleep delay to wait for the required services to get started:



    @reboot sleep 10; /usr/bin/python3 /home/guy/github/SmartHome_SoC/MQTTswitches/MQTTlogger.py && /usr/bin/python3 /home/guy/github/SmartHome_SoC/MQTTswitches/Sched_MQTT.py


    Your issue is similar to the one in this question, so you may wish to refer to it for additional information.



    You may also want to redirect your stderr to a file to capture any output for debugging purposes. Do that by adding this to the and of the crontab line I've listed above:



    > ~/cronjoblog 2>&1






    share|improve this answer




















    • thank you for your answer. I'm changing my original question - since trying to run only MQTTlogger.py fails. so it has nothing to do with the fact of sleep period between - BUT can you explain more about stderr and how to redirect errors to a file
      – Guy . D
      Sep 8 at 3:57










    • Did you try running with sleep?
      – Seamus
      Sep 8 at 13:23










    • Yes. See edit2- it was a path issue. 🙏🏾
      – Guy . D
      Sep 8 at 13:59










    • So - all is well then... your script runs??
      – Seamus
      Sep 8 at 14:02










    • yes, by the way- since MQTT needs some time until network is UP, sleep was necessary!
      – Guy . D
      Sep 8 at 14:06














    up vote
    1
    down vote













    Most likely the issue is that cron is trying to run these jobs (one or both) before a service has become available. The easy way to fix this is to add a sleep delay to wait for the required services to get started:



    @reboot sleep 10; /usr/bin/python3 /home/guy/github/SmartHome_SoC/MQTTswitches/MQTTlogger.py && /usr/bin/python3 /home/guy/github/SmartHome_SoC/MQTTswitches/Sched_MQTT.py


    Your issue is similar to the one in this question, so you may wish to refer to it for additional information.



    You may also want to redirect your stderr to a file to capture any output for debugging purposes. Do that by adding this to the and of the crontab line I've listed above:



    > ~/cronjoblog 2>&1






    share|improve this answer




















    • thank you for your answer. I'm changing my original question - since trying to run only MQTTlogger.py fails. so it has nothing to do with the fact of sleep period between - BUT can you explain more about stderr and how to redirect errors to a file
      – Guy . D
      Sep 8 at 3:57










    • Did you try running with sleep?
      – Seamus
      Sep 8 at 13:23










    • Yes. See edit2- it was a path issue. 🙏🏾
      – Guy . D
      Sep 8 at 13:59










    • So - all is well then... your script runs??
      – Seamus
      Sep 8 at 14:02










    • yes, by the way- since MQTT needs some time until network is UP, sleep was necessary!
      – Guy . D
      Sep 8 at 14:06












    up vote
    1
    down vote










    up vote
    1
    down vote









    Most likely the issue is that cron is trying to run these jobs (one or both) before a service has become available. The easy way to fix this is to add a sleep delay to wait for the required services to get started:



    @reboot sleep 10; /usr/bin/python3 /home/guy/github/SmartHome_SoC/MQTTswitches/MQTTlogger.py && /usr/bin/python3 /home/guy/github/SmartHome_SoC/MQTTswitches/Sched_MQTT.py


    Your issue is similar to the one in this question, so you may wish to refer to it for additional information.



    You may also want to redirect your stderr to a file to capture any output for debugging purposes. Do that by adding this to the and of the crontab line I've listed above:



    > ~/cronjoblog 2>&1






    share|improve this answer












    Most likely the issue is that cron is trying to run these jobs (one or both) before a service has become available. The easy way to fix this is to add a sleep delay to wait for the required services to get started:



    @reboot sleep 10; /usr/bin/python3 /home/guy/github/SmartHome_SoC/MQTTswitches/MQTTlogger.py && /usr/bin/python3 /home/guy/github/SmartHome_SoC/MQTTswitches/Sched_MQTT.py


    Your issue is similar to the one in this question, so you may wish to refer to it for additional information.



    You may also want to redirect your stderr to a file to capture any output for debugging purposes. Do that by adding this to the and of the crontab line I've listed above:



    > ~/cronjoblog 2>&1







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Sep 8 at 2:10









    Seamus

    22610




    22610











    • thank you for your answer. I'm changing my original question - since trying to run only MQTTlogger.py fails. so it has nothing to do with the fact of sleep period between - BUT can you explain more about stderr and how to redirect errors to a file
      – Guy . D
      Sep 8 at 3:57










    • Did you try running with sleep?
      – Seamus
      Sep 8 at 13:23










    • Yes. See edit2- it was a path issue. 🙏🏾
      – Guy . D
      Sep 8 at 13:59










    • So - all is well then... your script runs??
      – Seamus
      Sep 8 at 14:02










    • yes, by the way- since MQTT needs some time until network is UP, sleep was necessary!
      – Guy . D
      Sep 8 at 14:06
















    • thank you for your answer. I'm changing my original question - since trying to run only MQTTlogger.py fails. so it has nothing to do with the fact of sleep period between - BUT can you explain more about stderr and how to redirect errors to a file
      – Guy . D
      Sep 8 at 3:57










    • Did you try running with sleep?
      – Seamus
      Sep 8 at 13:23










    • Yes. See edit2- it was a path issue. 🙏🏾
      – Guy . D
      Sep 8 at 13:59










    • So - all is well then... your script runs??
      – Seamus
      Sep 8 at 14:02










    • yes, by the way- since MQTT needs some time until network is UP, sleep was necessary!
      – Guy . D
      Sep 8 at 14:06















    thank you for your answer. I'm changing my original question - since trying to run only MQTTlogger.py fails. so it has nothing to do with the fact of sleep period between - BUT can you explain more about stderr and how to redirect errors to a file
    – Guy . D
    Sep 8 at 3:57




    thank you for your answer. I'm changing my original question - since trying to run only MQTTlogger.py fails. so it has nothing to do with the fact of sleep period between - BUT can you explain more about stderr and how to redirect errors to a file
    – Guy . D
    Sep 8 at 3:57












    Did you try running with sleep?
    – Seamus
    Sep 8 at 13:23




    Did you try running with sleep?
    – Seamus
    Sep 8 at 13:23












    Yes. See edit2- it was a path issue. 🙏🏾
    – Guy . D
    Sep 8 at 13:59




    Yes. See edit2- it was a path issue. 🙏🏾
    – Guy . D
    Sep 8 at 13:59












    So - all is well then... your script runs??
    – Seamus
    Sep 8 at 14:02




    So - all is well then... your script runs??
    – Seamus
    Sep 8 at 14:02












    yes, by the way- since MQTT needs some time until network is UP, sleep was necessary!
    – Guy . D
    Sep 8 at 14:06




    yes, by the way- since MQTT needs some time until network is UP, sleep was necessary!
    – Guy . D
    Sep 8 at 14:06












    up vote
    0
    down vote













    two things that solved my problem:



    1 ) adding sleep to crontab- as Seamus suggested:



    @reboot sleep 10; /usr/bin/python3 /home/guy/github/SmartHome_SoC/MQTTswitches/MQTTlogger.py >> /home/guy/crontab.log 2>&1`


    2) fixing path inside code to fix path ( as asked in stackover flow ), reffer






    share|improve this answer
























      up vote
      0
      down vote













      two things that solved my problem:



      1 ) adding sleep to crontab- as Seamus suggested:



      @reboot sleep 10; /usr/bin/python3 /home/guy/github/SmartHome_SoC/MQTTswitches/MQTTlogger.py >> /home/guy/crontab.log 2>&1`


      2) fixing path inside code to fix path ( as asked in stackover flow ), reffer






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        two things that solved my problem:



        1 ) adding sleep to crontab- as Seamus suggested:



        @reboot sleep 10; /usr/bin/python3 /home/guy/github/SmartHome_SoC/MQTTswitches/MQTTlogger.py >> /home/guy/crontab.log 2>&1`


        2) fixing path inside code to fix path ( as asked in stackover flow ), reffer






        share|improve this answer












        two things that solved my problem:



        1 ) adding sleep to crontab- as Seamus suggested:



        @reboot sleep 10; /usr/bin/python3 /home/guy/github/SmartHome_SoC/MQTTswitches/MQTTlogger.py >> /home/guy/crontab.log 2>&1`


        2) fixing path inside code to fix path ( as asked in stackover flow ), reffer







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Sep 8 at 14:09









        Guy . D

        1053




        1053












            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)