Until recently, I used to daemonize a slackbot program written in Python with python-daemon, but it was troublesome to manage because the program was not automatically executed when the OS started. When I tried using systemd instead of python-daemon, I was able to execute the program automatically when the OS started, so I will show you how to do it.
Windows 10 Home Python 3.7.3 Raspberry Pi OS 10
run.py
import time
from slackbot.bot import Bot
from slacker import Slacker
def main():
bot = Bot()
bot.run()
slack = Slacker('Enter API token')
channel_name = 'Enter the name of the channel you want to post a message to'
message = 'Enter the content of the message'
if __name__ =="__main__":
slack.chat.post_message(channel_name, message, as_user=True) #Post a message at startup
time.sleep(3) # 'Start request repeated too quickly.'Measures for
main() #Launch slackbot
slackbot_settings.py
API_TOKEN = 'Enter API token'
DEFAULT_REPLY = 'Enter the message that the bot returns when you don't understand the meaning of the message addressed to the bot'
PLUGINS = ['run.py, slackbot_settings.Program name used other than py
(Example: slackbot_module)write']
slackbot_module.py
import random
import re
from slackbot.bot import listen_to
morning_greetings = ['Good morning!', 'Nem ...(˘ω˘)']
@listen_to(r'^(good morning|Good morning ~|Good morning)(There is.*)?$')
def morning_greeting(message, fir_word, sec_word):
message.reply(random.choice(morning_greetings))
message.react('Enter the English name of the emoji')
afternoon_greetings = ['Hello!', 'Hello']
@listen_to(r'^Hello.*$')
def afternoon_greeting(message):
message.reply(random.choice(afternoon_greetings))
message.react('Enter the English name of the emoji')
night_greetings = ['Good evening!', 'Good evening']
@listen_to(r'^Good evening.*$')
def night_greeting(message):
message.reply(random.choice(night_greetings))
message.react('Enter the English name of the emoji)')
Be sure to name the file that describes API_TOKEN slackbot_settings.py
: 3
Be sure to write time.sleep (3) in run.py
...! The reason will be explained later.
Transfer the locally created file to the Raspberry Pi. As a security measure, let's set up public key authentication. After setting, start the command prompt or PowerShell and
scp path of file to copy-i Private key file path-p port number username@hostname:Directory name to put the file
Transfer the file with.
Ssh connect to the Raspberry Pi from the command line.
ssh -i Private key file path-p port number username@hostname
You can quickly connect to the remote by creating a configuration file called config
directly under~ / .ssh /
.
Entry example
Host raspi
User hoge
HostName raspberrypi
Port 2222
IdentityFile ~/.ssh/id_rsa
IdentitiesOnly yes
In this example
ssh -i ~/.shh/id_rsa -p 2222 hoge@raspberry
Others,
ssh raspi
You can also make an SSH connection with the command.
Create a service file under / etc / systemd / system /
.
This time I want to daemonize run.py, so I will create a file called rund.service (it doesn't matter if the file name is appropriate).
$ sudo nano /etc/systemd/system/rund.service
Write the following settings to rund.service.
rund.service
[Unit]
Description = slackbot daemon
[Service]
Type = simple
Restart = on-failure
ExecStart =Absolute path to Python3 run.Absolute path to py
[Install]
WantedBy = multi-user.target
Since the writing is finished, I will start systemd.
$ sudo systemctl start rund
Don't forget to check if it works! Let's check the status with the following command.
$ sudo systemctl status rund
If ʻActive: active (running)` is displayed, it is OK.
Set the program called run.py, which was daemonized earlier, to be automatically executed when the OS starts.
$ sudo systemctl enable rund
Just in case, make sure that it can be started automatically.
$ sudo reboot
After reconnecting, check the status again with the following command.
$ sudo systemctl status rund
As before, if ʻActive: active (running)` is displayed, the setting is complete! !!
Immediately after starting the OS, the internet connection is not established yet, so even if run.py is executed at that timing, it seems that slackbot fails to start (in my environment, ʻAfter = network.target in the service file). I couldn't even add `). If it fails to start, rund.service will run run.py again. However, because the internet connection is not connected, the startup fails again ... and so on. After investigating, it seems that the automatic start is set to stop if the automatic start fails 5 times and the total time to start run.py for the 6th time is within 10 seconds. As a countermeasure, write time.sleep () in the line before main () so that automatic startup will not be performed 6 times within 10 seconds.
abridgement
if __name__ =="__main__":
time.sleep(3)
main()
If you write it like this, it will start automatically.
Recommended Posts