简体   繁体   中英

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.

We don't trust our web server and want to make retry GET HTTP on file every days until it's downloaded.

We also want to download files at precise hours.

During regular times, the program is:

  • managing the state of Leds and.network state
  • receive messages from message queues to execute web request and.network administration
  • It monitors 2 other process one bluetooth and one Zigbee that may crash.
  • By the time we go in prod it will probably do other things.

Problematic:

I want to program in C a signal every day to get asynchronous events. So i basically want crontab without crontabs.

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

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.

Question: What is the proper way in C on Linux to get dayly/periodic signal?

Many thanks,

I don't know other way to create asynchronous behaviors on Linux

Linux itself seems not to support anything that is similar to what you want.

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 . However, one program ( cron ) is running in the background permanently.

The same is true for 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:

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. 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.

The example simply triggers one action every 24h.

However, you can use the same principle to trigger some action at a certain time (eg 11:30pm) or multiple actions at different times...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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