Moving from cron jobs to using systemd

For the lack of thinking this through (and dealing with Luks volumes all day) What would an efficient entry be to execute a command on the 1st Saturday of the month look like in a crontab file?

This is a bit over my head as I’m not a great scripter and I appear to be getting more advanced when using crons, so it seems.

Thank all!

UPDATE:
Looks like this is going to work:

0 9 1-7 * * [ “$(date ‘+%a’)” = “Sat” ] && some_path/somescript.sh

1 Like

Try to drop a script in /etc/cron.daily which looks like,

[pseudo]
If (date of week == 6 && date of month < 7)
then
do_something
[/pseudo]

2 Likes

Brings back a few memories, been years since I used a cron job.
Is there a reason for not using systemctl?

1 Like

Habit mainly. I have a bunch of maintenance scripts that I have used and developed over the past few years.
I’m always open for new and better ways of doing things.

I like what chroot mentioned but I’m unsure if I have the ability to actually pinpoint a time when I want things to start. As I understand it (at least at one point) using /etc/cron.daily runs things that day but not at specific times.

1 Like

Forgot systemd rules now. :grinning:

1 Like

That’s actually interesting… Can timers actually be set to actually run on certain days at certain times?

Yes they can.

2 Likes

Ah I see that! OnCalendar
Looks very doable and gives me a few more projects for the next few weekends.
Thank you both, chroot and sammiev.

Either way I go, I’m moving away from the mess (admittedly) I created.

1 Like
1 Like

YES!!! off loaded my user crons to systemd.

1 Like

Can you show what you did?

I wouldn’t know the difference between a cron job and a blow job.

And I am ok with it.

2 Likes

It’s likely unlimited, here’s the one I did up a few years back.
It runs 10 min after boot and every 24 hours after that.

# hosts-updater.timer

[Unit]
Description=hosts-updater

[Timer]
OnBootSec=10 min
OnUnitActiveSec=24hr
Persistent=true
Unit=hosts-updater.service

[Install]
WantedBy=timers.target

2 Likes

I need to run for a bit. I’ll document examples and pages that helped me out.
Back soon.

Ok, so I’ll include an example (for updating newsboat) and the 2 sites I used as referance.

newsupdate.service:

[Unit]
Description=Newsboat update service

[Service]
Type=simple
ExecStart=/home/chris/.local/bin/newsup

[Install]
WantedBy=default.target

newsupdate.timer

[Unit]
Description=newsboat update timer

[Timer]
OnBootSec=2m
OnUnitActiveSec=5m
OnCalendar=*-*-* */6:00:00
Unit=newsupdate.service

[Install]
WantedBy=timers.target

I opted for the --user space to run this.
The jist of the timer, is that I have this set to run/update every 6 hours.
You enable/start it as you would any other service.
Check out the following links as it’s pretty much to the point.
The second link focuses on Oncalendar - this is key if you want cron-type functionality.

https://techviewleo.com/configuring-cron-jobs-in-linux-using-systemd-timers/

https://silentlad.com/systemd-timers-oncalendar-(cron)-format-explained

3 Likes

Top job @Chris

1 Like

Thanks @altman !
I’m far from being an expert in this feature but this worked for me. Now I just need to figure out how to send the completed task, an email :wink:

2 Likes

Ah ha! Caveat with using systemd, there isn’t a mailto feature. That’s Ok, I can add this to the script I’m calling:

ExecStart=/bin/bash -lc "echo \"$(whoami) created this message on $(date) to remind you about...\" | mutt -s \"Don't forget...\" you@domain.com"

Well, it needs to be modded a bit but I did run a modded version on the cli and it works perfectly!!
So ideally, it would be ran after a scheduled task completes and of course, I would need to pipe the output of the task (for example, running a backup that captures backed up file) and attach that to the email.
lol. - this gets complex.

I’m changing the topic to something like, using systemd instead of crontab.

2 Likes