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.
Related
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
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.
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
So I have a program that I want to autostart in Raspberry Pi. My program is supposed to grab some api-info online and then display it on a little screen. I've added these lines to rc.local:
sudo python ./home/pi/Documents/Skanetrafiken_projekt/testStart.py &
sudo python ./home/pi/Documents/Skanetrafiken_projekt/main.py &
The testStart.py just tests the display and it works fine, the screen lights up when the Raspberry Pi boots up. So that works. The main.py won't work at all however. In the beginning of the main code I even put the same code as in testStart.py, just to see if the display lights up, but it doesn't. So that is super weird to me.
Could it be something about that the main.py will connect to internet? I tried setting "Waiting of network to boot" in the raspi-config settings, but that didn't help.
The main works fine when I run in manually. I also tried to start with cron, but that didn't work. I don't have that much experience.
And ideas?
try using sudo crontab -e and then adding #reboot sudo python /path/to/your/script.py this then should run your script every time you boot up.
I tested running the script from the prompt rather than directly from the python script, and I guess the prompt uses another compilator or something because now I got many errors that I didn't get before. Including that I have to include the line
# -*- coding: utf-8 -*-
for it to understand my comments. So now it works anyway.
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.