I want to copy log files (project log files) when system goes down or shutdown in Ubuntu server. I have using aws Autoscaling for one server. so my case when cpu load is less than 50% then scale down one instance. I want to take this instance log files before going down.


Create a script and put it in /usr/lib/systemd/system-shutdown/

Immediately before executing the actual system halt/poweroff/reboot/kexec systemd-shutdown will run all executables in /usr/lib/systemd/system-shutdown/ and pass one arguments to them: either "halt", "poweroff", "reboot" or "kexec", depending on the chosen action. All executables in this directory are executed in parallel, and execution of the action is not continued before all executables finished.

Other way to achieve same thing is to create unit file and use ExecStop.

Edit: Because Filipe wanted example here is one:

Description=Backup example
RequiresMountsFor=/mnt/backup /home/backup


| improve this answer | |
  • Does a system-shutdown script run while network is still up? If the intention is to copy logs off the machine, that would be important... Your mention of a unit with ExecStop= only is a good way to achieve that, unfortunately your answer doesn't explain how exactly to set that up... Wanna amend it to expand on that? – filbranden Nov 9 '18 at 14:51
  • @FilipeBrandenburger About internet connection. This depends a lot on Os you are using and how it's configured. Some distros stop the internet connection some don't. – AsenM Nov 9 '18 at 15:03
  • @FilipeBrandenburger Sorry for misspelling your name. Does my answer satisfy you or i need to elaborate more? – AsenM Nov 9 '18 at 15:09
  • 1
    Your other example is a step in the right direction, but I think it has some issues that might need fixing. For example, /etc/systemd/system/backup.sh is not an appropriate place to store user scripts. You want to add After=network.target to ensure network is still up at the time it runs. Description= could be better too... You probably want to give more detailed instructions on where to store this unit, on reloading the daemon and enabling the unit (otherwise it won't run on shutdown), also starting it so it runs in the current shutdown. – filbranden Nov 9 '18 at 17:15
  • 1
    Also, in older versions of systemd (probably v219 which is still around in RHEL/CentOS 7), units without an ExecStart= wouldn't work, so they need as a workaround to specify a dummy one, such as ExecStart=/bin/true. You should probably mention that as part of your answer, since it might be relevant. – filbranden Nov 9 '18 at 17:18

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.