Found an example of a demon, launched from the console.Question - how to manage it, in particular, to stop? How to restart in case of an error? And how to make it a background so that you can write commands in the console? Thank you.
while true;do
    begin=`date +% s`
    php /var/www/html/rules/script.php
    end=`date +% s`
    if[$(($end - $begin)) -lt 5];then
        sleep $(($begin + 5 - $end))

3 Answers 3

Design your script as a systemd service.
In the future, you can manage it like this:
systemctl stop/start/restart/status name.service
man bg, fg, jobs, systemctl
  • if you arrange it as a system unit, then nothing is needed except it :) – Hillbilly Manlove Jun 13 '19 at 20:40
  • [[pfg21]], well, not everything at once.First, we will demonstrate how to complete the script correctly :) – Odd54 Jun 13 '19 at 21:16
1.demon should be written in such a way as not to write to the console, but immediately write to the log file(or have a separate option for debugging to the console).

2.The daemon should be able to process signals for correct termination(the same -15), or be ready for an incorrect shutdown.

3.A demon should be able to leave its PID so that it is convenient for it to write a wrapper to send signals.

p.s.signals can be sent via kill
  • this is if the management daemon is not able to do anything.
    The aforementioned system will deal with the sponsored process itself.
    – Hillbilly Manlove Jun 13 '19 at 20:39
  • [[pfg21]], Ein OS, Ein Verkäufer, Ein Init. – Wild Monkey Jun 14 '19 at 00:09
  • >a demon should be written this way
    >A demon must be able to leave its PID
    With systemd, this is not needed, for example.There, stdout and stderr are automatically redirected to the logs, and the PID of the main and all child processes are automatically tracked.
    – Wandering99 Jun 14 '19 at 11:53
  • systemd is good, but nobody canceled recommendations on writing demons.
    The question is not how to start an incomprehensible process, but a person has found a demon and wants to figure it out, learn to control it.I say that for this control should also be supported by the demon.
    Minimal recommendations for writing demons are as follows:

    * Fork from the parent process, so as not to depend on its closure.In Linux, the process will automatically go under init and will work in the background, regardless of the process that started it.
    * setsid - Make a new session.And disconnect from the tty of the parent process
    * Catch signals - Ignore or process signals for correct completion and additional actions(for example, re-read the config without restarting).
    * Fork again so that the process becomes the main one in its session.Then you can even get TTY again, already for yourself(only the session leader can have tty)
    * chdir - Change the process directory to the desired one
    * umask - If you need to work with files.
    * close - Close all file descriptors(including stdout, stderr, stdin) that could have come from the parent process open.Then you can open only the ones you need.
    – Gifted Grebe Jun 14 '19 at 13:39