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.
Asked
Active
Viewed 263 times
-1
-
2Make an init/systemd script. – Ipor Sircer Nov 09 '18 at 13:12
-
Im using ubuntu server 16 – Lakshminarayanan S Nov 09 '18 at 13:13
1 Answers
2
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:
[Unit]
Description=Backup example
RequiresMountsFor=/mnt/backup /home/backup
[Service]
ExecStop=/etc/systemd/system/backup.sh
Type=oneshot
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target

filbranden
- 21,751
- 4
- 63
- 86

AsenM
- 568
-
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 09 '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 09 '18 at 15:03
-
@FilipeBrandenburger Sorry for misspelling your name. Does my answer satisfy you or i need to elaborate more? – AsenM Nov 09 '18 at 15:09
-
I'm not convinced that a
system-shutdown
script is really useful in this case, since I believe it runs really late at shutdown. See for example this question on how at that time the root filesystem is read-only, most others are probably unmounted and the network is most likely down (as you said, there are no guarantees it will be up, which should be enough to make this a deal breaker.) – filbranden Nov 09 '18 at 17:12 -
1Your 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 addAfter=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 09 '18 at 17:15 -
1Also, 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 asExecStart=/bin/true
. You should probably mention that as part of your answer, since it might be relevant. – filbranden Nov 09 '18 at 17:18