How to start and run a python script during boot - python

First, I apologize for my bad English and now my problem :
at the moment I try that my python script camera.py should autostart after the boot has been finished.
However, I tried using rc.local, .bashrc and init.d but nothing will work. Does someone have a better solution, so my script autostarts.
Here you can see the line that I add in my rc.local. Before this line are only blue comments
sudo python /home/pi/Desktop/camera.py &
exit 0
Here is the .bashrc that I used. I only add on the last line of .bashrc those two lines:
echo Running at boot
sudo python /home/pi/Desktop/camera.py

Are you sure that it has been added to the end of the .bashrc?
1.Make sure you are in the pi folder:
$ cd ~
2.To open up .bashrc for configuration:
$ sudo nano .bashrc
Scroll down to the bottom and add the line: python ~/Desktop/camera.py
Save and exit: Ctrl+X, Y, Enter
then reset your pi

You should try setting it as a chron job. Check this out: https://www.ostechnix.com/a-beginners-guide-to-cron-jobs/
This will make it run independent of your user login to a shell. Specifically, look at the #reboot section.

Related

Unable to execute a python3 script from rc.local

The Python script is unable to work in rc.local, as soon as it nevet gets executed. My idea is to run the script when the Raspberry Pi gets boot.
I have tested it with this sentence. The log.txt file only appears when I execute the program manually.
f = open("log.txt", "w")
f.write("log is working")
f.close()
Before that, I have tried to insert a time.sleep(30), to use usr/bin/python3, to change the head of the script to #!/usr/bin/env python3, change the user is executing the program to -u pi and a lot of things I can't even remember.
The final sentence is had before exit(0) is
sudo /usr/bin/python3 /home/pi/script.py &
rc.local is working as soon as it runs an echo that I have create in the file.
Finally the problem I had was that the script needed network, so I added it to crontab -e.
It still didn't work so I changed the raspi-config, as there is an option for network wait to network, but without success.
Finally, as that solution didn't work too, I added an sleep in the command as follows to wait for network:
#reboot sleep 40 && /usr/bin/python3 /home/pi/script.py
That finally worked.

Problem running python script at boot raspberry pi

I'm having an insane amount of trouble with my script at startup, I've tried countless ways and spent hours trying to get this to work.
I have a python script that I need to run at startup. However, it needs access to the internet, so I have to wait for the network.
I have tried many ways in the several tutorials I have tried, with crontab, by making a service with systemd, with rc.local however none of these have worked.
The only way that I was able to work was by doing a .desktop Desktop Entry, but that only worked for me while I had an external monitor plugged in, and my raspberry pi will be running without one.
Also, I was able to make my script run using the service method and now the rc.local
by adding this line:
sudo bash -c '/usr/bin/python3 /home/pi/Projects/capstone/main.py > /home/pi/capstone.log 2>&1' &
However, in the python script that I am trying to run, I have the following code:
os.system("sudo killall servod")
time.sleep(1)
os.system('sudo ~/PiBits/ServoBlaster/user/./servod')
And for some reason, it's not running my script correctly because I get the following error in my logs:
servod: no process found
sudo: /root/PiBits/ServoBlaster/user/./servod: command not found
The first one is expected because I run sudo killall servod when it may or may not be started, but the second one "Command not found" is what is the issue, if that bit of code doesn't get executed my program doesn't work.
Anyone out there could help me out with this?
Replace:
os.system('sudo ~/PiBits/ServoBlaster/user/./servod')
with:
os.system('sudo /home/pi/PiBits/ServoBlaster/user/./servod')
Double check the path and try to use absolute path
Make sure you script has permission 644
You can also try to copy the script to /etc/init.d and run it as init.d script. You do need to add the following to your script:
### BEGIN INIT INFO
# Provides: scriptname
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start daemon at boot time
# Description: Enable service provided by daemon.
### END INIT INFO
sudo chmod +x <yourscript.py>
sudo update-rc.d <yourscript.py> defaults
One way you could easily wait for the network inside your python script is to ping a server until successful -- in this case Google.
def wait_for_network():
while os.system("ping -c 1 8.8.8.8") != 0:
time.sleep(1)
return
As for running the script at startup, I recommend editing /etc/xdg/lxsession/LXDE-pi/autostart and adding your python script in there with the format
#python3 home/pi/your_script.py

Auto run commands on Raspberry pi

i wanted to know what would be the best approach to solving this issue. I would like to run a bunch of commands to run some python scripts and a service when the raspberry pi loads into the desktop. Here are my commands:
cd /var/www/html/
python servocontrol.py
cd /var/www/html/Misc
python temp1.py
python seven_segment.py
sudo /etc/init.d/livestream.sh start
My initial method which i read on most post was to add it in the rc.local by:
sudo nano /etc/rc.local
And paste the exact commands in it as follows:
#
# By default this script does nothing.
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi
sleep 15
cd /var/www/html/
python servocontrol.py
cd /var/www/html/Misc
python temp1.py
python seven_segment.py
sudo /etc/init.d/livestream.sh start
exit 0
Sadly it didn't work. Kindly if any one can point out what i am missing or if there are additional steps that need to be done for this to work. If there are other methods i am also open to them!
Thanks
you can use crontab on raspberry pi (The crontab (short for "cron table") is a list of commands that are scheduled to run at regular time intervals on your computer system. The crontab command opens the crontab for editing, and lets you add, remove, or modify scheduled tasks.)
AND you can create service for your script.
it's very easy just need 5 sec search to find instruction.
are you want execute a script in server start?
you can use #reboot in cron

Do I have to change directories and give permissions everytime I try to run a file directly from Terminal?

I wrote some simple code that would would open google maps and would search for the address that you pass to it as an attribute.The name of the file is mapit.py The code is:
#! /usr/bin/env python3
#! python3
import sys,webbrowser
a=sys.argv
b=a[1:]
main=' '.join(b)
webbrowser.open('https://www.google.co.in/maps/place/' +str(main))
As you can see, this is designed to run from the Terminal. This is what I do when I try to run this program in Terminal:
1 Save the file on the desktop
2 Open terminal and change the directory to desktop
3 Pass chmod +x mapit.py
4 Enter ./mapit.py Harvard University, USA
And it works perfectly fine.
But is there a simple way to run mapit.py in the terminal without changing directories and giving permissions every time I want to run the file?
To running scripts without changing directory, add scripts' location to the PATH. Lets say your dir name is mapit/ under the home dir:
$ export PATH=$PATH":$HOME/mapit"
$ mapit.py
Adding ~/mapit to the PATH like this is only temporary, however. It won’t stick across terminal sessions or system restarts. If you want to make your command permanently available on a system:
echo 'export PATH=$PATH":$HOME/mapit"' >> .profile
And one last thing, when you changed the permissions you dont need to change it over and over again to run the script.

Daemon with python 3

I am writing a script in python3 for Ubuntu that should be executed all X Minutes and should automatic start after logging in. Therefore I want to create a daemon (is it the right solution for that?) but I haven't found any modules / examples for python3, just for python 2.X. Do you know something what I can work with?
Thank you,
I would simply make the script, and have it somewhere, and then add a line to the crontab of the user who you want to run the script. This may be the root.
sudo crontab -e
To start the editor of the crontab
X * * * * /usr/bin/python /path/to/the/script
This way the script will be executed every X minutes. No need to daemonize, no need to make your own timer in the script.
Suppose for python script name is monitor. use following steps:
copy monitor script in /usr/local/bin/ (not necessary)
Also add a copy in /etc/init.d/
Then execute following command to make it executable
sudo -S chmod "a+x" "/etc/init.d/monitor"
At last run update.rc command
sudo -S update-rc.d "monitor" "defaults" "98"
this will execute you monitor whenever you login for all tty.

Categories

Resources