-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

closed as too broad by Rui F Ribeiro, RalfFriedl, John WH Smith, schily, thrig Sep 9 '18 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 '18 at 19:46
  • @Kusalananda - no, each one stands for itself. I did not find any error posted ( as said ) – Guy . D Sep 7 '18 at 19:48
  • Answered in :enter link description here – Guy . D Sep 8 '18 at 11:12
1

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

  • 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 '18 at 3:57
  • Did you try running with sleep? – Seamus Sep 8 '18 at 13:23
  • Yes. See edit2- it was a path issue. 🙏🏾 – Guy . D Sep 8 '18 at 13:59
  • So - all is well then... your script runs?? – Seamus Sep 8 '18 at 14:02
  • yes, by the way- since MQTT needs some time until network is UP, sleep was necessary! – Guy . D Sep 8 '18 at 14:06
0

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.