大网数据广告活动
服务器运维问答

systemd timer for a process that runs every 5 minutes, but may run longer than 5 minutes

· 52CE 我爱测

I did a little searching and didn't see any similar questions. I have a job that should run once every five minutes. There should only be one job running at a time. If the program takes more than 5 minutes to run, it should start…

问题原文

原帖链接 · Paul T. · CC BY-SA 3.0

I did a little searching and didn't see any similar questions.

I have a job that should run once every five minutes.

There should only be one job running at a time.

If the program takes more than 5 minutes to run, it should start again immediately, not wait.

Most of the time, the job will complete in under a minute, occasionally it must do things that might take as long as 20 minutes.

I currently run this job as a systemd service, and the job is a shell script that calls the actual program in a loop, and sleeps between iterations (5 minutes - last run time). It works great.

#!/bin/sh
#
# Sample executable repeat-loop
#
# Run my job in a loop.  If it takes less than max_delay seconds
# to run, sleep until it's time to repeat.  If, for some reason, it took longer, just start
# over again immediately.

max_delay=300                       # maximum delay between runs

# Don't allow multiple copies of this script to run
[ "${FLOCKER}" != "$0" ] && exec env FLOCKER="$0" flock -en "$0" "$0" "$@"

restart=0
now=0

while true ; do
    now=`date -u '+%s'`
    if [ $restart -gt $now ] ; then
        sleep `expr $restart - $now`
    fi
    restart=`expr $now + ${max_delay}`

    echo "my job runs here and may take between 30 seconds and 20 minutes to execute"
done

Since I am a bloody idiot, I was thinking it would be great to change this to a oneshot service, and have systemd timers fire off.

I haven't been able to figure out how to emulate this behavior using OnUnitActiveSec or OnUnitInactiveSec...

OnUnitActiveSec is going to fire up my oneshot service every x seconds. OnUnitInactiveSec is going to fire up my oneshot x seconds after the last time the oneshot terminated.

Any thoughts besides "Hey, your shell script works great, why beat yourself with a dead horse?"

社区高分解答

原帖链接 · Paul T. · CC BY-SA 3.0

I should have tested better, rather than relying on my (incorrect) interpretation of the docs.

Creating a timer file and using OnUnitActiveSec pointing to a service file that is Type=simple does exactly what I want. The timer will be invoked every 5 minutes, if the process runs longer than 5 minutes, it will be invoked (almost) immediately. It will not be double-invoked.

It's important to use simple instead of oneshot (per the docs).

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-05-18)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。