R
R
r3star2018-03-10 13:58:44
bash
r3star, 2018-03-10 13:58:44

How to create a Unit without info about the system or is it easier to finish the script?

Good day to everyone, I continue my acquaintance with bash, the question arose whether it is possible to create a Unit that will take information about the system or is it easier to change something in the script.
it is necessary that the last 10 seconds of the memory state and the CPU load are recorded in the logs =) that is, constantly =)
and one more thing, how to make the information about the networks also get into the logs like I wrote After=network.service , but it does not work =( the log file is empty =(

[Unit]
Description=make log file ^^
After=network.service
 
[Service]
 
ExecStart=/bin/bash '/restar/scripts/restar2.sh'
Type=forking
 
[Install]
WantedBy=multi-user.target
Alias=bash.service

and here is the script that it runs
#!/bin/bash

for i in $(free > /restar/logs/memory.txt | cat /proc/stat > /restar/logs/cpu.txt) {1..10}  
do
sleep 1
done


for iii in $(journalctl -p err > /restar/logs/errors.txt | ip a > /restar/logs/seti.txt)  
do
echo "all ok"
done

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xotkot, 2018-03-10
@r3star

the question arose whether it is possible to create a Unit that will take information about the system or is it easier to change something in the script.

the main purpose of a unit is to start, stop and monitor your script, in general state management
it's not for this at all, and target and not service
you are doing something really fucking, especially the perversion with pipes (pipe - "|") touches ))
so you decide, constantly or the last 10 seconds,
if only the last 10 seconds, then why do you need a unit?
If you constantly, for example, read information from the utilities you need once a second, then it is probably better to use a systemd timer for this, which will periodically launch your unit, which in turn will control your script from several commands:
free >> /restar/logs/memory.txt &
cat /proc/stat >> /restar/logs/cpu.txt &
...

useful links
Working with Journal and not only
systemd/Timers

S
Saboteur, 2018-03-10
@saboteur_kiev

You will find out how your monitoring works, read the journalctl help to understand what exactly you want to watch, and then you can bite something with your head. This is not a question

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question