Pages

Showing posts with label journalctl. Show all posts
Showing posts with label journalctl. Show all posts

Sunday, September 23, 2018

Fedora 28 : Start a service daemon with Python.

In this tutorial I will starting one service using systemctl , python and systemd. First, you need to create a file named testpython.service .
[mythcat@desk system]# cd /etc/systemd/system/
[root@desk system]# vim testpython.service
This file is a configuration file for this service.
[Unit]
Description=Python Service
After=multi-user.target
[Service]
Type=simple
ExecStart=/usr/bin/python /home/mythcat/test_service.py
[Install]
WantedBy=multi-user.target
Create the python file for this service. I named test_service.py .
[root@desk system]# exit
exit
[mythcat@desk system]$ cd ~
[mythcat@desk ~]$ vim test_service.py

#!/usr/bin/env python

import logging
import time

logging.basicConfig(level="INFO")

while True:
    logging.info("Hi")
    time.sleep(3)
Change permissions file for this python file and testpython.service, see:
[mythcat@desk ~]$ chmod 764 test_service.py
Because you run this service with systemd then selinux will send you error, fix that:
[mythcat@desk ~]$ chcon -t bin_t ~/test_service.py
Reload all services and start your service with this commands:
[root@desk system]# systemctl daemon-reload
[root@desk system]# systemctl start  testpython.service
[root@desk system]# systemctl status  testpython.service
● testpython.service - Python Service
   Loaded: loaded (/etc/systemd/system/testpython.service; enabled; vendor>
   Active: active (running) since Sat 2018-09-22 21:36:23 EEST; 5s ago
 Main PID: 7213 (python)
    Tasks: 1 (limit: 2102)
   Memory: 5.7M
   CGroup: /system.slice/testpython.service
           └─7213 /usr/bin/python /home/mythcat/test_service.py

Sep 22 21:36:23 desk systemd[1]: Started Python Service.
Sep 22 21:36:24 desk python[7213]: INFO:root:Hi
Sep 22 21:36:27 desk python[7213]: INFO:root:Hi
You can use the journalctl command to see the output of this service:
[root@desk system]# journalctl -u testpython.service 
-- Logs begin at Sat 2018-09-22 20:40:06 EEST, end at Sat 2018-09-22 21:31:07 EEST. --
Sep 22 20:40:06 desk python[6232]: INFO:root:Hi
Sep 22 20:40:09 desk python[6232]: INFO:root:Hi
Sep 22 20:40:12 desk python[6232]: INFO:root:Hi
Sep 22 20:40:15 desk python[6232]: INFO:root:Hi
Sep 22 20:40:18 desk python[6232]: INFO:root:Hi
Sep 22 20:40:21 desk python[6232]: INFO:root:Hi
Sep 22 20:40:24 desk python[6232]: INFO:root:Hi
Sep 22 20:40:27 desk python[6232]: INFO:root:Hi
Sep 22 20:40:30 desk python[6232]: INFO:root:Hi
Let's see the result:

Tuesday, March 28, 2017

The journalctl command.

This is a good Linux command for Linux maintenance.
The first step is to read the documentation:
[root@localhost mythcat]# man journalctl
JOURNALCTL(1)                     journalctl                     JOURNALCTL(1)

NAME
       journalctl - Query the systemd journal

SYNOPSIS
       journalctl [OPTIONS...] [MATCHES...]

DESCRIPTION
       journalctl may be used to query the contents of the systemd(1) journal
       as written by systemd-journald.service(8).

       If called without parameters, it will show the full contents of the
       journal, starting with the oldest entry collected.

       If one or more match arguments are passed, the output is filtered
       accordingly. A match is in the format "FIELD=VALUE", e.g.
       "_SYSTEMD_UNIT=httpd.service", referring to the components of a
       structured journal entry. See systemd.journal-fields(7) for a list of
       well-known fields. If multiple matches are specified matching different
       fields, the log entries are filtered by both, i.e. the resulting output
       will show only entries matching all the specified matches of this kind.
       If two matches apply to the same field, then they are automatically
       matched as alternatives, i.e. the resulting output will show entries
       matching any of the specified matches for the same field. Finally, the
       character "+" may appear as a separate word between other terms on the
       command line. This causes all matches before and after to be combined
       in a disjunction (i.e. logical OR).
       ...
The self-maintenance method is to vacuum the logs.
This helps you with free space into your Linux OS.
For example, I got 3 Gigabytes of data in just 3 days.
# journalctl --vacuum-time=3d
Vacuuming done, freed 3.7G of archived journals on disk. To clean up this you can use the command into several ways:
  • by time
  • journalctl --vacuum-time=2d
  • retain only the past 500 MB
  • journalctl --vacuum-size=500M
As you know: The is an init system used in Linux distributions to bootstrap the user space and manage all processes subsequently. The journald daemon handles all of the messages produced by the kernel, initrd, services, etc. You can use the journalctl utility, which can be used to access and manipulate the data held within the journal. Let's start with some examples: How to see the configuration file for this process:
[root@localhost mythcat]# cat /etc/systemd/journald.conf
Also, you can see the status of this service:
[root@localhost mythcat]# systemctl status  systemd-journald
● systemd-journald.service - Journal Service
   Loaded: loaded (/usr/lib/systemd/system/systemd-journald.service; static; vendor preset: disabled)
   Active: active (running) since Tue 2017-03-28 09:12:20 EEST; 1h 8min ago
     Docs: man:systemd-journald.service(8)
           man:journald.conf(5)
 Main PID: 803 (systemd-journal)
   Status: "Processing requests..."
    Tasks: 1 (limit: 4915)
   CGroup: /system.slice/systemd-journald.service
           └─803 /usr/lib/systemd/systemd-journald

Mar 28 09:12:20 localhost.localdomain systemd-journald[803]: Runtime journal (/run/log/journal/) is 8.0M,
max 371.5M, 363.5M free.
Mar 28 09:12:20 localhost.localdomain systemd-journald[803]: Journal started
Mar 28 09:12:22 localhost.localdomain systemd-journald[803]: System journal (/var/log/journal/) is 3.9G,
max 4.0G, 23.8M free.
Mar 28 09:12:23 localhost.localdomain systemd-journald[803]: Time spent on flushing to /var is 915.454ms
I hope this article will help you with Linux maintenance