{"id":798,"date":"2025-12-10T18:18:12","date_gmt":"2025-12-11T00:18:12","guid":{"rendered":"https:\/\/kop.lat\/blog\/?p=798"},"modified":"2025-12-10T18:18:13","modified_gmt":"2025-12-11T00:18:13","slug":"how-to-create-a-systemd-service-in-linux","status":"publish","type":"post","link":"https:\/\/kop.lat\/blog\/how-to-create-a-systemd-service-in-linux\/","title":{"rendered":"How to create a Systemd Service in Linux"},"content":{"rendered":"\n<p>This guide applies to all modern Linux distributions that use systemd (Ubuntu, Debian, Fedora, CentOS, RHEL, Arch, etc.).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Prepare the application or script<\/h3>\n\n\n\n<p>Place the executable or script in a permanent location, such as <code>\/opt\/myapp\/<\/code> or <code>\/usr\/local\/bin\/<\/code>.<\/p>\n\n\n\n<p>Example (a simple Python HTTP server):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo mkdir -p \/opt\/myapp\nsudo nano \/opt\/myapp\/server.py<\/code><\/pre>\n\n\n\n<p>Content of <code>server.py<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/env python3\nimport http.server\nimport socketserver\n\nPORT = 8000\nhandler = http.server.SimpleHTTPRequestHandler\n\nwith socketserver.TCPServer((\"\", PORT), handler) as httpd:\n    httpd.serve_forever()<\/code><\/pre>\n\n\n\n<p>Make it executable:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo chmod +x \/opt\/myapp\/server.py<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. (Recommended) Create a dedicated system user<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo useradd --system --no-create-home --shell \/usr\/sbin\/nologin myappuser\nsudo chown -R myappuser:myappuser \/opt\/myapp<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. Create the systemd unit file<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/systemd\/system\/myapp.service<\/code><\/pre>\n\n\n\n<p>Standard unit file template:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;Unit]\nDescription=Example application service\nAfter=network.target\nWants=network-online.target\n\n&#91;Service]\nType=simple\nUser=myappuser\nGroup=myappuser\nWorkingDirectory=\/opt\/myapp\nExecStart=\/usr\/bin\/python3 \/opt\/myapp\/server.py\nRestart=always\nRestartSec=5\n\n# Optional security directives\nPrivateTmp=true\nProtectSystem=strict\nProtectHome=true\nNoNewPrivileges=true\n\n&#91;Install]\nWantedBy=multi-user.target<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. Activate the service<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl daemon-reload\nsudo systemctl enable myapp.service    # enable at boot\nsudo systemctl start myapp.service     # start immediately<\/code><\/pre>\n\n\n\n<p>Or combine enable and start:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl enable --now myapp.service<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. Verify operation<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl status myapp.service\njournalctl -u myapp.service -f         # follow logs in real time<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6. Common management commands<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl start myapp.service\nsudo systemctl stop myapp.service\nsudo systemctl restart myapp.service\nsudo systemctl reload myapp.service    # only if the program supports reload\nsudo systemctl disable myapp.service<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">7. Modify settings safely (recommended method)<\/h3>\n\n\n\n<p>Instead of editing the main file directly:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl edit myapp.service<\/code><\/pre>\n\n\n\n<p>This creates an override file that persists across updates.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">8. Open required ports (if the service listens on the network)<\/h3>\n\n\n\n<p>Ubuntu (ufw):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo ufw allow 8000\/tcp<\/code><\/pre>\n\n\n\n<p>Fedora\/RHEL\/CentOS (firewalld):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo firewall-cmd --add-port=8000\/tcp --permanent\nsudo firewall-cmd --reload<\/code><\/pre>\n\n\n\n<p>The service is now installed, automatically started at boot, monitored, and will restart on failure.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This guide applies to all modern Linux distributions that use systemd (Ubuntu, Debian, Fedora, CentOS, RHEL, Arch, etc.). 1. Prepare the application or script Place the executable or script in a permanent location, such as \/opt\/myapp\/ or \/usr\/local\/bin\/. Example (a simple Python HTTP server): Content of server.py: Make it executable: 2. (Recommended) Create a dedicated [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":226,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[20,4,40,111],"tags":[143,5,119,120],"_links":{"self":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/798"}],"collection":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/comments?post=798"}],"version-history":[{"count":1,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/798\/revisions"}],"predecessor-version":[{"id":800,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/798\/revisions\/800"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media\/226"}],"wp:attachment":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media?parent=798"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/categories?post=798"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/tags?post=798"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}