Difference between revisions of "HOWTO Create a Service"

From Pumping Station One
Jump to navigation Jump to search
(Created page with "== Philosophy == * ease of use * pass off project maitinance == working on sally == All members have shell and admin access on sally. ssh [email protected]...")
 
m (Robot: Cosmetic changes)
 
(12 intermediate revisions by 2 users not shown)
Line 1: Line 1:
== Philosophy ==  
+
== Why do it this way? ==
  
* ease of use
+
* Your service will be watchdogged by systemd
* pass off project maitinance
+
* Your service will recover from network and power outages.
 +
* It allows other members to help maintain your service, or disable it cleanly if it need attention.
 +
* It's great for IRC bots
  
== working on sally ==
+
== Working on Sally ==
  
 
All members have shell and admin access on sally.
 
All members have shell and admin access on sally.
  
 
     ssh [email protected]
 
     ssh [email protected]
 +
 +
You also have sudo (root user) access.
 +
 
     sudo uname -a
 
     sudo uname -a
  
== Create service environment ==
+
== Create Your Service Environment ==
  
=== Create a system account for your service ===
+
=== Create a System Account for your Service ===
  
 
     sudo useradd strup -b /srv/ -m -r
 
     sudo useradd strup -b /srv/ -m -r
  
=== Working With SystemD ===
+
 
 +
The creates a user named <code>strup</code>. The -b indicates the base home directory is /srv/, and therefore the system accounts home directory is /srv/strup/
 +
 
 +
== Working with SystemD ==
  
 
Create a unit file:
 
Create a unit file:
Line 37: Line 45:
 
</pre>
 
</pre>
  
==== start service, check for log output, etc ====
+
=== Start Your Service and Check for Log Output ===
  
 
     sudo systemctl start strup.service
 
     sudo systemctl start strup.service
 
     sudo systemctl status strup.service
 
     sudo systemctl status strup.service
 
  
 
To follow log output:
 
To follow log output:
Line 48: Line 55:
  
  
Initially, in my example, I see <quote>ircbot: /home/PS1/hef/projects/strup/irc/main.cpp:45: int main(int, char**): Assertion `file.is_open()' failed.</quote> because I havn't created a file yet.
+
Initially, in my example, I see <code>ircbot: /home/PS1/hef/projects/strup/irc/main.cpp:45: int main(int, char**): Assertion `file.is_open()' failed.</code> because I havn't created a file yet.
  
==== working with service files ====
+
=== Working with Service Files ===
  
 
If you tried to see your service user's home directory earlier, you may have noticed it didn't work. switch to your service accounts users.
 
If you tried to see your service user's home directory earlier, you may have noticed it didn't work. switch to your service accounts users.
  
 
     sudo su strup
 
     sudo su strup
 +
 +
 +
Your are now running as the user ''strup'', but you are now in the user's home directory. Running <code>cd</code> without parameters will switch to the current user's home directory.
 
     cd
 
     cd
 +
  
 
My example requires a file from project gutenberg.
 
My example requires a file from project gutenberg.
Line 64: Line 75:
  
 
     exit
 
     exit
 
  
 
=== Enabling Your Service ===
 
=== Enabling Your Service ===
Line 70: Line 80:
 
After verifying that the service is starting and running correctly:
 
After verifying that the service is starting and running correctly:
  
     sudo systemctl status strup
+
     sudo systemctl status strup.service
  
 
Enable the service
 
Enable the service
  
     sudo systemctl enable strup
+
     sudo systemctl enable strup.service
 
 
  
 
The service is now configured to start when the machine reboots.
 
The service is now configured to start when the machine reboots.

Latest revision as of 05:00, 7 August 2014

Why do it this way?

  • Your service will be watchdogged by systemd
  • Your service will recover from network and power outages.
  • It allows other members to help maintain your service, or disable it cleanly if it need attention.
  • It's great for IRC bots

Working on Sally

All members have shell and admin access on sally.

   ssh [email protected]

You also have sudo (root user) access.

   sudo uname -a

Create Your Service Environment

Create a System Account for your Service

   sudo useradd strup -b /srv/ -m -r


The creates a user named strup. The -b indicates the base home directory is /srv/, and therefore the system accounts home directory is /srv/strup/

Working with SystemD

Create a unit file: /etc/systemd/system/strup.service

[Unit]
Description=Strup IRC bot

[Service]
Type=simple
User=strup
Group=strup
ExecStart=/usr/local/bin/ircbot
WorkingDirectory=/srv/strup/

[Install]
WantedBy=multi-user.target

Start Your Service and Check for Log Output

   sudo systemctl start strup.service
   sudo systemctl status strup.service

To follow log output:

   sudo journalctl -u strup.service -f


Initially, in my example, I see ircbot: /home/PS1/hef/projects/strup/irc/main.cpp:45: int main(int, char**): Assertion `file.is_open()' failed. because I havn't created a file yet.

Working with Service Files

If you tried to see your service user's home directory earlier, you may have noticed it didn't work. switch to your service accounts users.

   sudo su strup


Your are now running as the user strup, but you are now in the user's home directory. Running cd without parameters will switch to the current user's home directory.

   cd


My example requires a file from project gutenberg.

   wget http://www.gutenberg.org/cache/epub/11/pg11.txt

Don't forget to switch back to your normal user when done

   exit

Enabling Your Service

After verifying that the service is starting and running correctly:

   sudo systemctl status strup.service

Enable the service

   sudo systemctl enable strup.service

The service is now configured to start when the machine reboots.