Run a script one time per day

1

I think my question applies to "computer software", if not, please tell me.

So I set up a WebDav mount so i can load my password-database with keepassxc without any problems. Now I want to backup the specified file (whichs path / name stay the same all the time) one time per day, preferably the earliest time it can. I already set up the needed script.

echo "[`date`] Doing an automated Backup of my KeePass-File... ({,[BACKUP DRIVE]Backups/KeePass/Keepass-`date +"%Y-%m-%d"`.kdbx})" >> [LOG FOLDER]/keepass-backup.log
chown [USER : GROUP] [LOG DIRECTORY]/keepass-backup.log
/bin/cp [SOURCE DIRECTORY]/current.kdbx {,[BACKUP DRIVE]/Backups/KeePass/Keepass-`date +"%Y-%m-%d"`.kdbx}

But i wasn't able to find out, how to run a job automatically as i want it. But however, i was able to find out about a folder named "/etc/cron.daily", which seems to run one time per day, which would be okay too, even if it wouldn't be the first time possible. But it doesn't seem to work. I found out that a service named "anacron" would be responsible for these folders. It was preinstalled on my Manjaro x86_64 Gnome, but i didn't do anything to configure it.

I would be happy about a tip or even a solution :)

Regards

user12100401

Posted 2019-09-21T18:50:56.950

Reputation: 27

1

You did everything quite alright until you got stuck using the cron Software Utility. Read how to use cron and use crontab expressions in order to schedule your task.

– None – 2019-09-21T19:09:47.713

Answers

0

You can run:

crontab -e

And you get a text editor, with lots of comments explaining how to set it up.

You can then add lines at the end, with a specification for each column. That way, you can run the script everyday at, say, 08:00.

Alternatively, you can put your script inside /etc/cron.daily. It has to have a shebang (the #!/folder/shell at the beginning) and execute permissions. You can take a look at one of the existing ones.

Eduardo Trápani

Posted 2019-09-21T18:50:56.950

Reputation: 541

But the problem i have is, that the time my PC is powered on isn't the same everyday, so what i want to archive is that it runs the first time possible, but just one time per day. Is this possible? If not, i'll try this solution. – user12100401 – 2019-09-21T19:17:41.307

Sure, just put your script in /etc/cron.daily and install anacron. – Eduardo Trápani – 2019-09-21T19:56:19.073

anacron is installed, my script begins with "#!/bin/sh" and i've put it as keepassbackup.sh in /etc/cron.daily. To test it, i changed the time and rebooted, but i'm pretty sure the script didn't run. Did i do something wrong? – user12100401 – 2019-09-21T20:24:12.683

cron is a daemon, but anacron is not. Anacron starts with the system (also after resume). Did you reboot/resume? You can find the timestamps for the commands in /var/log/anacron. Or do 'anacron -f' to run all jobs now. – Eduardo Trápani – 2019-09-21T20:33:15.873

Yes, I rebooted. Also the file "/var/log/anacron" does not exist in my system, even after running "sudo anacron -f". – user12100401 – 2019-09-21T20:39:27.623

Then check /var/log/syslog, something like 'grep anacron /var/log/syslog' and you should see the info there. – Eduardo Trápani – 2019-09-21T20:49:03.320

"/var/log/syslog" doesn't exist either... I'm using Manjaro, an arch based distro, if that helps – user12100401 – 2019-09-21T20:52:22.997

In Manjaro it seems that anacron is provided by cronie. And the information might be found in /var/spool... The Arch wiki has some more info.

– Eduardo Trápani – 2019-09-21T21:00:33.283

0

I have a similar problem (backup essential files on a server in the cloud to my PC).

An easy solution

I run Kubuntu (KDE desktop) but you can find equivalent utilities in other desktops.

I set up KAlarm (which is normally meant to display things at the user at specified times) to run a script that does the backup every day early in the morning (7am).

It happens that KAlarm executes alarms as soon as it can after a power-up so the backup is one of the first thing that runs when I open the PC in the morning.

A robust solution

A robust solution is to use a cron job. But don't run it only once a day. Run it hourly or even every 10 minutes, and have the script determine if the day's backup is already done or not (after a successful execution, leave a tracker file in /tmp or /var/run, than the next executions can check).

xenoid

Posted 2019-09-21T18:50:56.950

Reputation: 7 552

Got to try those solutions one by one tomorrow, thanks :) – user12100401 – 2019-09-21T22:09:16.240