O
O
oo22maxi2016-01-31 10:03:58
linux
oo22maxi, 2016-01-31 10:03:58

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

I add the file finish-yandex.shto the folder /etc/init.d/and make it executable.
sudo chmod +x /etc/init.d/finish-yandex.sh
Next, I execute the command
sudo update-rc.d finish-yandex.sh stop 01 0 1 6 .
. A message appears:
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
After I reboot and there is a bubble, test.logthere is no folder in the folder ...
I tried to manually make symlinks in /etc/rc0.d/ /etc/rc1.d/ /etc/rc6.d/with the name K00finish-yandex.shor S00finish-yandex.shand it does not work.
What's wrong? Why does not it work?
Thank you.
UPD: Everything written above is most likely suitable for Ubuntu 14.10 and below (did not check)
Extras
From here
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

2 answer(s)
O
oo22maxi, 2016-01-31
@oo22maxi

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

A little about the file
Unit - общая информация.
Description - описание сервиса.
After - задаёт порядок загрузки. В моём случае после старта сессии пользователя.

Service - содержит информацию о службе
ExecStop - Выполняет команду при остановке сервиса.
Type - Типы службы. oneshot полезен для скриптов, которые выполняют одно задание и завершаются

Install - когда юнит должен быть активирован

finish-yandex.sh for the test contains:
#!/bin/sh
echo "it works test" >> /home/user/.yandex_ram/test.log

Make the finish-yandex file executable:
Copy the file to /etc/systemd/system with *.service extension:
Reload systemd to search for new or changed units:
Enable the unit to autostart on system boot:
Reboot and check. Everything worked for me, the test.log file appeared after the reboot.
To check for a start without a reboot, you can start and stop the unit:
sudo systemctl start finish-yandex.service

sudo systemctl stop finish-yandex.service

To disable a unit from autorun on system boot:
Useful link about systemd - click .

V
Vadim Yakovlev, 2016-01-31
@1nn0

Try to use full path to echo
For example:
/bin/echo

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question