When you need to ensure that no new task starts if the previous one is still running, and logs all actions to the journal, cron is not suited.
I have a SSD and a HDD. Since the HDD is slow, and copying files to it takes a long time, I want to create a scheduled job that will copy all the new files from my SSD directory “/home/qbittorrentvpn/bufferzone” to “/media/NewAdditions”. This should be run every night at 2am, and if its still running, another tasks should not start. It should log all to journal as well. Instead of CRON, Idecided to use a periodic service.
This will be easier in ensuring that no new task starts if the previous one is still running. I also wanted to lpay with the services Wants requirements criteria.
1. Create an rsync
script
First, create a bash script that will handle the file copy process and log to the systems journal using logger.
#!/bin/bash
# Script to copy new files from SSD to HDD using rsync and log actions to journal
SRC_DIR="/home/qbittorrentvpn/bufferzone"
DEST_DIR="/media/NewAdditions"
# Log to journal
logger "Starting rsync job: Copying files from $SRC_DIR to $DEST_DIR"
# Use rsync to copy new files and preserve attributes (-a), show progress (-v), and skip files that are already up-to-date
rsync -av --ignore-existing "$SRC_DIR/" "$DEST_DIR"
if [ $? -eq 0 ]; then
logger "rsync job completed successfully"
else
logger "rsync job encountered errors"
fi
- Save this script, for example, as
/home/qbittorrentvpn/scripts/rsync_job.sh
, and make it executable:
chmod +x /home/qbittorrentvpn/scripts/rsync_job.sh
2. Create a systemd service and timer
You can use systemd
to handle the scheduling and ensure that no new instance starts if the previous one is still running.
Step 2.1: Create the systemd service
Create a service file /etc/systemd/system/rsync_job.service
with the following content:
[Unit]
Description=Rsync job to copy new files from SSD to HDD
Wants=rsync_job.timer
After=network.target
[Service]
Type=oneshot
ExecStart=/home/qbittorrentvpn/scripts/rsync_job.sh
ExecStartPre=/usr/bin/logger "Starting scheduled rsync job"
ExecStartPost=/usr/bin/logger "Finished scheduled rsync job"
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
This service runs the rsync script, logs output, and ensures everything is tracked by the systems journal.
Step 2.2: Create the systemd timer
Now create a timer file /etc/systemd/system/rsync_job.timer
to run the job at 2 AM every day:
[Unit]
Description=Timer for rsync job to copy files from SSD to HDD
[Timer]
OnCalendar=*-*-* 02:00:00
Persistent=true
[Install]
WantedBy=timers.target
OnCalendar=*-*-* 02:00:00
schedules the task to run daily at 2 AM.Persistent=true
ensures that if the system was off at the scheduled time, it runs the job when the system is back up.
3. Enable and start the timer
Enable and start the systemd timer and service:
sudo systemctl enable rsync_job.timer
sudo systemctl start rsync_job.timer
4. Check the status and logs
You can check the status of the timer and service with:
sudo systemctl status rsync_job.timer
sudo systemctl status rsync_job.service
All logs related to the job will be available in the system journal. You can view them using:
journalctl -u rsync_job.service
This setup ensures that:
- The job runs every night at 2 AM.
- It wont start another instance if the previous one is still running.
- All actions are logged to the system journal.