I want to manage systemd by time zone! !!

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.

Thing you want to do

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.

The first method I came up with

I will write about the problems later, but for the time being.

Start-up

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

systemd timer

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

Administrator privileges cron

You can do the same with administrator-privileged cron by running systemctl start at a specified time.

  1. sudo crontab -e
  2. Add right 0 8 * * * systemctl start {Mr.daemon}

Stop

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}

Problems with the above method

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.

Then what should we do

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.

Commentary

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.

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

Finally

None of these methods seem smart, so if anyone knows a good method, please teach me.

Recommended Posts

I want to manage systemd by time zone! !!
I want to understand systemd roughly
[Python] I want to manage 7DaysToDie from Discord! 1/3
I want to be cursed by a pretty girl every time I sudo! !!
I want to sell Mercari by scraping python
[Python] I want to manage 7DaysToDie from Discord! 2/3
I want to restart CentOS 8 on time every day.
I want to solve Sudoku (Sudoku)
I want to save the photos sent by LINE to S3
[Go] I want to separate endpoints by Read / Write to DB
I want to convert an ISO-8601 character string to Japan time
I want Sphinx to be convenient and used by everyone
I want to create a Dockerfile for the time being.
I want to scrape images to learn
I want to do ○○ with Pandas
I want to copy yolo annotations
I want to debug with Python
I want to record the execution time and keep a log.
For the time being, I want to convert files with ffmpeg !!
I want to pin Spyder to the taskbar
I want to detect objects with OpenCV
I want to output to the console coolly
I want to print in a comprehension
I want to separate the processing between test time and production environment
I want to handle the rhyme part1
Set the time zone to Japan Standard Time
I want to know how LINUX works!
I want to blog with Jupyter Notebook
I want to handle the rhyme part3
I want to use jar from python
I want to build a Python environment
I want to use Linux on mac
I want to pip install with PythonAnywhere
I want to analyze logs with Python
I want to play with aws with python
Time zone
I want to use IPython Qt Console
I want to display the progress bar
[Google Colab] I want to display multiple images side by side in tiles
I want to make an automation program!
I want to embed Matplotlib in PySimpleGUI
Python: I want to measure the processing time of a function neatly
[Question] I want to scrape a character string surrounded by unique tags!
I want to handle the rhyme part2
I want to develop Android apps on Android
I want CAPTCHA to say HIWAI words
I want to handle the rhyme part5
I want to handle the rhyme part4
I want to make a music player and file music at the same time
I want to store the result of% time, %% time, etc. in an object (variable)
I want to do machine learning even without a server --Time Series Edition -
I want to make matplotlib a dark theme
I want to connect to PostgreSQL from various languages
I tried to program bubble sort by language
I want to do Dunnett's test in Python
I want to have recursion come to my mind
I want to easily create a Noise Model
I want to use MATLAB feval with python
I want to pin Datetime.now in Django tests
I want to analyze songs with Spotify API 2
I want to INSERT a DataFrame into MSSQL