Issue
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
Solution
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:
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
Alternatively you can also add a PATH value to the crontab with
PATH=/usr/bin
.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.
Answered By - isopach Answer Checked By - Katrina (WPSolving Volunteer)