I want to upload file before reboot or shutdown.
1.From my vps to vps
Setting for upload.service
vim /etc/systemd/system/upload.service
[Unit]
Description=upload files into my vps
Before=shutdown.target reboot.target
Requires=network-online.target
After=network.target
[Service]
ExecStart=/bin/true
ExecStop=/bin/bash /home/upload.sh
[Install]
WantedBy=multi-user.target
Script for upload.sh
vim /home/upload.sh
/usr/bin/scp -P 22 -i /home/.ssh/id_rsa /home/wp.bak root@remote_ip:/home
It is time to test it.
systemctl enable upload
reboot
It is verified that wp.bak can uploaded from my vps1 to vps2 at reboot.
2.From pc at home to vps
ssh credential has been established between my pc at home and vps.
Same setting as case1.
journalctl -u upload
Started upload files into my vps.
ssh: connect to host xxxxxxxxxx port 22: Network is unreachable
lost connection
It is no use to write After=network.target
as After=network.target ssh.service
.
Do as nbari say.
sudo vim /etc/systemd/system/upload.service
[Unit]
Description=upload files into my vps
Before=shutdown.target reboot.target
After=network.target network-online.target
Requires=network-online.target network.target
[Service]
Type=oneshot
RemainAfterExit=true
ExecStop=/bin/bash /home/debian9/upload.sh
[Install]
WantedBy=multi-user.target
sudo vim /home/upload.sh
/usr/bin/scp -P 22 -i /home/.ssh/id_rsa /home/wp.bak root@remote_ip:/home
sudo systemctl daemon-reload
sudo systemctl enable upload
Reboot pc.
sudo journalctl -u upload
-- Logs begin at Fri 2018-04-27 10:46:34 HKT, end at Fri 2018-04-27 11:00:23 HKT
Apr 27 10:46:51 hwy systemd[1]: Started upload files into my vps.
It seems that upload service works fine.
issue1: Why
ExecStart=/bin/true
ExecStop=/bin/bash /home/upload.sh
works fine on my vps?
Why
RemainAfterExit=true
ExecStop=/bin/bash /home/upload.sh
can work instead of
ExecStart=/bin/true
ExecStop=/bin/bash /home/upload.sh
in my home pc?
issue2:
wp.bak is a big file more than 3G.
time /usr/bin/scp -P 22 -i /home/.ssh/id_rsa /home/wp.bak root@remote_ip:/home
cost me 3000s (50 minutes,almost one hour) or more.
Get the file size in my pc
ls -al /home/wp.bak
-rw-r--r-- 1 debian9 debian9 3977379840 Apr 22 12:23 /home/wp.bak
Get the uploaded file size in my vps.
ssh root@vps_ip
ls -al /home/wp.bak
-rw-r--r-- 1 root root 63045632 Apr 27 02:46 /home/wp.bak
Why only 1.6% ,small part of it uploaded?
63045632/3977379840=0.0158
My servant--home computer lied to me.
Please give a explanation in detail.
ExecStart=/bin/true
intoRemainAfterExit=true
take effcet ? – Rival