I have an issue.
my project is about to view streaming video and control motors or sound by web page (on local IP). I am using Raspberry PI Web Camera Interface. My python scripts only run when click the button (The buttons are on the web page). My sound button works well when I run my script on thonny or terminal but it doesn't work on boot.
Here is my code; ses.py
#!/usr/bin/env python3
from gpiozero import Button
from signal import pause
import time,sys
import subprocess
button = Button(21) # This button is not button on the web page
def play_music():
subprocess.run(["mpg321"],"/home/pi/003.mp3"])
subprocess.run('raspi-gpio set 21 op dh',shell=True)
try:
button.when_pressed = play_music
pause()
except KeyboardInterrupt:
sys.exit()
By the way buttons which is on the web page run .sh file and this .sh file run the python scripts
I write sudo python3 /home/pi/ses.py & > /home/pi/Desktop/log.txt 2>&1 in /etc/rc.local to start on boot.
the log file is empty.
My web gui is here, i have no real button. the buttons what i write about is on this photo. a .sh file run When i click the Ses-2 button, and this .sh file run my ses.py
I write sudo python3 /home/pi/ses.py & > /home/pi/Desktop/log.txt 2>&1 in /etc/rc.local to start on boot.
I guess you did not get that memo, but rc.local has been deprecated for a few years now. I think you may still be able to use it, but it's probably more trouble than it's worth... but here's a guide to help you get started if you're determined to use it.
N.B. This answer uses the CLI/terminal, so if you're not comfortable with that, you can stop reading now.
Other than rc.local you have two other mainstream alternatives for starting your script:
create a systemd "unit"
use cron
cron is generally easier than systemd. I'm all about instant gratification, so let's use that:
a cron job is typically one single line in your crontab file. That line includes a schedule, and a command. cron runs your command at the scheduled time - pretty straightforward.
to create your cron job, you must edit your crontab file. This is done like so:
$ crontab -e
when you open your crontab for the first time the system will prompt you to select an editor. If you're unsure which one to use, choose nano (pico?)
if you need root privileges (and it seems you do) to run your Python script, you should open root's crontab; the only thing to remember w/ scripts run by cron is do not use sudo in your script.:
$ sudo crontab
let's set up the schedule :
In your crontab editor, enter the following at the bottom of the crontab... the #reboot schedule will cause your command to be executed each time your RPi is booted.
#reboot
now that you've completed the schedule, let's enter the command:
#reboot /usr/bin/python3 /home/pi/ses.py > /home/pi/Desktop/log.txt 2>&1
save your crontab & exit the editor; you are ready to test:
$ sudo reboot
Some other things:
cron has some shortcomings relative to systemd. One is that cron does not know whether or not all of the resources needed to execute your script are ready when it is started (cron is actually started by systemd, but that's a longer story).
If you see any evidence in your /home/pi/Desktop/log.txt file that a resource was unavailable when cron tried to run it, the solution is simple: sleep. Edit your cron job to sleep a while before running the command:
#reboot sleep 10; /usr/bin/python3 /home/pi/ses.py > /home/pi/Desktop/log.txt 2>&1
another "shortcoming" is that cron does not run your jobs in the same environment that you have in your login or interactive shell. This can cause some surprises, but a bit of diligence in using full path specifications will generally keep things on track.
finally, note that I have removed your & background invocation in your command. I did this because all cron jobs run in the background... however, I'm not familiar with your application so other adjustments may be required.
So - that's it - end of answer... look forward to hearing your feedback.
SOLVED
I solved the problem. I created a .desktop file with this code sudo nano /etc/xdg/autostart/ses.desktop then i wrote these lines;
[Desktop Entry]
Name=ses
Exec=/usr/bin/python3 /home/pi/ses.py
and
reboot
Related
i have a file for my discord bot which i run on boot and now i also made a react.js app and i also want this to run on boot. But when i put:
cd /home/pi/kickzraptor-site && npm run start
also in my .bashrc file, only the first one gets run because its an infinite loop i think. How can i run both on startup? Thanks! (This is the line already at the bottom of my bashrc file)
echo Running bot.py scipt...
sudo python3 /home/pi/bot.py
Fastest way (not recommended) is to add & at the end of the command so that the program doesn't block for further processes. sudo python3 /home/pi/bot.py &
Recommended way is to create a systemd service that runs during or after the boot is completed (depending on its configuration). This method is also good for error handling and it provides more ability on the program.
I know this is an exact copy of this question, but I've been trying different solutions for a while and didn't come up with anything.
I have this simple script that uses PRAW to find posts on Reddit. It takes a while, so I need it to stay alive when I log out of the shell as well.
I tried to set it up as a start-up script, to use nohup in order to run it in the background, but none of this worked. I followed the quickstart and I can get the hello word app to run, but all these examples are for web applications and all I want is start a process on my VM and keep it running when I'm not connected, without using .yaml configuration files and such. Can somebody please point me in the right direction?
Well, at the end using nohup was the answer. I'm new to the GNU environment and I just assumed it didn't work when I first tried. My program was exiting with an error, but I didn't check the nohup.out file so I was unaware of it..
Anyway here is a detailed guide for future reference (Using Debian Stretch):
Make your script an executable
chmod +x myscript.py
Run the nohup command to execute the script in the background. The & option ensures that the process stays alive after exiting. I've added the shebang line to my python script so there's no need to call python here
nohup /path/to/script/myscript.py &
Logout from the shell if you want
logout
Done! Now your script is up and running. You can login back and make sure that your process is still alive by checking the output of this command:
ps -e | grep myscript.py
I'm looking to create a script in python that initiates an SSH session with a server. I know it has to be a simple process i'm just not sure where to start. My ultimate plan is to automate this script to run on startup. i am not even sure python is the best way to go i just know it comes preloaded on raspbain for the pi.
A simple bash script would be better suited to the task. It is possible with python, but there's no obvious reason to make it harder than necessary.
From
write a shell script to ssh to a remote machine and execute commands:
#!/bin/bash
USERNAME=someUser
HOSTS="host1 host2 host3"
SCRIPT="pwd; ls"
for HOSTNAME in ${HOSTS} ; do
ssh -l ${USERNAME} ${HOSTNAME} "${SCRIPT}"
done
From how do i run a script at start up (askubuntu):
You will need root privileges for any the following. To get root, open
a terminal and run the command
sudo su
and the command prompt will change to '#' indicating that the terminal
session has root privileges.
Alternative #1. Add an initscript.
Create a new script in /etc/init.d/myscript.
vi /etc/init.d/myscript
(Obviously it doesn't have to be called "myscript".) In this script,
do whatever you want to do. Perhaps just run the script you mentioned.
#!/bin/sh
/path/to/my/script.sh
Make it executable.
chmod ugo+x /etc/init.d/myscript
Configure the init system to run this script at startup.
update-rc.d myscript defaults
Alternative #2. Add commands to /etc/rc.local
vi /etc/rc.local
with content like the following.
# This script is executed at the end of each multiuser runlevel
/path/to/my/script.sh || exit 1 # Added by me
exit 0
Alternative #3. Add an Upstart job.
Create /etc/init/myjob.conf
vi /etc/init/myjob.conf
with content like the following
description "my job"
start on startup
task
exec /path/to/my/script.sh
Depending on what you do with the ssh connection, if it needs to stay open over the entire up time of the device, you will need to use some more trickery however (ssh connections are autoclosed after a period of inactivity).
I'm relatively new to raspberry pi (5 days using it) and I've just finished to run my python script succesfully (called dogcare.py). Now I'm trying to execute this script right after my raspberry is turned on. I've been doing some research and I find different ways to do it:
using /etc/profile
using /etc/rc.local
using crontab
using /etc/init.d
using systemd
But none of these ways are working for me.
Setup enviroment:
Hardware: RaspberryPi 2 Model B
Software: Raspbian or NOOBs (not sure)
Context:
Since for my project I need to run meet.jit.si, I followed this guide http://www.instructables.com/id/Video-Calling-on-Raspberry-Pi-3/?ALLSTEPS and It has a step where sets chromium website to start right after RPi is turned on. (Currently this is working fine)
My python script is using request library in order to use HTTP GET with an external website application I've been working on.
Main problem:
I need to run both events: chromium website with meet.jit.si and my python script when my raspberry is turned on.
Current situation: chromium website is running after my RPi is turned on but my script doesn't.
I'd appreciate any help !
I have done a similar thing with my Raspi 2 as well which involved sending myself an email with the ip address of the pi so I could easily ssh/vnc to it.
My steps involved making a shell script which ran the python program.
#!/bin/sh
cd pythonfiledirectory
sudo python pythonfile.py
cd /
Then I made it executable with the following command:
chmod 777 file.sh
Now edit your crontab to run the file on startup.
In your terminal, type:
sudo crontab -e
Inside of the crontab write:
#reboot sh file.sh
You could add a log file if you wanted to debug and see why it's not working by making a log directory and changing the text you wrote in the crontab to:
#reboot sh file.sh >/logdirectoy/ 2>&1
This is what made it work for me and if it doesn't work try and make sure you can run your .sh file and try the crontab with some other files to debug the problem.
I've setup an Amazon EC2 server. I have a Python script that is supposed to download large amounts of data from the web onto the server. I can run the script from the terminal through ssh, however very often I loose the ssh connection. When I loose the connection, the script stops.
Is there a method where I tell the script to run from terminal and when I disconnect, the script is still running on the server?
You have a few options.
You can add your script to cron to be run regularly.
You can run your script manually, and detach+background it using nohup.
You can run a tool such as GNU Screen, and detach your terminal and log out, only to continue where you left off later. I use this a lot.
For example:
Log in to your machine, run: screen.
Start your script and either just close your terminal or properly detach your session with: Ctrl+A, D, D.
Disconnect from your terminal.
Reconnect at some later time, and run screen -rD. You should see your stuff just as you left it.
You can also add your script to /etc/rc.d/ to be invoked on book and always be running.
You can also use nohup to make your script run in the background or when you have disconnected from your session:
nohup script.py &
The & at the end of the command explicitly tells nohup to run your script in the background.
If it just a utility you run ad-hoc, not a service daemon of some kind, i would just run it in screen. Than you can disconnect if you want and open the terminal back up later... Or reconnect the terminal if you get disconnected. It should be in your linux distros package manager. Just search for screen
http://www.gnu.org/software/screen/
nohup runs the given command with hangup signals ignored, so that the command can continue running in the background after you log out.
Syntax:
nohup Command [Arg]...
Example:
nohup example.py
nohup rasa run
Also, you can run scripts continuously using the cron command.
For more:
https://ss64.com/bash/nohup.html
https://opensource.com/article/17/11/how-use-cron-linux