Schedule script in python - python

I am looking to schedule a code on my raspberry pi zero. The task would be to run a .py script to a pet feeder 3 times a day on specific times based on the system clock. I have tried to search it on the internet but the results kept on coming back for windows 7 task scheduler, however I need to run it on Raspbian.
Any suggestion will be appreciated.
Thanks

Try the schedule package:
https://pypi.python.org/pypi/schedule
It had worked for me in raspberry pi.

cron works well to schedule the python script to run at certain times. It's much simpler than using the python schedule package.
Firstly, make sure your python script starts with a shebang
#!/usr/bin/env python
and is executable
$sudo chmod 755 ./my-python-script.py
Then, in command line on the pi. Open a new cron tab
$sudo crontab -e
(you may also need to select an editor, nano is the easiest)
and schedule your script to run by adding the follow to the last line of the crontab (this will run every 8 hours)
0 */8 * * * /path/to/my-python-script.py
or if you would like to log the output, use
0 */8 * * * /path/to/my-python-script.py > /home/pi/Desktop/my_script_log.txt
Lastly, save and exit from nano (cntl + x) and restart your Pi.
If you are having issues, check the cron sys log
$grep cron /var/log/syslog
Use this site for working out the crontab interval syntax - https://crontab.guru/

Okay so Rc.local didnt work, Ive tried everything, however Cron worked to autostart the launcher.sh.
Now, I need a second thing, where the scrip runs everyday 3 times in specific times, however the schedule 0.5.0 does not seem to work.
I have pip version 10.0.0 then I have managed to install schedule in the terminal, but when I try to run my script it comes back with an error, that the import schedule cant be found.
Do anyone know a solution for this?
import schedule error
link
Thanks

Related

Raspberry pi 3 crontab doens't work without log

I already found a lot of posts with crontab errors but i still can't figure out where is my issu.
I want to start a python script on /home/pi/somedir/main.py and to fix any relative path issu in my script i made a start.sh next to it containing
cd /home/pi/somedir
/usr/bin/python3.7 main.py
I made it executable with sudo chmod -x start.sh and it works find went i do /home/pi/somedir/start.sh manually, but i want to autostart it at start.
I tried multiple ways (like /etc/rc.local) and it never worked. As crontab seems to be the better way to do this, i tried crontab -e (the crontab for the pi user so) and i added
#reboot bash /home/pi/somedir/start.sh > /home/pi/somedir/logstart.txt &
to have the logs and to execute the script without blocking the rest of the system. But the script doesn't start and there is nothing in the log file.
The script is a python3 discord bot with logging output, running just find when launched manually. It is a raspberry pi 3B+, with a fresh install of raspberry pi os desktop and i have nothing else running on it.
Thanks for any help and sorry if my english is not correct !
ps: If you know better options to host python3 scripts like discord bots on raspberry pi it would also be usefull of course, i am clearly a beginner !
The issu was that the script won't start at reboot as it is before the system is fully booted. One solution was to start the process at precise hour when the system is fully start, like every morning. An other would be to add a delay before running the start.sh script, somethings like
#reboot sleep 60 && /home/pi/somedir/start.sh > /home/pi/somedir/logstart.txt &
works for me aswell.

How to run a python script with cron on LINUX

I'm trying to run a python script on my raspberrypi using cron.
I did the following:
crontab -e # To edit a crontab job
After the cron file opened, I added the following line:
#reboot /usr/bin/python /home/pi/path/to/file/example.py > /home/pi/cronlogs/mylog.log # JOB_ID_!
If I understand the documentation correctly, this cron job should be executed every time after the system booted up. However in my case, when I reboot the computer, the script will not be executed.
What's strange:
I checked the log file and it's empty, so it seems like everything goes fine
If I run the given command manually (so basically write the following code to the terminal) it executes and works correctly: /usr/bin/python /home/pi/path/to/file/example.py > /home/pi/cronlogs/mylog.log
I guess I missed something really obvious but I can't see it. Please can I ask for any advise how to debug this. Thanks!
The cron definition looks correct; I just checked this on my Pi running Debian stretch and it works OK:
#reboot /usr/bin/python /home/pi/example.py > /home/pi/mylog.log
Some other possible reasons it might not work:
working directory issue (if you're using relative paths)
a long running script (being a scraping script it might take a while to complete) - you can check if it's still running using ps aux | grep python
the script does not output anything (would need some more details about the script)
Just to be sure you catch any errors from the script, redirect stderr to stdout by using 2>&1

Cronjob on Ubuntu Mate for Raspberry stops right after execution

I use a RaspberryPi 3 with UbuntuMate 16.04. On it, I want to start a little Python (3.5) program every midnight. For that I call a little shell script, so that I can change into the wanted directory comfortably.
crontab:
5 0 * * * /path/to/script/start.sh
start.sh (yes, it's executable):
#!/bin/bash
cd /path/to/wanted/workingDir/
python3.5 ControllerQueue.py
#also tried: python3.5 ControllerQueue.py &
Now if I execute the programm or script from the terminal, everything runs fine. But if I use the crontab it starts the script and stops right after. I also tried running the programm directly but get the same outcome. The paths are correct as I copied the workingDir path from the crontab-file and started it via the terminal.
Is there something I overlook?
As stofvl suggested, I saved the error output of my shell script. It turns out that I needed to add a display. My programm is divided into two scripts. One which provides a GUI and the other main application. The script only starts the main application, without the GUI, but it seems that this didn't matter.
This discussion helped me solve the problem.

Why is my Python script running twice in the background when I try to execute it at startup of my Raspberry Pi by adding the command to /etc/profile?

Why is my Python script running twice in the background when I try to execute it at startup of my Raspberry Pi by adding the command to /etc/profile?
I have a command written at the end of the file /etc/profile for a Python script to run at startup of my Raspberry Pi, "sudo python /path/filename.py &", and for some reason it runs twice, every time. When I comment the line out and execute it manually from the command line it runs normally. Why does that happen and what can I do to prevent that from happening?
I know for fact that it is running twice in the background because in my code I have a buzzer that beeps twice at times and 3 times at others, and it beeps 4 times instead of 2 and 6 times instead of 3. Also the code ends up contradicting itself, clearly because each script run is trying to do something else at the same time.
Thanks in advance.
I'm answering my own question with a better method for running scripts at boot/startup.
I'm not exactly sure why this was happening, but I did learn that executing scripts on startup with this method is a bad practice and is best avoided.
I started using the Crontab instead.
This is what you need to do:
crontab -e
This opens up the crontab, then add the following line:
#reboot python /filelocation/filename.py
This will execute the script as soon as the Pi boots up.
No more double script runs!
Do you have VNC enabled? I think that's the problem. It was for me.
See this related discussion:
https://www.raspberrypi.org/forums/viewtopic.php?f=66&t=59285
So you can disable VNC, do the run levels danny suggested, create a new user (different to the one used for VNC) or start doing this sort of stuff in your script:
if [ x"$ALREADY_DONE" == x"" ]; then
export ALREADY_DONE=yes
foobar
fi

Raspberry Pi Auto Starting a program

So I have a python game (PyGame) running on a raspberry Pi.
I have followed the instructions found on many sites for getting the Raspberry Pi to auto login (those all work), auto run startx, but I'm stuck on getting my program to run once the GUI loads.
Many people (here on StackOverflow and other places) point to this presentation here:
http://www.slideshare.net/SeggySegaran/raspberry-pi-autostarting-a-python-program
I've tried both ways of doing it (putting the desktop file in autostart or putting the command in rc.local
I have opened up a Terminal window and copy / pasted the command to verify there are no typos and the code will run......
sudo python /home/pi/valley.py
and it will run. Is there a way to see a log to find out WHY the program doesn't launch? Is there a better way to get done what I want to get done?
I've got my python script to run at startup doing this:
sudo nano /etc/xdg/lxsession/LXDE/autostart
This will allow you to add an element to run when the LXDE desktop session begins (the raspian default GUI if setup to do from raspi-config)
It will probably have entries like these:
#lxpanel --profile LXDE
#pcmanfm --desktop --profile LXDE
#xscreensaver -no-splash
It's just a matter of adding your script there as well
#lxpanel --profile LXDE
#pcmanfm --desktop --profile LXDE
#xscreensaver -no-splash
#python /home/pi/yourAwesomePyScriptHere.py
If your python script uses GPIO, you need to run that as root (using sudo):
#sudo python /home/pi/yourGPIOScript.py
One thing I do want to point out: always test your script before hand.
I mean, run with the absolute path, make sure it still works, try to break it, make sure it's as robust as it can be. If there are errors in your script and you place it at start up you won't see those in a terminal window, but you will hog the cpu with python stuck in a loop at startup.
Also check out this answer on the RPi exchange
You can achieve this in two ways:
1). Using LXDE autostart.
2). As a service via init.d.
If you are starting X with "startx", you can also just stick your game in your .xinitrc. If your game binary is called "game" and is in your path, just do this:
echo "game" >> ~/.xinitrc
This works for other commands. Add a "&" if you want the command to keep running in the background.
This is how I start my window manager, load my wallpaper, start a compositor, etc. It is stupid simple, easy to change later, and can do anything you can do at a terminal prompt.
You can run your script automatically on startup of raspberry by using crontab.
Crontab is table that list all command to perform on scheduled time.
First, you need to edit crontab by using:
sudo crontab -e
And after this, add following line:
#reboot python path-of-your-script & (& should be at the end of line that means command will execute in background).
Save your script and reboot your system. When your system will start, your script will run automatically.

Categories