A short video on how to use 'crontab' to automatically start a Python program as your Raspberry Pi boots up. Great for headless projects and no screen.
Пікірлер: 182
@evancurry57965 жыл бұрын
cannot thank you enough, new to the raspberry pi scene, used this so my pi starts up a fantasy console program to use for an arcade cabinet me and a team built for a capstone project, youre a savior
@mathewgriffin47214 жыл бұрын
Thank you so much! I've been trying similar processes all day and this is the only one that worked.
@TeCoEd4 жыл бұрын
Glad that you found it useful.
@asici25 жыл бұрын
Thanks buddy, this was what i was looking for, even the command to boot from the command line. love it. thanks
@TeCoEd5 жыл бұрын
You are welcome.
@yoko31735 жыл бұрын
WOW ! A perfect and simple tutorial ! You helped me solve my problem, thanks a lot :D
@DaveCapamagian6 жыл бұрын
Awesome easy to understand, just what I needed. Thanks heaps
@thesiebi58545 жыл бұрын
Great tutorial, thank you very much! I was struggling with an ImportError, but the problem was that I installed my modules with "pip3 install x" and not "sudo pip3 install x", since with sudo it installs it for every user, not just for the "pi" user.
@alexvong14 жыл бұрын
Thanks for the tutorial, helped me out a lot!
@TeCoEd4 жыл бұрын
You are welcome
@jimlloydpesidas77415 жыл бұрын
thanks im going to try this
@freaker1266 жыл бұрын
pretty cool mate.
@awia12315 жыл бұрын
Thanks for the awesome video to help. spent hours trying to get autoboot scripts to work but the web was no help as no one said about crontabs
@TeCoEd5 жыл бұрын
Glad it was helpful.
@emeraldthunder3 жыл бұрын
Thank you for this. I am making a no screen device and need this to run the python master program.
@TeCoEd3 жыл бұрын
You are welcome. Hope it goes well.
@faiqahmadkhan60095 жыл бұрын
great,, good work
@no2sh5 жыл бұрын
Thanks a lot .. this works fine :)
@Sebastian-wq8xu6 жыл бұрын
Ehrenmann Love this
@amanpreetsingh7905 жыл бұрын
Thanks a Lot
@julien80973 жыл бұрын
thanks !
@moondemudimba79204 жыл бұрын
Awesome
@dean60464 жыл бұрын
Thank you!
@TeCoEd4 жыл бұрын
Welcome!
@PilotOdyssey4 жыл бұрын
I can see you, I can't see the commands your typing , can you post the commands below so we can cut and paste? Future videos in Terminal go to Edit / Preferences then click the Text tab and change the Font Size to 15-16 Thanks.
@rgmtb5 жыл бұрын
Really like this method. This method looks like it will allow the Pi to boot even if there is an issue with the script. Plus, the extra time delay at the top of your script is a great idea. So I'm guessing that you just add that time delay before the meat of your program kicks in correct? Also, am I correct in thinking that this method will allow the Pi to boot up even if there is an issue with the script? I'm just wondering what the boot order is? This is very helpful!
@TeCoEd5 жыл бұрын
Thanks. Yes you are correct. The Pi still boots even if the Python script is wrong. The delay is just to allow the hardware to connect to say BT or WiFi before the program runs.
@vortex89884 жыл бұрын
Hey man! Thanks for the fantastic tutorial. I was wondering how you saved it with control x. For me, the command console won’t don anything
@TeCoEd4 жыл бұрын
Glad that it was helpful.
@bennedictbyy4 жыл бұрын
Well explained tutorial. Thank you! However, is it possible to activate a virtual environment as well? I have tried activating my code using Crontab, shell script and systemd service, but it seem to but not working. Is there a way to do so? Thank you.
@TeCoEd4 жыл бұрын
Yes, you can, but I think you have to write a bash script that runs when you boot up and then triggers the virtual environment to run.
@ayeyefookinw0tm8595 жыл бұрын
I get so much anxiety when after doing ls doesnt do clear
@momorutkin7815 жыл бұрын
Thank you so much for this video! I noticed that when I go to "boot options" sudo raspi-config, I don't have the option for console autologin, would you have any idea why this is? It only shows me the option for Desktop/CLI, Wait for network at boot, and spash screen
@TeCoEd5 жыл бұрын
When version of the OS are you using? Try the latest that was released this month.
@TeCoEd5 жыл бұрын
"What version"
@momorutkin7815 жыл бұрын
TeCoEd I tried the update option in raspi-config but it still didn’t give me the option. I think I have version 9
@MrRHCPfan845 жыл бұрын
@@momorutkin781 Choose option 1 for desktop/cli Its in there pal.
@gabrielandrade52204 жыл бұрын
@@MrRHCPfan84 Thank you!
@codcompcentral5 жыл бұрын
Hi, the program is running although I have main.py which refers to camera.py (working) although mail.py which sends an email is not working i think since it is not sending an email. When I run manually via Terminal an email is sent. Do you know what might be the problem? The video is 10/10
@TeCoEd5 жыл бұрын
If it runs in the terminal you may need to use sudo in the crontab, @reboot sudo python3. ... Etc. Also check that the email library is for Py2 or 3 depending on what you are using.
@ranjanpal72173 жыл бұрын
Amazing Explanation...please let me know, once our job is done i.e. running program at when Pi boots, how do we exit from the crontab?
@TeCoEd3 жыл бұрын
If you add an & to the end of the crontab code it runs in the background. Is this what you meant?
@TeCoEd3 жыл бұрын
The crontab runs the program in the background.
@henriklauridsen96866 жыл бұрын
Thank you for sharing. How do I set the delay before starting my Python script? Tia, Henrik
@TeCoEd6 жыл бұрын
Henrik Lauridsen add the delay at the start of the Python code. time.sleep(5) for a 5 second delay.
@alkhashtee6 жыл бұрын
Thank you so much for this useful video. But is there any way to add it on Linux operating system? Just wondering
@TeCoEd6 жыл бұрын
Mohanad Alkhashtee crontab will work on most Linux OS.
@henriklauridsen96866 жыл бұрын
Thanks a lot and thank you for this great tutorial
@jacktheorigamimaster76895 жыл бұрын
Thx
@Granef5 жыл бұрын
Can you use vnc in conjunction with this? i would like to access my pi wirelessly but i also want it to run a python program when booted
@TeCoEd5 жыл бұрын
Yes you can use VNC still. Just enable it before boot up.
@Granef5 жыл бұрын
@@TeCoEd wow thanks!
@jayralph20674 жыл бұрын
your pi and pc must be the same wifi connection
@tahreemkhan24865 жыл бұрын
Hi! is there a way I could run two scripts in parallel on boot?
@TeCoEd5 жыл бұрын
Just add another script line under the other and it will run it.
@vijivijay24475 жыл бұрын
And also can you explain how do we do the reverse way i.e removing the python file which is running during boot and bring it to normal boot i.e desktop mode
@TeCoEd5 жыл бұрын
Follow the same instructions but comment out the boot up code. Then save the crontab.
@vijivijay24475 жыл бұрын
@@TeCoEd but that .py file will be running infinitely right,how can we open the terminal from there itself Suppose I have a py script to open the pi camera at boot itself and that will be running infinitely , whenever I power on the pi. so what commands are required to open the terminal and do the necessary changes.
@shreevlogs88535 жыл бұрын
@@vijivijay2447 did u get any solution?
@vinaym97002 жыл бұрын
Same problem bro how to stop that program from execution
@mrstreamtechsolutions66485 жыл бұрын
Thank you greate tutorial. Iam trying to play stream continusly on boot and refresh automatically Please sugest me some scripts Please help me
@TeCoEd5 жыл бұрын
Stream or Steam?
@mrstreamtechsolutions66485 жыл бұрын
@@TeCoEd stream
@md.emadulhasan11974 жыл бұрын
Hey is it possible to run the python file with the command line in this way when RPI3 booted?
@TeCoEd4 жыл бұрын
Yes you can boot to command line and the Python program will run. But, if you need graphics etc. then you need to boot to GUI.
@jakkidudu3 жыл бұрын
hello and thanks for the tutorial. i wrote a phyton script @reboot in crontab and after the reboot was not listed anymore in there while the script now still runs everytime i boot the RPi. any idea? where is this script listed?
@TeCoEd3 жыл бұрын
Did you use crontab or crontab e also sudo can change the access level.
@jakkidudu3 жыл бұрын
@@TeCoEd i was accessing crontab -e without sudo . now is there. thanks a million
@marcelverhoeven35144 жыл бұрын
I have created a kind of MENU program which I would like to start up at reboot and it should be shown on the RPi-Screen, Is that possible???
@TeCoEd4 жыл бұрын
Yes this is possible however, some programs required to boot to desktop to run, so PyGame needs a desktop and then it runs the program automatically. What did your build the Menu in?
@stashedup74155 жыл бұрын
How to reverse this? my VNC cant connect back in my laptop help
@alexvoicu33875 жыл бұрын
Use SSH connection, same problem. : ssh IP_ADDRESS -l pi
@pablorivero68735 жыл бұрын
Hey and now that u don t have a desktop GUI, how do you manage to change this settings?
@TeCoEd5 жыл бұрын
VNC into the command line or try SSH.
@engineersproblems56465 жыл бұрын
Did you manage GUI.?
@TeCoEd5 жыл бұрын
@@engineersproblems5646 go into the raspberry pi config and set the GUI to on then reboot.
@UnexpectedDanger2 жыл бұрын
I'm trying to set up my pi to automatically play music when it's rebooted or powered on... Would it be possible to have it pick a different playlist, for example for December have it play Christmas music?
@TeCoEd2 жыл бұрын
Yes. Use the Python 'time' module to return the month and then use an IF statement to check "IF the month is December: play December playlist". Sounds like a good project.
@UnexpectedDanger2 жыл бұрын
@@TeCoEd I'm still learning, it's been difficult. I'm setting it up in my bathroom to turn on when the light switch is flipped. My understanding is I'll need to set the system to 'read only' so all this turning on and off doesn't break it. I'll have to look into this 'time' module you speak of-- thanks for helping!
@TeCoEd2 жыл бұрын
@@UnexpectedDanger sounds interesting. I would keep the Pi o all the the time as the power draw is very low and the use a light sensor to trigger the music. When you turn the light on the sensor reads this and the code tells the Pi to play the music. 👍
@UnexpectedDanger2 жыл бұрын
@@TeCoEd I considered that but it seemed like an added layer of complexity. I don't know; maybe I will. Right now I have two python scripts; one for December and one of the rest of the year, set up via crontab. I fear that it's running the script lots of times since I set it up as * * * 12 * (any time in December, in this example). We'll see. Took me all night to get that far.
@UnexpectedDanger2 жыл бұрын
Thanks again for the advice, however; I'm giving up on this project. Finding code or tutorials is all but impossible. I've never seen such a gatekeepy community. Funny thing is, if I'd gotten this working, I'd be the first person ever to share their working code with everyone. So much for that.
@fardedballs43574 жыл бұрын
Will this just run it by itself or will I have to start the code in the raspberry pi?
@TeCoEd4 жыл бұрын
Yes it will run as the Pi boots up.
@matthewfordan29374 жыл бұрын
Tutorial on how to view camera on web page :D
@TeCoEd4 жыл бұрын
This is covered in one of the chapters in my new book. www.amazon.co.uk/12-Raspberry-Hacking-Projects-Minecraft/dp/1593279469/ref=tmm_pap_swatch_0?_encoding=UTF8&qid=&sr=
@TheSocialPotwal2 жыл бұрын
Hey, does this also work, if i boot in GUI mode? With autologgin? I tired it, but the script wont start :( Any idea, how to do it?
@TeCoEd2 жыл бұрын
If you boot into GUI then give your Pi enough time to boot up. I always add a 10 second delay to the Python code.
@GurpreetSingh-jz3rx2 жыл бұрын
Hi, thanks for your video. But, I ran into a problem. I am running a PyQt GUI program in full screen after boot, which works fine. But the thing is that the desktop screen is visible before the program starts running and so it doesn't look professional. My aim is to boot the raspberry pi to my app directly and not to desktop. I also tried using a splash screen in PyQt but got same result. Can you please help me with this? I am stuck with this for days. Thank you so much😃
@TeCoEd2 жыл бұрын
Do you have a delay before running the program? Try reducing this delay so that the program loads before the desktop and then over writes the desktop so you never see it.
@GurpreetSingh-jz3rx2 жыл бұрын
@@TeCoEd Thanks for your reply. But no i am not giving any delay. It may be that loading of window takes time.
@TeCoEd2 жыл бұрын
@@GurpreetSingh-jz3rx I see. The issue is that you need the desktop to enable the PyQt GUI to run. I will look at some of my old notes.
@GurpreetSingh-jz3rx2 жыл бұрын
@@TeCoEd thank you so much sir. It would be a great help
@ignit895 жыл бұрын
so at the end your cam is working after boot?
@TeCoEd5 жыл бұрын
Yes. The Cam is control by Python code with runs at start up.
@ignit895 жыл бұрын
@@TeCoEd ok thank you ill try it, wish me luck
@TeCoEd5 жыл бұрын
@@ignit89 keep me posted on how you get on.
@ignit895 жыл бұрын
@@TeCoEd THANK YOU VERY MUCH ! it works!
@TeCoEd5 жыл бұрын
@@ignit89 excellent news. Well down. 👍
@sonhiya5 жыл бұрын
Is there any way to autorun a program like chromium in pi.
@TeCoEd5 жыл бұрын
Check this link out. It provides five different ways. www.dexterindustries.com/howto/run-a-program-on-your-raspberry-pi-at-startup/
@anyssurayya26174 жыл бұрын
how to run multiple scripts when boot? where i have to modify
@TeCoEd4 жыл бұрын
Just add them to the crontab, one per new line, like a list.
@stashedup74155 жыл бұрын
what happens when the Rpi autoruns the program upon boot while HDMI is connected? My Rpi is not with me as of the moment :)
@TeCoEd5 жыл бұрын
The crontab runs the program on bootup. It doesn't matter whether there is a screen attached or not. If the program had graphics you may have to set the 'boot to desktop' option.
@stashedup74155 жыл бұрын
i want to connect my rpi to laptop using vnc viewer , unfortunately it says connection was refused, im lost now how to fix thiss heeelp me sir
@TeCoEd5 жыл бұрын
@@stashedup7415 is VNC enabled on the Pi? Check the settings. Also use the IP address of the Pi not the hostname. Try these.
@stashedup74155 жыл бұрын
@@TeCoEd im really lost any steps sir?
@stashedup74155 жыл бұрын
i tried the startx unfortunately nothing happens
@tikorzgamer11516 жыл бұрын
how can i interrupt the camera when it starts ?
@TeCoEd6 жыл бұрын
kemal uluz what do you mean by interrupt?
@tikorzgamer11516 жыл бұрын
to escape the autostart.
@TeCoEd6 жыл бұрын
kemal uluz go back into the Crontab and delete or comment out the line of code.
@tikorzgamer11516 жыл бұрын
how can i go to crontab if i can´t do anything if it starts the camera. Or do i enter something in startup ?
@MaxMax-wy7bi3 жыл бұрын
HEY GREAT VIDEO. But one question: It does not work to save crontab with cntrl X. When I press this ^x appears and it doesn’t save. To write, first I have to press i. Then I write the code and after that I am in insert mode still. Then the problem with ^x What am I doing wrong? Pls help me
@MaxMax-wy7bi3 жыл бұрын
I am talking about Minute 2:45 And I am not sure if it’s the nano editor because it looks not exactly like yours. There is no explanation with shortcut does what
@TeCoEd3 жыл бұрын
Try "sudo" before the crontab command.
@MaxMax-wy7bi3 жыл бұрын
@@TeCoEd do you mean sudo @reboot...?
@TeCoEd3 жыл бұрын
@@MaxMax-wy7bi when you open the crontab to edit it use sudo before the command.
@MaxMax-wy7bi3 жыл бұрын
@@TeCoEd I did it but it didn't worked
@ahmedAshraf-bh8qk3 жыл бұрын
Hi, I want to know how to auto run a face detection model this way doesn't work with me
@TeCoEd3 жыл бұрын
Some programs if they use a GUI or hardware have to be run from the desktop. So you create a script that runs 'after' boot up and loads your program.
Where do i need to type startx? I cant go back to my desktop
@TeCoEd5 жыл бұрын
Type in the command prompt after the pi@raspberrypi
@daanroes23005 жыл бұрын
@@TeCoEd i cant come to the command prompt
@shreevlogs88535 жыл бұрын
@@daanroes2300 did u find any solution?
@java204224 жыл бұрын
Sigh.... open the SD card on another device, navigate to the script you edited, and remove the command.... should work 100% and was easy to figure out also...
@nimtharadewruwani75583 жыл бұрын
can I run multiple python codes in pi boot up
@TeCoEd3 жыл бұрын
Yes, just add them in the same way to the crontab.
@kudakwashedzorani75763 жыл бұрын
How can i make this work on a virtual machine?
@TeCoEd3 жыл бұрын
No idea. 😮
@pawejanowiec59872 жыл бұрын
Is it working with desktop application in Python?
@TeCoEd2 жыл бұрын
Not sure what you mean?
@pawejanowiec59872 жыл бұрын
@@TeCoEd I made GUI im pyqt5 and I'm wondering if your way help me to make autostart
@TeCoEd2 жыл бұрын
@@pawejanowiec5987 it should do, but I have only used easy GUI and PyGame. It worked with them.
@hyperunstable93974 жыл бұрын
Does nothing when i control x just does the ^X sign wont save
@TeCoEd4 жыл бұрын
Sounds like you are not in the crontab.
@dergoldaffe76923 жыл бұрын
even after saving it and after the reboot my Raspberry pi didnt start the script. I look in Crontab and the writting line is gone
@TeCoEd3 жыл бұрын
Did you type 'sudo contain -e" or not sudo. ?
@dergoldaffe76923 жыл бұрын
@@TeCoEd yeah with sudo. Then it save it but still doesn't work
@juanfranciscocevallosvaldi1492 жыл бұрын
Bro now when I try to get into the desktop I get a message Cannot Currently show the Desktop, how can I disable this again to be able to get into de desktop?
@juanfranciscocevallosvaldi1492 жыл бұрын
NP, Just connect Into ssh and change boot option Just in case someone need it haha
@TeCoEd2 жыл бұрын
You can following the steps and open crontab again and delete the entry. Basically reverse the steps you followed.
@juanfranciscocevallosvaldi1492 жыл бұрын
Thank u, great tutorial
@Bryan-cm1jb3 жыл бұрын
cant read the screen
@adilkampoo94833 жыл бұрын
Brother please help! It's not working for me
@TeCoEd3 жыл бұрын
What program are you trying to start?
@jayralph20674 жыл бұрын
how to stop the program sir?
@TeCoEd4 жыл бұрын
Go back to crontab and delete or comment out the entry, then reboot.
@CameronCobb5 жыл бұрын
Unfortunately for me it didn't work : (
@TeCoEd5 жыл бұрын
What version of OS are you using ?
@CameronCobb5 жыл бұрын
@@TeCoEd I got it to work now. I had to do "@reboot sudo -H -u pi /usr/bin/python3 /home/pi/Desktop/scriptname.py &". Thank you for the video. It was very helpful!
@TeCoEd5 жыл бұрын
@@CameronCobb always remember the sudo. 👍
@joanmontoyamoya4 жыл бұрын
In case it does not work, for me it was very helpful being able to diagnose the errors. See here how to do it: www.raspberrypi.org/forums/viewtopic.php?t=195819 PS: Great video!
@TeCoEd4 жыл бұрын
Thanks for sharing the link.
@aqibfadh52225 жыл бұрын
How do i get back to dekstop? Lol
@TeCoEd5 жыл бұрын
Type 'startx'
@ManishGupta-jz9jf4 жыл бұрын
Its not work for me. What I can do?
@TeCoEd4 жыл бұрын
Can you send a screenshot?
@ManishGupta-jz9jf4 жыл бұрын
@@TeCoEd Sir, whose screenshort?
@TeCoEd4 жыл бұрын
@@ManishGupta-jz9jf a screenshot of the crontab.
@ManishGupta-jz9jf4 жыл бұрын
Sir, Thanku for reply...... Sir, Your email id please....
@TeCoEd4 жыл бұрын
@@ManishGupta-jz9jf kzfaq.info
@jacob6824 жыл бұрын
for some reason its not running
@jacob6824 жыл бұрын
im using a pygame were it runs a audio file
@alexvong14 жыл бұрын
make sure you're entering everything correctly
@TeCoEd4 жыл бұрын
PyGame does not always work with crontab as the GUI has to load and then run PyGame. There is some chat on the Pi forums about how to solve the issue www.raspberrypi.org/forums/viewtopic.php?t=128602