I am using Ubuntu 16.04. I have a directory named Work which I want to backup to github everytime I poweroff my computer. I have written the backup script and it's working fine but I cannot run it before shutting down. Please help. Here are the contents of backup_work.sh

cd /home/kaustab/Work
git add .
git commit -m "Daily Backup"
mkdir /home/kaustab/test 
git push origin master
echo "Backed up"
read -n 1 -s -r -p "Press any key to continue"

You can run a script at shutdown by following the instructions here.

Basically, you can place the script in /etc/rc6.d/, and make it executable. The one drawback with this approach is that any script in the above directory will run as the root user, which can change file permissions and cause issues later. The alternative is to leave your script in your home directory, and add the following script in /etc/rc6.d


sudo -u kaustab /home/kaustab/backup_work.sh
exit 0

Finally, you should remove the call to read as well (or add a timeout), since it will ask you for a keypress before shutting down and block the shut down.

Systemd approach

The rc6.d script approach above is the legacy sysv-init approach to running scripts at shutdown. With newer versions of Debian/Ubuntu, it's safer to use a systemd unit file to run the script at shutdown.

Place the following script in /etc/systemd/system/backup-work.service (source) and run sudo systemctl daemon-reload

Description=Backup work directory
Before=shutdown.target reboot.target halt.target
# This works because it is installed in the target and will be
#   executed before the target state is entered
# Also consider kexec.target

ExecStart=/home/kaustab/backup_work.sh  # your path and filename

WantedBy=halt.target reboot.target shutdown.target
  • I tried the systemd approach but it is not working. My script has execute permissions. Also when the script will run I will be asked for my username and password. – KaustabPal Sep 3 at 18:15
  • What I want is to first run my script and if the script runs successfully only then the system will shutdown. Is ther any way to do this? – KaustabPal Sep 3 at 18:16
  • 1
    If you are using HTTPS to connect to Github, then yes, it will ask for a password. You are better off using passwordless SSH to connect to Github. A systemd spawned service will not have a terminal access, so you will not see a request for username/password. – QIS Sep 3 at 20:47
  • For blocking the shutdown, you can consider using a service like reboot-guard – QIS Sep 3 at 20:48
  • Isn't there a need to enable the script after daemon-reload? – Jan Dorniak Sep 6 at 8:36

Thank you all for all your help but I have managed to solve this. What I did was edit the shutdown.desktop in /usr/share/applications and changed the exec to my script. This is what my modified shutdown.desktop file looks like.

[Desktop Entry]
Comment=Backup and power off the computer
GenericName=Shut Down
Exec=gnome-terminal -e /home/kaustab/.scripts/backup_work.sh

At the end of the backup_work.sh script I have added the line gnome-session-quit --power-off to give me the options of the power menu. Thanks to QIS for pointing out to use ssh instead of https to connect to github. I will be trying that later.

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.