CRON run every 10 seconds

You need to run the crown every 10 seconds. The answer was found only in what would make 5 instances of the task and a slip for 10,20,30,40 and 50 seconds, respectively. But the problem is that I can't make a slip in the crown. Tell me how.

enter a description of the image here

 2
Author: Danemark, 2018-07-06

3 answers

You can even do this:

watch --interval=10 path_to_script.sh

There is a problem of tracking this process, etc. I would still like to show you one of the ways that is much easier than something you decided to contact.

Cron itself is not very suitable for this task, because we know that the minimum work interval is 1 minute, but the disadvantages of cron for your task are not uploaded, if your worker starts to think, it will create an avalanche effect., that at one point, without proper blocking at the script level, will cause increased resource usage and create unpredictable results, especially if the worker is working with the database.

I would recommend you for your task SystemD (if of course you have it or have the ability to install), and I will tell you why:

  1. The ability to specify any time interval, even every second
  2. Writes the script output to its log and the ability to do this is all view
  3. You can view the task status systemctl status your-service
  4. You can view the date of the last launch and the planned launch of systemctl list-timers
  5. You can limit resources and the number of simultaneous processes, by default, one

Yes, of course there are disadvantages, you will have to create 2 files, in one specify the command to start the service and make a brief description of it, and in the other (.timer) specify the start time, against crontab-e with writing the command in the editor.

Creating two files:

# /etc/systemd/system/project-worker.service

[Unit]
Description=Runs worker
Wants=project-worker.timer

[Service]
ExecStart=/usr/bin/php /path_to_worker_from_root.php

[Install]
WantedBy=multi-user.target


# /etc/systemd/system/project-worker.timer

[Unit]
Description=Run project-worker every 10 seconds
Requires=project-worker.service

[Timer]
Unit=project-worker.service
OnCalendar=*:*:0/10

[Install]
WantedBy=timers.target

Running:

systemctl daemon-reload
systemctl enable project-worker.timer
systemctl start project-worker.timer

Checking the status:

systemctl status project-worker.timer

Useful commands

systemctl start SERVICE #запуск СЕРВИСА
systemctl stop SERVICE #остановка СЕРВИСА
systemctl status SERVICE #статус СЕРВИСА

systemctl list-timers  # статус всех таймеров

journalctl  # просмотр всех логов less
journalctl -u SERVICE  # просмотр логов сервиса
journalctl -f  # tail всех логов
journalctl -f -u SERVICE  # tail логов специфичного СЕРВИСА

This is far from complete information about setting up via SystemD, there is a very detailed description here here.

Of course, there is still the option that you can rewrite your daemon and make it run every 10 seconds on the level program code, and set up SystemD so that it will monitor its state, and if it "crashes", it will fix it and restart it.

The solution as a whole depends on the specific task.

 6
Author: Firepro, 2018-07-06 21:19:33

And who prevents you from making a script with sleep loop the necessary actions in it through while :;do;done and register this script in cron with the option @reboot

Script /usr/local/bin/w10

#!/bin/bash
while:;do
    wget -O /dev/null bot-nat.tk/worker.php?3
    sleep 10
done

sudo chmod +x /usr/local/bin/w10

And if you want to start at startup, then in crontab write

@reboot /usr/local/bin/w10

Or so:

* * * * * sleep 10;wget -O /dev/null bot-nat.tk/worker.php?3

But so it will run with a delay of 10 seconds. every minute

 2
Author: Ole Lukøje, 2018-07-06 20:35:21

I usually do this:

*/10 * * * * echo "Прошло пять минут"
 0
Author: Филипп, 2018-07-07 04:37:26