I want to upload a file to my dropbox when my pc is shutting down.
sudo vim /etc/systemd/system/upload.service
[Unit]
Description=upload files into dropbox
Before=network.target shutdown.target reboot.target
Requires=network-online.target
[Service]
ExecStop=/bin/true
ExecStart=/bin/bash /home/upload.sh
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
And the upload.sh script.
cd /home
curl -X POST https://content.dropboxapi.com/2/files/upload \
--header "Authorization: Bearer xxxx" \
--header "Dropbox-API-Arg: {\"path\":\"/test.txt\",\"mode\":{\".tag\":\"overwrite\"}}" \
--header "Content-Type: application/octet-stream" \
--data-binary @"test.txt"
bash upload.sh
can executed successfully,and test.txt
file uploaded into my dropbox.
sudo systemctl enable upload service
To reboot my pc.
sudo journalctl -u upload
Apr 13 23:58:52 localhost systemd[1]: Started upload files into dropbox.
Apr 13 23:58:52 localhost systemd[1]: Starting upload files into dropbox...
Apr 13 23:58:52 localhost bash[117]: % Total % Received % Xferd Average Speed Time Time Time Current
Apr 13 23:58:52 localhost bash[117]: Dload Upload Total Spent Left Speed
Apr 13 23:58:52 localhost bash[117]: 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0curl: (6) Could not resolve host: content.dropboxapi.com; Un
Apr 13 23:58:52 localhost systemd[1]: upload.service: main process exited, code=exited, status=6/NOTCONFIGURED
Apr 13 23:58:52 localhost systemd[1]: Unit upload.service entered failed state.
Apr 13 23:58:52 localhost systemd[1]: upload.service failed.
Some DNS error Could not resolve host: content.dropboxapi.com
result in upload.service failed.
I have added Requires=network-online.target
in upload.service,how to make DNS parser resolve the host at my pc's shutdowning time?
method 1: /etc/systemd/system/upload.service
1.1 /etc/systemd/system/upload.service
1.2 vim /home/upload.sh
1.3
sudo systemctl enable upload.service
method 2: /lib/systemd/system-shutdown/upload.service
2.1 /lib/systemd/system-shutdown/upload.service
2.2 vim /home/upload.sh
2.3
sudo systemctl enable /lib/systemd/system-shutdown/upload.service
Execstop and Execstart reversed
According to this Unix & Linux answer: How to run a script with systemd right before shutdown, your Execstop and Execstart are reversed.
The lines:
Should read:
The answer goes on to mention to restart services. So after creating the file, make sure to
systemctl daemon-reload
andsystemctl enable yourservice --now
Alternate method not working
The
before=
,requires=
, etc. can be confusing at times. You can put your script in/usr/lib/systemd/system-shutdown/
directory instead. It will be run immediately upon shutdown. See: https://superuser.com/questions/1016827/how-do-i-run-a-script-before-everything-else-on-shutdown-with-systemdThe
/usr
prefix may be different on some systems, ie just start directory name with/lib
.The best way to run a program or script while the system is going down is by using the
ExecStop=
of the unit to run such program. TheExecStart=
can either be a dummy script (such as/bin/true
) or, in recent versions of systemd, it can even be omitted.Regarding dependencies, you want to place your unit after the services you need, such as
network-online.target
and perhapssystemd-resolved.service
if you're using it for DNS resolution. The reason for that is that systems are stopped at shutdown time in the reverse order in which they are started. So by ordering your service after the dependencies, it means yourExecStop=
script will be run before the dependencies are stopped.Something like this should work:
Enable the unit as usual (the
--now
argument also starts it):And it will perform the upload when stopped, which you can test with:
This was recently discussed in the systemd-devel mailing list.