0

Good morning everyone,

I'm doing a Telegram Bot using Telegram API (I'm just using requests(http://api.telegram.org/sendMessage.....) and it's working fine when I manually execute my script with python myscript.py).

I want to let the script run all the time so I went to the crontab of my raspberry and add @reboot python /home/pi/myscript.py. Then I did sudo reboot and no message on telegram... I have no idea why it is not working, do you have the same issue?

Thanks

Boröom
  • 15
  • 1
  • 4
  • maybe you could find some hints here: https://unix.stackexchange.com/questions/109804/crontabs-reboot-only-works-for-root – zufall Sep 09 '21 at 17:33

1 Answers1

1

cron may not know where to find the Python interpreter because it doesn't share your user account's environment variables.

There are 3 solutions to this:

  1. If Python is at /usr/bin/python, you can change the cron job to use an absolute path: @reboot /usr/bin/python /home/pi/myscript.py

  2. Alternatively you can also add a PATH value to the crontab with PATH=/usr/bin.

  3. Another solution would be to specify an interpreter in the script file, make it executable, and call the script itself in your crontab:

    a. Put shebang at the top of your python file: #!/usr/bin/python.

    b. Set it to executable: $ chmod +x /home/pi/myscript.py

    c. Put it in crontab: @reboot /home/pi/myscript.py

Adjust the path to the Python interpreter if it's different on your system.

Reference

isopach
  • 1,783
  • 7
  • 31
  • 43