-
Notifications
You must be signed in to change notification settings - Fork 2k
Addition of some information on using systemd to start scripts at boot #681
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3fd8fff
Info on creating a systemd control file
buxtonpaul 4cdbd74
Added systemd control
buxtonpaul 8dfe781
Update systemd.md
buxtonpaul 4f853c8
Update systemd.md
buxtonpaul 73aac40
copy edits
JaninaPi cbddc26
copy edits
JaninaPi c76663c
copy edits
JaninaPi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# systemd | ||
|
||
In order to have a command or program run when the Pi boots, you can add it as a service. Once this is done, you can start/stop enable/disable from the linux prompt. | ||
|
||
## Creating a service | ||
|
||
On your Pi, create a .service file for your service, for example: | ||
|
||
myscript.service | ||
|
||
```[Unit] | ||
Description=My service | ||
After=network.target | ||
|
||
[Service] | ||
ExecStart=/usr/bin/python3 -u main.py | ||
WorkingDirectory=/home/pi/myscript | ||
StandardOutput=inherit | ||
StandardError=inherit | ||
Restart=always | ||
User=pi | ||
|
||
[Install] | ||
WantedBy=multi-user.target | ||
``` | ||
So in this instance, the service would run Python 3 from our working directory `/home/pi/myscript` which contains our python program to run `myscript.py`. But you are not limited to Python programs: simply change the ExecStart line to be the command to start any program/script that you want running from booting. | ||
|
||
Copy this file into `/lib/systemd/system` as root, for example: | ||
``` | ||
sudo cp myscript.service /lib/systemd/system/myscript.service | ||
``` | ||
|
||
Once this has been copied, you can attempt to start the service using the following command: | ||
``` | ||
sudo systemctl start myscript.service | ||
``` | ||
|
||
Stop it using following command: | ||
``` | ||
sudo systemctl stop myscript.service | ||
``` | ||
When you are happy that this starts and stops your app, you can have it start automatically on reboot by using this command: | ||
``` | ||
sudo systemctl enable myscript.service | ||
``` | ||
|
||
The `systemctl` command can also be used to restart the service or disable it from boot up! | ||
|
||
Some things to be aware of: | ||
+ The order in which things are started is based on their dependencies — this particular script should start fairly late in the boot process, after a network is available (see the After section). | ||
+ You can configure different dependencies and orders based on your requirements. | ||
|
||
|
||
You can get more information from: | ||
``` man systemctl``` | ||
or here: https://fedoramagazine.org/what-is-an-init-system/ | ||
|
||
Also be sure to reference absolute file names rather than doing so relative to your home folder, for example use `/home/pi/myscript.py` rather than `myscript.py`. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thing, you probably need to explain that service you are starting is, in this example, a Python program, but that you could have any sort of application start up here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree, that makes sense.