Skip to content

Instantly share code, notes, and snippets.

@TehPeGaSuS
Last active October 17, 2022 18:21
Show Gist options
  • Save TehPeGaSuS/177325c0cb14564bc45a7ba1885d9862 to your computer and use it in GitHub Desktop.
Save TehPeGaSuS/177325c0cb14564bc45a7ba1885d9862 to your computer and use it in GitHub Desktop.
#--------------------------------------------------#
# Be sure to have screen installed in your machine #
# Systemd unit tested on Ubuntu 18.04 and newer #
#--------------------------------------------------#
#----------------------------------------------------------------------------#
# This script will start the bot under a dettached screen session so you can #
# attach to the session and use the terminal to simulate DCC chat #
# You can still use telnet to connect to the bot, nonetheless #
#----------------------------------------------------------------------------#
[Unit]
#------------------------------------------------------------------#
# Set here the description to be shown on systemd control commands #
## #
# Example: Description=Micaela (Eggdrop) #
#------------------------------------------------------------------#
Description=Micaela (Eggdrop)
#-------------------------------------------------------------#
# DO NOT EDIT THIS OPTION UNLESS YOU KNOW WHAT YOUR'RE DOING! #
#-------------------------------------------------------------#
After=default.target
[Service]
#----------------------------------------------#
# Path to your bot folder #
## #
# Example: WorkingDirectory=/home/bots/Micaela #
#----------------------------------------------#
WorkingDirectory=/home/bots/Micaela
#------------------------------------------------------------------------------------------------------------------------------------#
# Command to start the bot, using a screen session. #
## #
# Example: ExecStart=/usr/bin/screen -L -U -dmS Micaela /home/bots/Micaela/eggdrop -t micaela.conf #
# Explanation of the options used here: #
# -L = logs the entire screen session on a file named screenlog.0 in the bot folder. #
# -U = Tell screen to use UTF-8 encoding. #
# -dmS <name> = Start as daemon: Screen session in detached mode. #
#------------------------------------------------------------------------------------------------------------------------------------#
ExecStart=/usr/bin/screen -L -U -dmS Micaela /home/bots/Micaela/eggdrop -t micaela.conf
#---------------------------------------------------------------------------------------------------#
# Command to stop the bot #
## #
# Unfortunatelly seems I have to send a `quit` to the screen session, otherwise it will error with: #
# Main process exited, code=exited, status=1/FAILURE #
## #
# Example: ExecStop=/usr/bin/screen -XS Micaela quit #
#---------------------------------------------------------------------------------------------------#
ExecStop=/usr/bin/screen -XS Micaela quit
#---------------------------------------------------------------------------------------------------------------#
# DON'T TOUCH ANYTHING BELOW UNLESS YOU KNOW WHAT YOU ARE DOING! #
# #
# If you touch the code below and then complain the script "suddenly stopped working", I'll touch you at night. #
#---------------------------------------------------------------------------------------------------------------#
Type=forking
Restart=on-failure
[Install]
WantedBy=default.target
@TehPeGaSuS
Copy link
Author

TehPeGaSuS commented Dec 4, 2021

Instructions

This was tested on Ubuntu 18.04 but should work in every distro with systemd. Check your distro docs to be sure.

Be sure to have screen installed on your system

  • Enable lingering for your user with:
    loginctl enable-linger

  • Create the .config/systemd/user directory, either manually or by running the following command:
    systemctl --user enable systemd-tmpfiles-clean.timer && systemctl --user disable systemd-tmpfiles-clean.timer

  • Enter the .config/systemd/user directory with:
    cd ~/.config/systemd/user

  • Download the user unit (replacing <botname>_screen_user.service with your bot name) with:
    wget https://gist.githubusercontent.com/PeGaSuS-Coder/177325c0cb14564bc45a7ba1885d9862/raw/1eaa42a3a22058ee81c37871de748d76bf7e8ede/eggdrop_screen_user.service -O <botname>_screen_user.service

  • Edit the user unit to fit your install

  • Reload the user systemd daemon (so your new user unit is loaded) with:
    systemcl --user daemon-reload

  • Start the bot with:
    systemctl --user start <botname>_screen_user.service

  • Stop the bot with:
    systemctl --user stop <botname>_screen_user.service

  • Restart the bot with:
    systemctl --user restart <botname>_screen_user.service

  • Enable the bot to start automatically after a system reboot/restart with:
    systemctl --user enable <botname>_screen_user.service

@TehPeGaSuS
Copy link
Author

TehPeGaSuS commented Dec 4, 2021

Examples

Actual valid user unit for example purposes:

#--------------------------------------------------#
# Be sure to have screen installed in your machine #
# Systemd unit tested on Ubuntu 18.04 and newer    #
#--------------------------------------------------#

#----------------------------------------------------------------------------#
# This script will start the bot under a dettached screen session so you can #
# attach to the session and use the terminal to simulate DCC chat            #
# You can still use telnet to connect to the bot, nonetheless                #
#----------------------------------------------------------------------------#

[Unit]
#------------------------------------------------------------------#
# Set here the description to be shown on systemd control commands #
##                                                                 #
# Example: Description=Micaela (Eggdrop)                           #
#------------------------------------------------------------------#
Description=Scheherazade (Eggdrop)

#-------------------------------------------------------------#
# DO NOT EDIT THIS OPTION UNLESS YOU KNOW WHAT YOUR'RE DOING! #
#-------------------------------------------------------------#
After=default.target

[Service]
#----------------------------------------------#
# Path to your bot folder                      #
##                                             #
# Example: WorkingDirectory=/home/bots/Micaela #
#----------------------------------------------#
WorkingDirectory=/home/bots/Scheherazade

#------------------------------------------------------------------------------------------------------------------------------------#
# Command to start the bot, using a screen session.                                                                                  #
##                                                                                                                                   #
# Example: ExecStart=/usr/bin/screen -L -U -dmS Micaela /home/bots/Micaela/eggdrop -t micaela.conf                                   #
# Explanation of the options used here:                                                                                              #
#	-L = logs the entire screen session on a file named screenlog.0 in the bot folder.                                               #
#	-U = Tell screen to use UTF-8 encoding.                                                                                          #
#	-dmS <name> = Start as daemon: Screen session in detached mode.                                                                  #
#------------------------------------------------------------------------------------------------------------------------------------#
ExecStart=/usr/bin/screen -L -U -dmS Scheherazade /home/bots/Scheherazade/eggdrop -t scheherazade.conf

#---------------------------------------------------------------------------------------------------#
# Command to stop the bot                                                                           #
##                                                                                                  #
# Unfortunatelly seems I have to send a `quit` to the screen session, otherwise it will error with: #
# Main process exited, code=exited, status=1/FAILURE                                                #
##                                                                                                  #
# Example: ExecStop=/usr/bin/screen -XS Micaela quit                                                #
#---------------------------------------------------------------------------------------------------#
ExecStop=/usr/bin/screen -XS Scheherazade quit

#---------------------------------------------------------------------------------------------------------------#
# DON'T TOUCH ANYTHING BELOW UNLESS YOU KNOW WHAT YOU ARE DOING!                                                #
#                                                                                                               #
# If you touch the code below and then complain the script "suddenly stopped working", I'll touch you at night. #
#---------------------------------------------------------------------------------------------------------------#
Type=forking
Restart=on-failure

[Install]
WantedBy=default.target

Example output of systemctl --user status botname_screen_user.service:

$ systemctl --user status scheherazade_screen_user.service 
● scheherazade.service - Scheherazade (Eggdrop)
     Loaded: loaded (/home/bots/.config/systemd/user/scheherazade_screen_user.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2022-10-02 22:43:27 CEST; 5min ago
    Process: 1465372 ExecStart=/usr/bin/screen -U -dmS Scheherazade /home/bots/Scheherazade/eggdrop -t scheherazade.conf (code=exited, status=0/SUCCESS)
   Main PID: 1465373 (screen)
      Tasks: 3 (limit: 4554)
     Memory: 13.6M
        CPU: 3.611s
     CGroup: /user.slice/user-1001.slice/user@1001.service/app.slice/scheherazade_screen_user.service
             ├─1465373 /usr/bin/SCREEN -U -dmS Scheherazade /home/bots/Scheherazade/eggdrop -t scheherazade.conf
             └─1465374 /home/bots/Scheherazade/eggdrop -t scheherazade.conf

Oct 02 22:43:27 X3 systemd[1974]: Starting Scheherazade (Eggdrop)...
Oct 02 22:43:27 X3 systemd[1974]: Started Scheherazade (Eggdrop).

After this, you can use telnet to login to the bot or just use screen -r Scheherazade. To detach from a screen session, use Ctrl+A, D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment