Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[Solved] systemd timers script not executed
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
pogwizd
n00b
n00b


Joined: 26 Mar 2006
Posts: 60
Location: Warszawa

PostPosted: Sun Apr 19, 2020 7:19 am    Post subject: [Solved] systemd timers script not executed Reply with quote

Hello everybody,

I wrote a script that automatically backup all my PostgreSQL databases, and now I wanted my system to automatically execute it once a week.
The script is working, as I've tested it manually, my problem is that even though it is set to be executed by systemd, and systemd says it was executed, it is not.

Here is my "systemd list-timer -a" result:
Code:
systemctl list-timers -a
NEXT                         LEFT       LAST                         PASSED     UNIT                         ACTIVATES                     
Sun 2020-04-19 09:15:33 CEST 13min left n/a                          n/a        systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.service
Mon 2020-04-20 00:00:00 CEST 14h left   Mon 2020-04-13 11:35:18 CEST 5 days ago DatabaseBackup.timer         DatabaseBackup.service       
Mon 2020-04-20 00:00:00 CEST 14h left   Mon 2020-04-13 11:35:18 CEST 5 days ago fstrim.timer                 fstrim.service


Here is the result of the folder containing the backups:
Code:
ls /root/psqlDB_Backup/ -l
razem 87040
-rw-r--r-- 1 root root 11890176 04-05 07:01 2020.04.05_akonadi_xxxxxxxxx.backup
-rw-r--r-- 1 root root     4608 04-05 07:01 2020.04.05_akonadi_yyyy.backup
-rw-r--r-- 1 root root 32644608 04-05 07:01 2020.04.05_nextcloud.backup
-rw-r--r-- 1 root root     4608 04-05 07:01 2020.04.05_roundcubemail.backup
-rw-r--r-- 1 root root 11909632 04-10 17:33 2020.04.10_akonadi_xxxxxxxxx.backup
-rw-r--r-- 1 root root     4608 04-10 17:33 2020.04.10_akonadi_yyyy.backup
-rw-r--r-- 1 root root 32647680 04-10 17:33 2020.04.10_nextcloud.backup
-rw-r--r-- 1 root root     4608 04-10 17:33 2020.04.10_roundcubemail.backup


As you can see, the databases were backuped only on 05th, and 10th of April, after I launched the script manually. But there is not backup files on the data systemd tells me it executed the scripts.

For info, those backup files are to be located on
Quote:
/root/psqlDB_Backup
of course those folders are not seperate partition, so they are available as soon as my root is mounted.

I give in bellow the systemd scripts (timer & service)
Code:
/etc/systemd/system # cat DatabaseBackup.timer
[Unit]
Description=Timer to execute the PostgreSQL database backup script once a week

[Timer]
OnCalendar=weekly
Persistent=true

[Install]
WantedBy=multi-user.target


Code:
/etc/systemd/system # cat DatabaseBackup.service
[Unit]
Description=Execute the PostgreSQL database backup script

[Service]
Type=oneshot
ExecStart=/bin/bash -c '/root/scripts/DatabaseBackup'


Does anyone has any suggestion where my mistake might be?
Thanks,

Krzysztof


Last edited by pogwizd on Mon Apr 20, 2020 2:23 pm; edited 1 time in total
Back to top
View user's profile Send private message
mike155
Advocate
Advocate


Joined: 17 Sep 2010
Posts: 4438
Location: Frankfurt, Germany

PostPosted: Sun Apr 19, 2020 11:09 am    Post subject: Reply with quote

Quote:
The script is working, as I've tested it manually, my problem is that even though it is set to be executed by systemd, and systemd says it was executed, it is not.

It could well be that your backup script was executed, but that it didn't work as expected.

Add the line below to the top of your backup script:
Code:
/usr/bin/touch /root/psqlDB_Backup/backup_script.last_run

The timestamp of that file will tell you when your script was executed the last time.

It could be that your backup script didn't work as expected, because the Systemd timer service initializes environment variables like PATH to unexpected values. That's a well-known problem with cron jobs. In order to check that, add the line below to the top of your backup script:
Code:
/usr/bin/env > /root/psqlDB_Backup/backup_script.environment

Let the Systemd timer service run your backup script. Then, check the environment variables written to /root/psqlDB_Backup/backup_script.environment, especially the PATH environment variable.
Back to top
View user's profile Send private message
pogwizd
n00b
n00b


Joined: 26 Mar 2006
Posts: 60
Location: Warszawa

PostPosted: Sun Apr 19, 2020 12:56 pm    Post subject: Reply with quote

Hi Mike,

So I have added the two commands to my script, and executed tell systemd to execute it with:
Code:
systemctl start DatabaseBackup.service

And in that case, I have my backups, which is quite strange, because the script do nothing more than checking the time stamp and the environement. So I guess I would have to tell the timers to execute the script somehow to check it, and not systemd directly. But I'll be franc, I do not know how

For sure, I will know tomorrow, since the script execution by systemd's timer is planned for tomorrow
Back to top
View user's profile Send private message
pogwizd
n00b
n00b


Joined: 26 Mar 2006
Posts: 60
Location: Warszawa

PostPosted: Mon Apr 20, 2020 2:23 pm    Post subject: Reply with quote

OK, I found the source of the problem.
I am trying to make backups of my database, so of course postgresql has to be run before the script is initialised. I modified my "timer" script to have this:
Code:
[Unit]
Description=Timer to execute the PostgreSQL database backup script once a week
Requires=postgresql-12.service

And voila,

Thanks mike, your comment help me figure out that the problem came in fact from somewhere else :wink:
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum