Let’s create a systemd service and timer that will automatically re-render all websites multiple times a day at specified times.

Step 1: Create the Systemd Service

This service will execute the render_hugo.sh --all command to regenerate all websites.

  1. Open a terminal and create a new systemd service file:

    sudo nano /etc/systemd/system/hugo-render.service
    
  2. Add the following content:

    [Unit]
    Description=Re-render all Hugo websites
    After=network.target
    
    [Service]
    Type=oneshot
    User=pi  # Change to the appropriate user who owns the websites
    WorkingDirectory=/home/pi  # Change if needed
    ExecStart=/home/pi/render_hugo.sh --all
    StandardOutput=journal
    StandardError=journal
    
  3. Save and exit (CTRL+X, then Y, then Enter).


Step 2: Create the Systemd Timer

This timer will trigger the service multiple times a day at specific times.

  1. Create a new timer file:

    sudo nano /etc/systemd/system/hugo-render.timer
    
  2. Add the following content:

    [Unit]
    Description=Timer to re-render all Hugo websites at specific times
    Requires=hugo-render.service
    
    [Timer]
    OnCalendar=*-*-* 06:00:00
    OnCalendar=*-*-* 12:00:00
    OnCalendar=*-*-* 18:00:00
    OnCalendar=*-*-* 23:00:00
    Persistent=true
    
    [Install]
    WantedBy=timers.target
    

    Explanation:

    • OnCalendar=*-*-* 06:00:00 → Runs at 06:00 AM.
    • OnCalendar=*-*-* 12:00:00 → Runs at 12:00 PM.
    • OnCalendar=*-*-* 18:00:00 → Runs at 06:00 PM.
    • OnCalendar=*-*-* 23:00:00 → Runs at 11:00 PM.
    • You can modify these times as needed.
  3. Save and exit (CTRL+X, then Y, then Enter).


Step 3: Enable and Start the Timer

Run the following commands:

sudo systemctl daemon-reload
sudo systemctl enable hugo-render.timer
sudo systemctl start hugo-render.timer

To check if the timer is active, run:

systemctl list-timers --all

To manually trigger the render, you can run:

sudo systemctl start hugo-render.service

To check logs for the service, use:

journalctl -u hugo-render.service --no-pager --reverse

Modifying the Schedule

If you want to change the times, edit /etc/systemd/system/hugo-render.timer and adjust the OnCalendar values.

After making changes, reload the systemd daemon and restart the timer:

sudo systemctl daemon-reload
sudo systemctl restart hugo-render.timer

Now your Hugo sites will be automatically re-rendered multiple times a day at the specified times! 🚀 Let me know if you need any modifications.