I'm using a Raspberry Pi 3 with Raspbian distribution.
I've written a script in Python 3 and I would need to start it up automatically just when system boots up, without logging in.
I would recommend using systemd to achieve this. Take for example your python script is called hello.py.
Create a systemd service file at /lib/systemd/system/hello.service:
[Unit]
Description=hello.py service file
After=multi-user.target
[Service]
Type=simple
ExecStart=/usr/bin/python /dir/to/your/hello.py
Restart=always
[Install]
WantedBy=multi-user.target
The full list of commands and functions for the systemd service file can be found here.
Add appropriate permissions to the .service file: sudo chmod 644 /lib/systemd/system/hello.service
Reload the systemd daemon: sudo systemctl daemon-reload
Enable the hello systemd service: sudo systemctl enable hello.service
You can check that your service is running by using the command: sudo systemctl status hello.service and check for any errors using sudo journalctl
Raspberry PI has quite good support. Tasks can be schedualed with usage of "crontab" command.
You can find documentation on: Documentation
Within this documentation you have example for running "python" script on Raspberry Pi reboot.
Hope it helps!
Related
I am working on an RFID-based access control system for which I have a working python script. For some specific details if they matter, the main processing is done on a pi zero w, which is connected by USB to a microcontroller that handles the input from the RFID module and sends it to the pi in string format for simplicity. The pi then compares the string received to a yaml file and a schedule and uses GPIO to switch on or off a door strike using a power supply. The issue I'm running into is that the script stops running after about 30 minutes, and I'm not quite sure why, but I think the ideal solution in any case is to daemonize it, because a cron job is too subject to failure and a daemon seems very appropriate for this use. Does anyone have any suggestions for daemonizing the script such that it will start on boot and restart itself if it detects a failure or that it is no longer running?
As larsks said, you can create systemd service
sudo nano /etc/systemd/system/yourscript.service
This file should be something like this (read the documentation for more information):
[Unit]
Description=My cool script
After=multi-user.target
[Service]
User=root
WorkingDirectory=/path/to/your/script/directory/
Restart=on-failure
RestartSec=5s
ExecStart=/usr/bin/python3 your_script.py
StandardOutput=append:/var/log/your_script.log
StandardError=append:/var/log/your_script.log
SyslogIdentifier=coolawesomescript
[Install]
WantedBy=multi-user.target
Then enable and start it:
foo#bar:~$ sudo systemctl enable yourscript
foo#bar:~$ sudo systemctl start yourscript
Now your script will automatically restart when it crashes
You can check if your script actually works by typing sudo systemctl status yourscript
I'm running a chatbot on Ubuntu server on Amazon EC2 instance. I want to be able to run the python3 program even after closing the pUTTY window. So far I've tried 'Ctrl+a, d' as well as 'Ctrl+z, bg'. However both methods did not seem to work after closing the pUTTY window. I referenced the following youtube video:
Would really appreciate any help!
I hope the question is "how to keep the python script running even after closing the putty"
IMO, you can use 2 approaches here.
Use nohup
The unix command nohup can make your program run in background even after exiting the terminal.
You can run it like
nohup python3 LP_poolVol.py > /dev/null &
nohup will make the script running and the & at the end will make it in
background
Make the script a service
Run the script as a linux service which you can start and stop using the systemctl
You can create a service descriptor file pool-vol.service with contents similar to below.
[Unit]
Description=Pool Vol Service
After=multi-user.target
[Service]
Type=simple
Restart=always
ExecStart=/usr/bin/python3 <path-to>/LP_poolVol.py
[Install]
WantedBy=multi-user.target
Then copy this service file to /etc/systemd/system/. And then install it via the commands below
sudo systemctl daemon-reload
sudo systemctl enable pool-vol.service
sudo systemctl start pool-vol
Now your app is running as a service. You can stop or restart it using the systemctl itself like
sudo systemctl start pool-vol
I wrote a python program that I want to execute on boot, but it HAS to be executed as root, and I don't know how.
What are the things I need to do to achieve this? Where should I put the file(the script is in a folder with the necessary python packages) so it runs on boot?
How do I run the file as root? Whether it is to put the python folder in a certain directory, use another script to execute the python script, or another way, please share your solutions!
I'm doing it on a Raspberry Pi, so the OS is Linux.
The easiest way is to create a systemd service that might look like:
[Unit]
Description=Some python script
After=network.target
[Service]
ExecStart=/usr/bin/python3 script.py
WorkingDirectory=/path/to/scriptdir
StandardOutput=inherit
StandardError=inherit
Restart=always
User=root
[Install]
WantedBy=multi-user.target
You should save this in /etc/systemd/system/servicename.service where servicename can be anything, then set it to run on startup with sudo systemctl enable servicename.service.
I am working on raspberry pi 3 about 3 months , I had a problem when I started working with it.
I couldn't find an efficient and safe way to run a python script on raspberry when it turns on(without monitor and mouse and keyboard).At the moment I have added "$sudo run myscript.py &" at /etc/profile but sometimes when I turn it on my script doesn't run until I connect monitor and mouse and keyboard to it and run the script with GUI and after that it works fine (again without mouse and keyboard).
I want to know is there any solution that I will be sure my script will run after I turn raspberry pi on?
Thanks a lot
You will want to setup a service and user sudo service <my_service> [start, stop, restart] to get it working on startup. See here for reference.
The /etc/profile is executed when new shell session in being started, so unless you start at least single shell session your script will not be run. Moreover it will be terminated when session stops, and if you start multiple sessions then the script will also be started for each session, which is probably not what you want.
Depending on your init system you would need to create SysVinit or systemd service. Assuming you use systemd based distro (which is currently default for most Linux distributions) you need to do following:
Step 1: Place your script in location from which it will be executed by service. For example /usr/local/bin/ may be good choice.
Step 2: Create service file. Assuming you want to name it myscript.service, create file at following path /etc/systemd/system/myscript.service with following content:
[Unit]
Description=myscript
[Service]
ExecStart="/usr/bin/python /usr/local/bin/myscript.py"
[Install]
WantedBy=multi-user.target
Step 3: Reload systemd daemon and enable your service:
systemctl daemon-reload
systemctl enable myscript
Now after you restart your system, your service should be automatically started. You can verify that using command systemctl status myscript, which returns service status.
I'm now working on a Web application which is based on Django-1.8.2 and on virtual env provided by Python-3.4.3 of Ubuntu-15.04.
My app. is now working on the development server, and has been successfully deployed using mod_wsgi-4.4.11 (pip installed) and Ubuntu's Apache. That is, after collecting static files and modifying the file permissions of db.sqlite3, I did ...
sudo ../bin/python manage.py runmodwsgi --setup-only --port=80 --user www-data --group www-data --server-root=/etc/mod_wsgi-express-80
sudo /etc/mod_wsgi-express-80/apachectl start
to have a working server.
I'd like the daemons to start up on boot-up automatically, but I have no luck in writing an init.d script or a config file. Probably, an utterly new way is required for the systemd of 15.04. Any suggestions will be welcome.
Looks like the question is about Ubuntu systemd launcher rather than django + mod_wsgi...
After struggling with init.d scripts, I have moved over to systemd service file:
/lib/systemd/system/apache_mod_wsgi.service
[Unit]
Description=apache2 with mod_wsgi
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/etc/mod_wsgi-express-80/apachectl start
ExecStop=/etc/mod_wsgi-express-80/apachectl stop
[Install]
WantedBy=multi-user.target
With this file saved, I killed the native apache2 service and enabled the new service...
fukuda#ubuntu:~% sudo update-rc.d apache2 remove
fukuda#ubuntu:~% sudo systemctl enable apache_mod_wsgi.service
fukuda#ubuntu:~% sudo reboot
The daemons have started successfully, and the django application seems working properly (that is, is responding in an exactly same manner with "manually started" version and the development server.
The only concern is that dmesg would not report the starting up of the service.
If you searched for how to start a mod_wsgi-express with init.d or the likes instead of with django, first have a look at https://pypi.python.org/pypi/mod_wsgi#running-mod-wsgi-express-as-root.
you should use setup-server:
mod_wsgi-express setup-server wsgi.py --port=80 \
--user www-data --group www-data \
--server-root=/etc/mod_wsgi-express-80
This should be the prerequisite to OP's answer.