简体   繁体   English

没有crontab怎么每天发一个信号?

[英]How can I send a signal every day without crontab?

Context:语境:

I am working on an embedded openwrt on a retry system for http request and certificate peremption checking.我正在重试系统上开发嵌入式 openwrt,用于 http 请求和证书强制检查。

We don't trust our web server and want to make retry GET HTTP on file every days until it's downloaded.我们不信任我们的 web 服务器,并希望每天重试 GET HTTP 文件,直到它被下载。

We also want to download files at precise hours.我们还想在精确的时间下载文件。

During regular times, the program is:在正常时间,节目是:

  • managing the state of Leds and.network state管理 LED 的 state 和.network state
  • receive messages from message queues to execute web request and.network administration从消息队列接收消息以执行 web 请求和.network 管理
  • It monitors 2 other process one bluetooth and one Zigbee that may crash.它监视 2 个其他进程,一个蓝牙和一个可能崩溃的 Zigbee。
  • By the time we go in prod it will probably do other things.到我们生产 go 时,它可能会做其他事情。

Problematic:有问题的:

I want to program in C a signal every day to get asynchronous events.我想每天在C编程一个信号来获取异步事件。 So i basically want crontab without crontabs.所以我基本上想要没有 crontabs 的 crontab。

The signal I want to use is the ones used in IPC (I don't know other way to create asynchronous behaviours on linux): https://www.man7.org/linux/man-pages/man7/signal.7.html我想使用的信号是 IPC 中使用的信号(我不知道在 linux 上创建异步行为的其他方法): https://www.man7.org/linux/man-pages/man7/signal.7.html

I know how to use crontab but i would prefer to do everything in C as it would make my architecture simpler for my coworkers who don't use linux.我知道如何使用 crontab,但我更愿意在 C 中做所有事情,因为它会让我的架构对于不使用 linux 的同事来说更简单。

Question: What is the proper way in C on Linux to get dayly/periodic signal?问: Linux上C获取日/周期信号的正确方法是什么?

Many thanks,非常感谢,

I don't know other way to create asynchronous behaviors on Linux我不知道在 Linux 上创建异步行为的其他方法

Linux itself seems not to support anything that is similar to what you want. Linux 本身似乎不支持与您想要的类似的任何东西。

To perform certain actions at a certain time, some program must be started and run in the background until the action shall be performed.要在特定时间执行特定操作,必须启动某些程序并在后台运行,直到执行该操作。 If the action shall be performed cyclically, the program must be running permanently.如果要循环执行操作,则程序必须永久运行。

Using a crontab has the advantage that only one program ( cron ) is running in the background even if you have hundreds of different actions in your crontab .使用crontab的好处是只有一个程序 ( cron ) 在后台运行,即使您的crontab中有数百个不同的操作。 However, one program ( cron ) is running in the background permanently.但是,一个程序 ( cron ) 在后台永久运行。

The same is true for systemd . systemd也是如此。

If you don't want to use such a tool, your program must run in the background permanently.如果您不想使用这样的工具,您的程序必须永久地在后台运行。

timer_create

This can be used if you require a quite high precision (for example less than one second).如果您需要相当高的精度(例如小于一秒),则可以使用它。

If you don't need a high precision and you don't want cron or similar, I would do something like this:如果你不需要高精度并且你不想要cron或类似的东西,我会做这样的事情:

seconds_per_day = 60*60*24;
next_time = time(NULL) + seconds_per_day;
while(1)
{
    usleep(5000000);
    if(time(NULL) >= next_time)
    {
        perform_action();
        next_time += seconds_per_day;
    }
}

In the example I use usleep() to wait for 5 seconds.在示例中,我使用usleep()等待 5 秒。 Using a longer time (eg 10 seconds) will reduce the CPU power consumed by your program running in the background - and make the exact point in time when you trigger your action more imprecise.使用更长的时间(例如 10 秒)将减少后台运行的程序所消耗的 CPU 功率 - 并使您触发操作的准确时间点更加不精确。

The example simply triggers one action every 24h.该示例仅每 24 小时触发一次操作。

However, you can use the same principle to trigger some action at a certain time (eg 11:30pm) or multiple actions at different times...但是,您可以使用相同的原理在特定时间(例如晚上 11:30)触发某些动作或在不同时间触发多个动作......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM