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