Answer the question
In order to leave comments, you need to log in
Executing a script on shutdown-restart, what's wrong?
Ubuntu 15.10
For some reason, the script is not executed on restart or shutdown. Actually the whole process
For the test, I wrote the following script finish-yandex.sh
:
#!/bin/sh
### BEGIN INIT INFO
# Provides: finish-yandex
# Required-Start:
# Required-Stop:
# Default-Start:
# Default-Stop: 0 1 6
# Short-Description: Save yandex config.
# Description:
### END INIT INFO
echo "it works test" >> /home/user/.yandex_ram/test.log
finish-yandex.sh
to the folder /etc/init.d/
and make it executable. sudo chmod +x /etc/init.d/finish-yandex.sh
sudo update-rc.d finish-yandex.sh stop 01 0 1 6 .
update-rc.d: warning: start and stop actions are no longer supported; falling back to defaults
/etc/rc0.d/ /etc/rc1.d/ /etc/rc6.d/
Symlinks appear in the
folders . K01finish-yandex.sh
test.log
there is no folder in the folder ... /etc/rc0.d/ /etc/rc1.d/ /etc/rc6.d/
with the name K00finish-yandex.sh
or S00finish-yandex.sh
and it does not work. How can I make the script run when the machine is turned off? The /etc/rc6.d directory contains scripts that are executed when the machine is rebooted. The /etc/rc0.d directory contains scripts that are executed when the machine is turned off. I put a symbolic link in the /etc/rc0.d directory, because I need the script to be executed when the computer is turned off. Named the file S00backup. And all business. Everything turns off and backs up as it should.
Answer the question
In order to leave comments, you need to log in
Solution for Ubuntu 15.10 (I think it works on 15.04 and above too):
Don't forget to change user to your own Let's
create a new file, I called it finish-yandex, with the following content:
[Unit]
Description=Save Yandex config
After=systemd-user-sessions.service
[Service]
ExecStop=/bin/bash '/home/user/.yandex_ram/finish-yandex.sh'
Type=oneshot
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target reboot.target poweroff.target
Unit - общая информация.
Description - описание сервиса.
After - задаёт порядок загрузки. В моём случае после старта сессии пользователя.
Service - содержит информацию о службе
ExecStop - Выполняет команду при остановке сервиса.
Type - Типы службы. oneshot полезен для скриптов, которые выполняют одно задание и завершаются
Install - когда юнит должен быть активирован
#!/bin/sh
echo "it works test" >> /home/user/.yandex_ram/test.log
sudo systemctl start finish-yandex.service
sudo systemctl stop finish-yandex.service
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question