How to run a script at shutdown on Debian 9 or Raspbian 8 (Jessie)

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite
1












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.










share|improve this question























  • Why don't you use cron and @reboot ?
    – fugitive
    Feb 24 '17 at 10:03







  • 1




    Because I need to copy something when the system shuts down. @reboot runs at startup.
    – Frank Breitling
    Feb 24 '17 at 10:27















up vote
3
down vote

favorite
1












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.










share|improve this question























  • Why don't you use cron and @reboot ?
    – fugitive
    Feb 24 '17 at 10:03







  • 1




    Because I need to copy something when the system shuts down. @reboot runs at startup.
    – Frank Breitling
    Feb 24 '17 at 10:27













up vote
3
down vote

favorite
1









up vote
3
down vote

favorite
1






1





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.










share|improve this question















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.







shutdown sysvinit system-v






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 22:40









Rui F Ribeiro

38.2k1475125




38.2k1475125










asked Feb 24 '17 at 9:21









Frank Breitling

337416




337416











  • Why don't you use cron and @reboot ?
    – fugitive
    Feb 24 '17 at 10:03







  • 1




    Because I need to copy something when the system shuts down. @reboot runs at startup.
    – Frank Breitling
    Feb 24 '17 at 10:27

















  • Why don't you use cron and @reboot ?
    – fugitive
    Feb 24 '17 at 10:03







  • 1




    Because I need to copy something when the system shuts down. @reboot runs at startup.
    – Frank Breitling
    Feb 24 '17 at 10:27
















Why don't you use cron and @reboot ?
– fugitive
Feb 24 '17 at 10:03





Why don't you use cron and @reboot ?
– fugitive
Feb 24 '17 at 10:03





1




1




Because I need to copy something when the system shuts down. @reboot runs at startup.
– Frank Breitling
Feb 24 '17 at 10:27





Because I need to copy something when the system shuts down. @reboot runs at startup.
– Frank Breitling
Feb 24 '17 at 10:27











3 Answers
3






active

oldest

votes

















up vote
7
down vote



accepted










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.






share|improve this answer





























    up vote
    0
    down vote













    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






    share|improve this answer




















    • Thanks, but it doesn't work. The article also seems to confuse rc0.d with rc6.d. Reboot is rc6.d!
      – Frank Breitling
      Feb 24 '17 at 10:10











    • it's not reboot it's shut down .
      – F.sb
      Feb 24 '17 at 19:36










    • Look here: en.wikipedia.org/wiki/Runlevel
      – Frank Breitling
      Feb 24 '17 at 20:35










    • This doesn't work on Jessie.
      – baldengineer
      Apr 15 '17 at 2:52

















    up vote
    -1
    down vote













    Try to execute your script as a startscript in runlevel 6



    ln -s /etc/init.d/te1 etc/rc0.d/S01te1





    share|improve this answer




















    • Thanks, I also tried this before without success. The reason is probably that K10reboot is executed first.
      – Frank Breitling
      Feb 24 '17 at 10:06











    • I have succesful tested it with Ubuntu 14.04.
      – ingopingo
      Feb 24 '17 at 12:20






    • 1




      The questioner explicitly said Debian 8 and 9, which are very different in this respect to Ubuntu 14.
      – JdeBP
      Feb 24 '17 at 12:27










    • This does not work on Jessie
      – baldengineer
      Apr 15 '17 at 2:53










    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f347275%2fhow-to-run-a-script-at-shutdown-on-debian-9-or-raspbian-8-jessie%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    7
    down vote



    accepted










    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.






    share|improve this answer


























      up vote
      7
      down vote



      accepted










      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.






      share|improve this answer
























        up vote
        7
        down vote



        accepted







        up vote
        7
        down vote



        accepted






        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.






        share|improve this answer














        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Apr 13 '17 at 12:56









        Community

        1




        1










        answered Feb 26 '17 at 15:29









        Frank Breitling

        337416




        337416






















            up vote
            0
            down vote













            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






            share|improve this answer




















            • Thanks, but it doesn't work. The article also seems to confuse rc0.d with rc6.d. Reboot is rc6.d!
              – Frank Breitling
              Feb 24 '17 at 10:10











            • it's not reboot it's shut down .
              – F.sb
              Feb 24 '17 at 19:36










            • Look here: en.wikipedia.org/wiki/Runlevel
              – Frank Breitling
              Feb 24 '17 at 20:35










            • This doesn't work on Jessie.
              – baldengineer
              Apr 15 '17 at 2:52














            up vote
            0
            down vote













            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






            share|improve this answer




















            • Thanks, but it doesn't work. The article also seems to confuse rc0.d with rc6.d. Reboot is rc6.d!
              – Frank Breitling
              Feb 24 '17 at 10:10











            • it's not reboot it's shut down .
              – F.sb
              Feb 24 '17 at 19:36










            • Look here: en.wikipedia.org/wiki/Runlevel
              – Frank Breitling
              Feb 24 '17 at 20:35










            • This doesn't work on Jessie.
              – baldengineer
              Apr 15 '17 at 2:52












            up vote
            0
            down vote










            up vote
            0
            down vote









            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






            share|improve this answer












            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







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Feb 24 '17 at 9:54









            F.sb

            849416




            849416











            • Thanks, but it doesn't work. The article also seems to confuse rc0.d with rc6.d. Reboot is rc6.d!
              – Frank Breitling
              Feb 24 '17 at 10:10











            • it's not reboot it's shut down .
              – F.sb
              Feb 24 '17 at 19:36










            • Look here: en.wikipedia.org/wiki/Runlevel
              – Frank Breitling
              Feb 24 '17 at 20:35










            • This doesn't work on Jessie.
              – baldengineer
              Apr 15 '17 at 2:52
















            • Thanks, but it doesn't work. The article also seems to confuse rc0.d with rc6.d. Reboot is rc6.d!
              – Frank Breitling
              Feb 24 '17 at 10:10











            • it's not reboot it's shut down .
              – F.sb
              Feb 24 '17 at 19:36










            • Look here: en.wikipedia.org/wiki/Runlevel
              – Frank Breitling
              Feb 24 '17 at 20:35










            • This doesn't work on Jessie.
              – baldengineer
              Apr 15 '17 at 2:52















            Thanks, but it doesn't work. The article also seems to confuse rc0.d with rc6.d. Reboot is rc6.d!
            – Frank Breitling
            Feb 24 '17 at 10:10





            Thanks, but it doesn't work. The article also seems to confuse rc0.d with rc6.d. Reboot is rc6.d!
            – Frank Breitling
            Feb 24 '17 at 10:10













            it's not reboot it's shut down .
            – F.sb
            Feb 24 '17 at 19:36




            it's not reboot it's shut down .
            – F.sb
            Feb 24 '17 at 19:36












            Look here: en.wikipedia.org/wiki/Runlevel
            – Frank Breitling
            Feb 24 '17 at 20:35




            Look here: en.wikipedia.org/wiki/Runlevel
            – Frank Breitling
            Feb 24 '17 at 20:35












            This doesn't work on Jessie.
            – baldengineer
            Apr 15 '17 at 2:52




            This doesn't work on Jessie.
            – baldengineer
            Apr 15 '17 at 2:52










            up vote
            -1
            down vote













            Try to execute your script as a startscript in runlevel 6



            ln -s /etc/init.d/te1 etc/rc0.d/S01te1





            share|improve this answer




















            • Thanks, I also tried this before without success. The reason is probably that K10reboot is executed first.
              – Frank Breitling
              Feb 24 '17 at 10:06











            • I have succesful tested it with Ubuntu 14.04.
              – ingopingo
              Feb 24 '17 at 12:20






            • 1




              The questioner explicitly said Debian 8 and 9, which are very different in this respect to Ubuntu 14.
              – JdeBP
              Feb 24 '17 at 12:27










            • This does not work on Jessie
              – baldengineer
              Apr 15 '17 at 2:53














            up vote
            -1
            down vote













            Try to execute your script as a startscript in runlevel 6



            ln -s /etc/init.d/te1 etc/rc0.d/S01te1





            share|improve this answer




















            • Thanks, I also tried this before without success. The reason is probably that K10reboot is executed first.
              – Frank Breitling
              Feb 24 '17 at 10:06











            • I have succesful tested it with Ubuntu 14.04.
              – ingopingo
              Feb 24 '17 at 12:20






            • 1




              The questioner explicitly said Debian 8 and 9, which are very different in this respect to Ubuntu 14.
              – JdeBP
              Feb 24 '17 at 12:27










            • This does not work on Jessie
              – baldengineer
              Apr 15 '17 at 2:53












            up vote
            -1
            down vote










            up vote
            -1
            down vote









            Try to execute your script as a startscript in runlevel 6



            ln -s /etc/init.d/te1 etc/rc0.d/S01te1





            share|improve this answer












            Try to execute your script as a startscript in runlevel 6



            ln -s /etc/init.d/te1 etc/rc0.d/S01te1






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Feb 24 '17 at 9:54









            ingopingo

            61944




            61944











            • Thanks, I also tried this before without success. The reason is probably that K10reboot is executed first.
              – Frank Breitling
              Feb 24 '17 at 10:06











            • I have succesful tested it with Ubuntu 14.04.
              – ingopingo
              Feb 24 '17 at 12:20






            • 1




              The questioner explicitly said Debian 8 and 9, which are very different in this respect to Ubuntu 14.
              – JdeBP
              Feb 24 '17 at 12:27










            • This does not work on Jessie
              – baldengineer
              Apr 15 '17 at 2:53
















            • Thanks, I also tried this before without success. The reason is probably that K10reboot is executed first.
              – Frank Breitling
              Feb 24 '17 at 10:06











            • I have succesful tested it with Ubuntu 14.04.
              – ingopingo
              Feb 24 '17 at 12:20






            • 1




              The questioner explicitly said Debian 8 and 9, which are very different in this respect to Ubuntu 14.
              – JdeBP
              Feb 24 '17 at 12:27










            • This does not work on Jessie
              – baldengineer
              Apr 15 '17 at 2:53















            Thanks, I also tried this before without success. The reason is probably that K10reboot is executed first.
            – Frank Breitling
            Feb 24 '17 at 10:06





            Thanks, I also tried this before without success. The reason is probably that K10reboot is executed first.
            – Frank Breitling
            Feb 24 '17 at 10:06













            I have succesful tested it with Ubuntu 14.04.
            – ingopingo
            Feb 24 '17 at 12:20




            I have succesful tested it with Ubuntu 14.04.
            – ingopingo
            Feb 24 '17 at 12:20




            1




            1




            The questioner explicitly said Debian 8 and 9, which are very different in this respect to Ubuntu 14.
            – JdeBP
            Feb 24 '17 at 12:27




            The questioner explicitly said Debian 8 and 9, which are very different in this respect to Ubuntu 14.
            – JdeBP
            Feb 24 '17 at 12:27












            This does not work on Jessie
            – baldengineer
            Apr 15 '17 at 2:53




            This does not work on Jessie
            – baldengineer
            Apr 15 '17 at 2:53

















             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f347275%2fhow-to-run-a-script-at-shutdown-on-debian-9-or-raspbian-8-jessie%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown






            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay