#AskFedi: is there a script / way to automate the restart of dnsmasq every 24 hours… on a VPS that runs on Debian 12?

(Funny how this would have sounded like a foreign language to me a mere year ago) 😆

Update: THANK YOU for the cronjob recommendation. Now looking for a tutorial that’s easy to follow for a newbie like me 😅

I really appreciate all the advice, YOU’RE THE BEST 🏆

#MySoCalledSudoLife

@elena a simple cron job should do
@mensrea thank you! Do you have recommendations about tutorials for newbies?
The Beginner's Guide to Linux Crontab: Automating Tasks

In the world of Linux system administration and development, repetitive tasks—like backups, log rotation, software updates, or script execution—can quickly become tedious. Manually running these tasks daily, weekly, or monthly wastes time and increases the risk of human error. Enter **crontab** (short for cron table), a powerful utility that lets you schedule tasks to run automatically at predefined intervals. Whether you’re a developer automating script runs, a sysadmin managing server maintenance, or a hobbyist streamlining your workflow, mastering crontab is a must. This guide will break down crontab fundamentals, syntax, usage, common use cases, troubleshooting, and best practices to help you automate tasks efficiently.

DotLinux.net

@mensrea sorry to bother you, but does this sound legit?

0 10 * * 1-5 systemctl restart dnsmasq

(I want it to run on weekdays at 10am)

I LOVED the explanation you sent, cronjobs feel like magic! sooo cool

@elena looks right from here
@elena no, wait: 0 0 10 * * 1-5
@mensrea @elena could you please explain the double zero? single seems correct https://crontab.guru/#0_10_*_*_1-5
Crontab.guru - The cron schedule expression generator

An easy to use editor for crontab schedules.

@def @elena single can be. the expression can have 5, 6, or 7 elements: seconds, minutes, hours, day of month, Month, Day of week, and Year. I've found including seconds is more predictable, and when you have a lot of jobs running it's good to start them off the hour and minute. eg, 10:12 05

@mensrea @elena Absolutely agree with using hour and minute to distribute the runs of cron jobs if there is lots of them. For example same job running on multiple servers, one might want avoid to running them at the same time.

As for the crontab format, I guess you are not talking about the standard cron distribution as that has exactly five time and date fields (https://man7.org/linux/man-pages/man5/crontab.5.html). TIL there are other variants, like nnCron, that support more params. Thank you.

crontab(5) - Linux manual page

@elena systemctl restart dnsmasq in a file under /etc/cron.daily ?
@bortzmeyer @elena yep, cron is the way to go. Also, curious, why do you need it?
@ed @elena Yes, removing dnsmasq would be a better idea :-)
@elena Could you make a cronjob out of it?

@elena What about a systemd timer which is triggering an oneshot service, which is restarting dnsmasq?
https://www.freedesktop.org/software/systemd/man/latest/systemd.timer.html
```
/etc/systemd/system/restart-dnsmasq.timer
[Unit]
Description=Daily restart of dnsmasq

[Timer]
OnCalendar=*-*-* 04:50:00
AccuracySec=4h
Persistent=false

[Install]
WantedBy=timers.target
```

systemd.timer

```
/etc/systemd/system/restart-dnsmasq.service
[Unit]
Description=Restart of dnsmasq
After=network-online.target
Requisite=network-online.target

[Service]
Type=oneshot
ExecStart=/usr/bin/systemctl restart dnsmasq.service
```

```
systemctl daemon-reload
systemctl enable --now restart-dnsmasq.timer
systemctl status restart-dnsmasq.timer
systemctl list-timers --all
```

@db_geek +1 for systems timers and against crontab. Crontab must die for this.
With timers you get easy log access. Additionally you can easily set it up&test, with cron you copy&paste the command there, wait, it fails, you debug and debug and have no chance in debugging the real execution and 90% of the time it's the missing path env... I hate it and get ptsd from too long debugging hours 😁 really love systemd timers, i think you noticed that 😉
@bws @db_geek crontab are freaking easy to use and debug if used on a proper OS.
I use cron everywhere with absolutely no effort except on latest Linux versions (latest CachyOS for example).
Replacing a one line crontab with 2 files and more than 10 lines of instructions is not progress either. It's enshittification.
@patpro @db_geek ok maybe i'm missing something?
You type 'crontab -e' and add/edit your line.
How to you execute it (in the environment it will run later)? And where do you get any logoutput from it (except in some email location)?

@bws @db_geek

I’ll take for granted that your system already runs crond or any derivative (cronie for example).

crontab creation / admin

There are user crontabs and system crontabs. User crontabs are setup with crontab -e (or crontab -u <login> -e).

System crontabs are in the file /etc/crontab and the directory /etc/cron.d, same syntax but with one more field used to set the user account that will run the line.
If you want everything in one file, use /etc/crontab, if you want a discrete file for each crontab, use /etc/cron.d.
On recent Linux distrib you’ll also find /etc/cron.{daily,hourly,monthly,weekly} directories so that you can dispatch scripts (not crontab) that you want to run daily, hourly, … It looks like an attempt to mimic BSD periodic and if you ask me, I think it’s a very bad idea to have named those directories cron.something when you must not store crontabs inside.
Scripts in those directories are run by corresponding crontabs in /etc/cron.d, for example:

$ cat /etc/cron.d/0hourly # Run the hourly jobs SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root 01 * * * * root run-parts /etc/cron.hourly

If you use system crontabs, stick with the file /etc/crontab and the directory /etc/cron.d, store your scripts elsewhere (/opt/foo, /usr/local/bar,…). You don’t need the ill-advised over-engineered /etc/cron.{daily,hourly,monthly,weekly} introduced by Linux distribs.

System crontabs are best for managed servers (a team manages hundreds servers with a config management tool, like Rudder). If you manage, alone and manually, a handful of servers then choice is yours but crontab -e does the trick pretty well.

Logging

If you want to log some output from the command ran by cron on your behalf, you can either pipe output to logger or add >/some/file 2>&1:

*/5 * * * * /path/to/my/script 2>&1 | logger @daily curl … |awk … | logger -t "baz" 0 10 20 * * /other/script >/tmp/script.output 2>&1

If you want (basic) logs about the cron service, grep you system logs or ask journalctl.

You’ll find here some decent Linux-centric documentation:
https://docs.rockylinux.org/10/fr/guides/automation/cronie/

cronie - Timed Tasks - Documentation

@patpro @db_geek still with this you cannot easily execute the script the _same_ way cron would.
And with all the custom logger stuff (does it log the cron run itself? No.) i have a hard time seeing the benfit over "bloated" systemd.
Especially when using a config system, those files are template generated anyway.

So i'd say lets agree to have different prefrences and i hope we both can continue chosing them as long as we like.

@bws @db_geek Well, if you want to run a command/script/whatever on your user crontab, it should run exactly the same in your shell and in your crontab. The only thing you have to check is that the PATH env for your crontab is properly setup.
In my whole sysadmin career (~ 25y), I’ve probably had to debug a handful crontabs by actually running them. Every thing else was conceived and debugged in the shell and put in crontab after.

@elena
crontab -e and add 5 2 * * * /usr/bin/systemctl restart dnsmasq (or whatever the service for dnsmasq would be called, you can test the command in the shell beforehand) should do it. That would restart it at 2:05 every day.

Though why would you need that, that sounds like a deeper issue at work :D

@elena First of all, if you need to restart it every 24h, something else is probably wrong. Why do you want to restart it?

@rozie too many requests… I see them in the logs.

I’ve increased the max number (sorry if I can’t remember the exact language) from the default 150 to 300… and then 500.

It’s helping a bit but I still need to restart from time to time or else my PeerTube gets really slow.

I think it’s an issue of having an increasingly more popular GoToSocial account on the same VPS as PeerTube. Federation issues and whatnot…

@elena @rozie I think there might be something here. Are the requests just coming from localhost?

@elena So, I guess we're talking about DNS, not DHCP. There's a cache-size parameter, default value is 150, but it can be safely increased. IIRC I used 2-3k.

But if you only need DNS server, not DHCP and "for real", I recommend unbound. Very fast, simple configuration.

@elena cron may be what you're looking for?
@elena you may consider using a systemd timer instead of a cron job, since they are easier to write, test and debug

@elena

You were already pointed to cronjobs, what also was my first idea.

And you also might already be aware about this #Wikipedia article:
https://en.wikipedia.org/wiki/Cron

But as you mentioned a VPS... do you use a graphical interface to manage this? If so, does this offer also a GUI for creating and maintaining cronjobs?

cron - Wikipedia

@nick I did it in CLI (I prefer CLI to a GUI). I see it's there (the cron job I just created). The real test will be tonight at 9pm 😅​