我有一个已激活的 systemd 用户单元,并且有一个每天在特定时间运行的计时器。我遇到的问题是,如果服务器重新启动(我不知道什么时候会发生),服务就会停止运行,然后我最终注意到该进程有一段时间没有运行,我登录...然后它启动当我登录并开始运行时。
如何才能在服务器重新启动时自动恢复计时器/服务,从而避免进程随机中断?
服务文件
[Unit]
Description=daily process update
AssertHost=ai1
[Service]
WorkingDirectory=/st2/process
ExecStart=/home/user/.venv/env/bin/python main.py --update_data
[Install]
WantedBy=default.target
定时器文件
[Unit]
Description=run process update daily
AssertHost=myserver
[Timer]
OnCalendar=*-*-* 14:20:00
Persistent=true
[Install]
WantedBy=timers.target
使用以下脚本安装
# systemd does not support symlinks across partitions, copy service and timer to user dir
# enable timer which calls service by the same name
# https://askubuntu.com/questions/1083537/how-do-i-properly-install-a-systemd-timer-and-service
cp ./stock-update.service /home/user/.config/systemd/user/process-update.service
cp ./stock-update.timer /home/user/.config/systemd/user/process-update.timer
systemctl --user disable process-update.timer
systemctl --user daemon-reload
systemctl --user enable --now process-update.timer
答案1
您可以启用挥之不去loginctl enable-linger $USER
通过为用户运行来获取 systemd 用户实例。