4

I would like to execute this shell script at reboot and shut down:

#!/bin/sh
touch /test

Its permissions are

-rwxr-xr-x 1 root root 22 Feb 24 09:34 /etc/init.d/te1

And it has this links

/etc/rc0.d/K01te1 -> ../init.d/te1
/etc/rc6.d/K01te1 -> ../init.d/te1

It is working at start up if I have a this link

/etc/rc5.d/S01te1 -> ../init.d/te1

But I need it running at shut down.

How can I do this on Debian 8 and 9 testing?

The suggestion touch /var/lock/subsys/te1 didn't work.

2
  • Why don't you use cron and @reboot ?
    – fugitive
    Commented Feb 24, 2017 at 10:03
  • 1
    Because I need to copy something when the system shuts down. @reboot runs at startup. Commented Feb 24, 2017 at 10:27

3 Answers 3

7

I got the impression that others seem to have problems in getting this running, too. Seems like starting with Debian 8.0 (Jessie) systemd breaks compatibility to System V init.

So here it was suggested to create a systemd service instead. The solution is used here and looks like this:

[Unit]
Description=The te1 script

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/usr/local/bin/te1

[Install]
WantedBy=multi-user.target

The systemd service needs to be saved in /lib/systemd/system/te1.service and installed with sudo systemctl enable te1.

1
  • On Raspbian, this did not work until I ran sudo systemctl daemon-reload. I am not sure at what time the daemon-reload has to be run, whether it is after installing the service config, or after enabling the service a first time, or whether the service has to be enabled after daemon-reload, but I did get it working.
    – Steven Lu
    Commented Aug 6, 2019 at 1:53
0

seems you could find it with a bit of search but:
put your script at /etc/rc6.d grant necessary permissions:

sudo chmod +x K99_script

and some points:
no .sh extension
K_99 is needed
scripts here are executed in alphabetical order
Read here

4
  • Thanks, but it doesn't work. The article also seems to confuse rc0.d with rc6.d. Reboot is rc6.d! Commented Feb 24, 2017 at 10:10
  • it's not reboot it's shut down .
    – FargolK
    Commented Feb 24, 2017 at 19:36
  • Look here: en.wikipedia.org/wiki/Runlevel Commented Feb 24, 2017 at 20:35
  • This doesn't work on Jessie. Commented Apr 15, 2017 at 2:52
-1

Try to execute your script as a startscript in runlevel 6

ln -s /etc/init.d/te1 etc/rc0.d/S01te1
4
  • Thanks, I also tried this before without success. The reason is probably that K10reboot is executed first. Commented Feb 24, 2017 at 10:06
  • I have succesful tested it with Ubuntu 14.04.
    – ingopingo
    Commented Feb 24, 2017 at 12:20
  • 1
    The questioner explicitly said Debian 8 and 9, which are very different in this respect to Ubuntu 14.
    – JdeBP
    Commented Feb 24, 2017 at 12:27
  • This does not work on Jessie Commented Apr 15, 2017 at 2:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .