If executed manually with sudo, the script does its job.
#!/bin/bash
echo "Script executed at $(date)" >> /var/log/realtimesync.log
echo
/opt/FreeFileSync/RealTimeSync /mnt/harddrive/SWbackup/RealTimeSync/BatchRun.ffs_batch &
echo "The return value is $?" >> /var/log/realtimesync.log
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
echo "Script is being executed." >>/var/log/realtimesync.log
else
echo "Script is being sourced."
fi
echo
echo
echo
echo
echo
echo
In order to execute the script as a root at startup I used systemd, configuring a systemd service unit.I created a systemd Service File:
sudo nano /etc/systemd/system/realtimesync.service
[Unit]
Description=realtimesync is executed and the DATA folder are synchronised between the hard drive and the external hard drive
After=network.target
[Service]
ExecStart=/mnt/harddrive/SWbackup/scripts/realtimesync.sh
User=root
Type=oneshot
RemainAfterExit=true
[Install]
WantedBy=multi-user.target
Then I reoladed and enabled the service
sudo systemctl daemon-reload
sudo systemctl enable realtimesync.service
Finally I started the service
sudo systemctl start realtimesync.service
After rebooting the script looks like to be executed but the software does not keep running. Somehow is like the process has been killed at some point. Any suggestions and how to keep it running?
EDIT: According to the messages I modified my script with the software execution not being run in background:
#!/bin/bash
echo "Script executed at $(date)" >> /var/log/realtimesync.log
echo
/opt/FreeFileSync/RealTimeSync /mnt/harddrive/SWbackup/RealTimeSync/BatchRun.ffs_batch
I also updated the Service File to change to Type=simple:
[Unit]
Description=realtimesync is executed and the DATA folder are synchronised between the hard drive and the external hard drive
After=network.target
[Service]
ExecStart=/mnt/harddrive/SWbackup/scripts/realtimesync.sh
User=root
Type=simple
[Install]
WantedBy=multi-user.target
However the software RealTimeSync is not running after reboot.
EDIT2: I updated my /etc/systemd/system/realtimesync.service
[Unit]
Description=realtimesync is executed and the DATA folder are synchronised between the hard drive and the external hard drive
After=network.target
[Service]
ExecStart=/opt/FreeFileSync/RealTimeSync /mnt/harddrive/SWbackup/RealTimeSync/BatchRun.ffs_batch
User=root
Type=simple
[Install]
WantedBy=multi-user.target
It is still nor running, but now there are logs:
× realtimesync.service - realtimesync is executed and the DATA folder are synchronised between the hard drive and the external hard drive
Loaded: loaded (/etc/systemd/system/realtimesync.service; enabled; preset: enabled)
Active: failed (Result: exit-code) since Tue 2025-08-19 20:35:31 CEST; 53min ago
Duration: 36ms
Invocation: 6e10d64335bd4e68ae166bde84483dc2
Process: 3879 ExecStart=/opt/FreeFileSync/RealTimeSync /mnt/harddrive/SWbackup/RealTimeSync/BatchRun.ffs_batch (code=exited, status=255/EXCEPTION)
Main PID: 3879 (code=exited, status=255/EXCEPTION)
Mem peak: 1.9M
CPU: 15ms
Aug 19 20:35:31 debian systemd[1]: Started realtimesync.service - realtimesync is executed and the DATA folder are synchronised between the hard drive and the external hard drive.
Aug 19 20:35:31 debian RealTimeSync[3879]: Authorization required, but no authorization protocol specified
Aug 19 20:35:31 debian RealTimeSync[3879]: 20:35:31: Error: Unable to initialize GTK+, is DISPLAY set properly?
Aug 19 20:35:31 debian systemd[1]: realtimesync.service: Main process exited, code=exited, status=255/EXCEPTION
Aug 19 20:35:31 debian systemd[1]: realtimesync.service: Failed with result 'exit-code'.
There are here info about this error
Unable to initialize GTK+, is DISPLAY set properly?
Overall, I cannot run the software as a root. The problem is that the software only works if executed as a root probably because it uses data that belongs to root. It was not straightforward to change the ownership of this data. That is why I opened this question.
oneshotreally the type you need?/opt/FreeFileSync/RealTimeSync /mnt/harddrive/SWbackup/RealTimeSync/BatchRun.ffs_batchdirectly as aType=simpleservice. The rest of the script doesn't do anything useful for systemd services.&, you can't get its exit status with$?since the script doesn't wait for it to exit.ExecStart=/mnt/harddrive/SWbackup/scripts/realtimesync.shI would have just usedExecStart=/opt/FreeFileSync/RealTimeSync /mnt/harddrive/SWbackup/RealTimeSync/BatchRun.ffs_batch. That way, you get all the output of that program in yourjournalctllog, and can figure out what goes wrong. Your whole manual logging in that script is pretty counter-productive! Your system brings much better logging tools :)