Ubuntu – Run a shutdown script

scriptsshutdown

I'm using Ubuntu 16.0.4
I want to run a script at shutdown
I've created a file etc/init.d/rabbitstop as below:

#!/bin/bash
cp -b /home/admin1/hello /home/admin1/pritesh/hi
exit 0

And I ran

sudo chmod +x /etc/rc6.d/rabbitstop

also ran this command:

sudo ln -s /etc/init.d/rabbitstop /etc/rc0.d/K01rabbitstop

then shutdown PC and started again, but I'm not getting the file named hi at this location:

/home/admin1/pritesh/

kindly suggest a solution to me

Best Answer

Make sure this directory exist : /home/admin1/pritesh you are using ubuntu 16.04 with systemd so create a systemd unit file in /etc/systemd/service/myunit.service

[Unit]
Description=my shutdown script
DefaultDependencies=no
Before=shutdown.target reboot.target halt.target

[Service]
Type=oneshot
ExecStart=/path/to/rabbitstop

[Install]
WantedBy=halt.target reboot.target shutdown.target

then run

systemctl daemon-reload
systemctl enable myunit

shutdown , halt or reboot to see the result .

Related Question