0

I have a simple script(upload postgresql db dump to google drive) and if I run this script in terminal, everything works fine. Now i want to run this script at system start up

I add my script to folder /etc/init.d, name of the script is gdrive-upload:

#! /bin/sh
### BEGIN INIT INFO
# Provides:          gdrive-upload
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start daemon at boot time
# Description:       Enable service provided by daemon.
### END INIT INFO
sleep 60
while true; do 
    name=$(date -d "today" +"dev_%Y-%m-%d_%H:%M").dump
    PGPASSWORD='passToDB' pg_dump -h localhost -p 5432 -U lm -F c -b -v -f /postgresql/dump/${name} DBname
    gdrive upload /postgresql/dump/${name}
    sudo rm  /postgresql/dump/${name}
    sleep 3600
done

file prmission of this file is : 755

To do this script executable I run sudo chmod a+x /etc/init.d/gdrive-upload After this no output

Then sudo update-rc.d gdrive-upload defaults After this:

perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
    LANGUAGE = (unset),
    LC_ALL = (unset),
    LC_CTYPE = "UTF-8",
    LANG = "en_US.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_US.UTF-8").
insserv: warning: current stop runlevel(s) (1 6) of script `gdrive-upload' overrides LSB defaults (0 1 6).

But script is not running after sudo reboot

But if I run /etc/init.d/gdrive-upload in terminal, everything works fine

0

Since you are on Ubuntu 16.04 which uses systemd you might want to use the systemd native service and timer combination.

Put the actual work into a script. E.g. /usr/local/sbin/gdrive-upload.sh

#!/bin/bash 

name=$(date -d "today" +"dev_%Y-%m-%d_%H:%M").dump
PGPASSWORD='passToDB' pg_dump -h localhost -p 5432 -U lm -F c -b -v -f /postgresql/dump/${name} DBname
gdrive upload /postgresql/dump/${name}
sudo rm  /postgresql/dump/${name}

A corresponding service /etc/systemd/system/gdrive-upload.service file would look like as follows, but might need some tweaks.

[Unit]
Description=Upload stuff to gdrive
After=network-online.target

[Service]
Type=simple
ExecStart=/usr/local/sbin/gdrive-upload.sh

[Install]
WantedBy=multi-user.target

Then you need a systemd timer /etc/systemd/system/gdrive-upload.timer to run the service file on a regular basis.

[Unit]
Description=Regularly upload stuff to gdrive

[Timer]
OnCalendar=*-*-* *:00:00
Persistent=true

[Install]
WantedBy=timers.target

After you have created the files you have to tell systemd that there are new services available to pickup and enable the timer.

$ systemctl daemon-reload
$ systemctl enable gdrive-upload.timer
$ systemctl start gdrive-upload.timer

Do not enable the gdrive-upload.service as this will be triggered by the timer.

Now you can watch your timer when it was triggered last and when it will be triggered next time with systemclt list-timers.
As already mentioned, you may need to tweak the timer and the service a bit, depending on your needs.

  • Thank you for your response, I'm getting this error: Failed to execute operation: Invalid argument, when run systemctl enable gdrive-upload.timer. – user2870934 Mar 13 '18 at 18:15
  • I assume, i badly create gdrive-upload.timer file, because I just run cp -rp /etc/systemd/system/sshd.service /etc/systemd/system/gdrive-upload.timer and paste your code for timer – user2870934 Mar 13 '18 at 18:35
  • Hm...might be. Just tested again and did not have an error. If you still experience problems, could you post the output of journalctl -u gdrive-upload.timer. – Thomas Mar 13 '18 at 20:14
  • output: -- No entries -- – user2870934 Mar 14 '18 at 8:29
  • Hm...any luck with systemctl status gdrive-upload.timer? – Thomas Mar 14 '18 at 11:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.