search

Home  >  Q&A  >  body text

python - I have a second-level task. How to handle the crond service in Linux? It takes at least 1 minute. PHP

I need to receive a piece of data in real time for processing. It must be in seconds. How should I process it?

習慣沉默習慣沉默2795 days ago628

reply all(4)I'll reply

  • 習慣沉默

    習慣沉默2017-05-16 13:03:44

    If the system uses systemd, you can use systemd.timer to set seconds or even millisecond-level scheduled tasks.
    Specific reference: here

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-16 13:03:44

    The default minimum unit of crontab is minutes, but it can also be implemented in some tricky ways. For example, execute every 10 seconds:

    * * * * * php /home/test.php
    * * * * * sleep 10; php /home/test.php
    * * * * * sleep 20; php /home/test.php
    * * * * * sleep 30; php /home/test.php
    * * * * * sleep 40; php /home/test.php
    * * * * * sleep 50; php /home/test.php

    Per second, it can also be achieved in the above way, but it is a lot and is not recommended, so using a shell script is a better choice.

    #!/bin/bash  
      
    step=1 #间隔的秒数,不能大于60  
      
    for (( i = 0; i < 60; i=(i+step) )); do  
        $(php '/home/test.php')  
        sleep $step  
    done  
      
    exit 0  

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-05-16 13:03:44

    crontab can’t handle it in seconds, you can only use the resident process to solve it

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-16 13:03:44

    The minimum execution time granularity of crontab is one minute. For seconds, you can start an infinite loop to continuously obtain data.
    while(true){
    file_get_contents('get_data_controller');
    sleep(1);
    }

    reply
    0
  • Cancelreply