Yes Yes Yes Yes, everyone Hello, this is ○○. Yes, Youtube will improve during the self-restraint period, thank you for always funny videos.
How to use systemd to start a daemon only at a specific time.
It seems that there is unexpected demand, but I could not find a related article, so I will write down trial and error as a memorandum.
I want to start the daemon (hereinafter referred to as Mr.daemon, also known as Daemon) only between 8:00 and 23:00.
I want to stop other times to reduce power consumption.
By the way, the daemon I want to operate is my own, and it keeps streaming audio using HLS with ffmpeg during startup.
As an aside, when I investigated what is different between services and daemons, it seems that both are resident programs, services are used in Windows systems, and daemons are used in Unix systems.
I will write about the problems later, but for the time being.
I think there are two main ways to start Daemon using systemd.
-** Automatic startup ** systemctl enable {Mr.daemon}
-** Manual start ** systemctl start {Mr.daemon}
However, you cannot specify the time with these methods. With automatic startup, it always starts without any questions, and it goes without saying that manual startup is necessary. Therefore, I think that the following method can be used to specify the time.
-** Timer ** {Mr.daemon} .timer
0 8 * * * systemctl start {Mr.daemon}systemd timerYou can start Daemon at a specific time by using the timer function of systemd.
bash:{Mr.daemon}.timer
[Unit]
#Any description
Description=Run {Mr.daemon}.service
[Timer]
#Time you want to start
OnCalendar=*-*-* 8:00:00 #year-month-date hour:minute:second
#If the OS is sleeping at the time specified by OnCalender, the daemon will be started at the next startup.
Persistent=true
[Install]
WantedBy=timers.target
After writing the unit file like this, enable the timer with sudo systemctl enable {Mr.daemon} .timer.
cronYou can do the same with administrator-privileged cron by running systemctl start at a specified time.
sudo crontab -e0 8 * * * systemctl start {Mr.daemon}It seems that the systemd timer can start but cannot stop.
So use cron anyway to stop it.
Just add the following to the cron with administrator privileges.
0 23 * * * systemctl stop {Mr.daemon}
Well, when I implemented it by the above method, there were some inconveniences. That is, the daemon will not start after rebooting between 8:00 and 23:00. Of course, only the start and end triggers are set, so if you restart at 9 o'clock for maintenance, for example, Daemon will stay asleep until 8 o'clock the next day unless you start it manually. This is not enough for a daemon that runs only at specific times.
I decided to play with the daemon startup script.
stream.sh
###Postscript part
NOW=`date +%H` #Get the current time
if [ $NOW -ge 23 -o $NOW -lt 8 ]; then #The current time is 23:00~If it's between 8 o'clock the next day
systemctl stop {Mr.daemon} #Stop the daemon
exit 0 #Successful completion
fi
###
function fork() {
#processing
}
fork > /dev/null 2>&1 </dev/null &
echo $! > /run/{Mr.daemon}.pid
In the postscript part, if this script runs between 23:00 and 8:00 the next day, it will exit the process.
In this state, set sudo systemctl enable {Mr.daemon} to always start automatically.
It starts automatically between 8:00 and 23:00, but the automatic start is canceled between 23:00 and 8:00 the next day, so Daemon stays asleep.
If you combine the systemd timer that you tried first with the cron that stops the daemon, you will have Daemon running at a specific time.
Initially, there was only ʻexit in ʻif, but in that state, an error was thrown when the startup failed.
It was the same even if I returned normal termination as ʻexit 0. It was until then that there was no problem, but I felt uncomfortable, so when I wrote systemctl stop {Mr.daemon}`, it was treated as startup cancellation and no error occurred.
cron?I haven't tried it, but if you don't mess with the daemon startup script, you can do the same with cron.
@reboot if [ $NOW -lt 23 -a $NOW -ge 8 ]; then systemctl start {Mr.daemon}; fi
If you add it to the administrator authority cron in this way, Daemon will also start when the OS is started between 23:00 and 8:00.
I feel that cron is smarter because it does not start wastefully, but it was troublesome to adjust the order related to systemd such as ʻAfterand dependencies, so I decided to start it automatically withsystemd`. did.
None of these methods seem smart, so if anyone knows a good method, please teach me.
Recommended Posts