systemd service on startup and on shutdown requiring network access Linux Mint 19.1





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I'm trying to setup a systemd service which executes a script after the system is fully booted (internet access is required) and also executes a different script right before the system shuts down or reboots (internet access is required).



I spend all day yesterday reading dozens of forum posts and guides but can't wrap my head around the logic.
Up until now I got:



[Unit]
Description=Execute custom script on shutdown
After=network.target

[Service]
Type=oneshot
ExecStart=/etc/init.d/hostup
ExecStop=/etc/init.d/hostdown

[Install]
WantedBy=halt.target reboot.target shutdown.target


My thoughts are:
The unit waits on startup for the network.target since it's required to perform the task After=network.target.



On service startup it executes the first script ExecStart=/etc/init.d/hostup.



And finally on shutdown or reboot the second script is executed ExecStop=/etc/init.d/hostdown



Is this even close or do I need to have two seperate services each handling one script?



Also where is the appropriate place to put these scripts. /etc/init.d/ was just an idea.



Any input is appreciated



EDIT:



I invested some more time in it and I now have to separate services:



For startup:



Description=Custom Command on startup
After=network-online.target

[Service]
Type=idle
ExecStart=/etc/init.d/hostup

[Install]
WantedBy=network-online.target


and for shutdown:



[Unit]
Description=Execute custom script on shutdown
Before=shutdown.target network.target

[Service]
ExecStart=/bin/true
ExecStop=/etc/init.d/hostdown
RemainAfterExit=yes

[Install]
WantedBy=halt.target reboot.target shutdown.target


My current problem is defining the runlevels.



when I run sudo systemctl enable hostup.service following error pops up:



Synchronizing state of hostup.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable hostup
update-rc.d: error: hostup Default-Start contains no runlevels, aborting.


Any help on how to define the runlevels correctly would be highly appreciated.










share|improve this question

























  • Multi-posted at unix.stackexchange.com/questions/504115 .

    – JdeBP
    Mar 4 at 16:21


















0















I'm trying to setup a systemd service which executes a script after the system is fully booted (internet access is required) and also executes a different script right before the system shuts down or reboots (internet access is required).



I spend all day yesterday reading dozens of forum posts and guides but can't wrap my head around the logic.
Up until now I got:



[Unit]
Description=Execute custom script on shutdown
After=network.target

[Service]
Type=oneshot
ExecStart=/etc/init.d/hostup
ExecStop=/etc/init.d/hostdown

[Install]
WantedBy=halt.target reboot.target shutdown.target


My thoughts are:
The unit waits on startup for the network.target since it's required to perform the task After=network.target.



On service startup it executes the first script ExecStart=/etc/init.d/hostup.



And finally on shutdown or reboot the second script is executed ExecStop=/etc/init.d/hostdown



Is this even close or do I need to have two seperate services each handling one script?



Also where is the appropriate place to put these scripts. /etc/init.d/ was just an idea.



Any input is appreciated



EDIT:



I invested some more time in it and I now have to separate services:



For startup:



Description=Custom Command on startup
After=network-online.target

[Service]
Type=idle
ExecStart=/etc/init.d/hostup

[Install]
WantedBy=network-online.target


and for shutdown:



[Unit]
Description=Execute custom script on shutdown
Before=shutdown.target network.target

[Service]
ExecStart=/bin/true
ExecStop=/etc/init.d/hostdown
RemainAfterExit=yes

[Install]
WantedBy=halt.target reboot.target shutdown.target


My current problem is defining the runlevels.



when I run sudo systemctl enable hostup.service following error pops up:



Synchronizing state of hostup.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable hostup
update-rc.d: error: hostup Default-Start contains no runlevels, aborting.


Any help on how to define the runlevels correctly would be highly appreciated.










share|improve this question

























  • Multi-posted at unix.stackexchange.com/questions/504115 .

    – JdeBP
    Mar 4 at 16:21














0












0








0








I'm trying to setup a systemd service which executes a script after the system is fully booted (internet access is required) and also executes a different script right before the system shuts down or reboots (internet access is required).



I spend all day yesterday reading dozens of forum posts and guides but can't wrap my head around the logic.
Up until now I got:



[Unit]
Description=Execute custom script on shutdown
After=network.target

[Service]
Type=oneshot
ExecStart=/etc/init.d/hostup
ExecStop=/etc/init.d/hostdown

[Install]
WantedBy=halt.target reboot.target shutdown.target


My thoughts are:
The unit waits on startup for the network.target since it's required to perform the task After=network.target.



On service startup it executes the first script ExecStart=/etc/init.d/hostup.



And finally on shutdown or reboot the second script is executed ExecStop=/etc/init.d/hostdown



Is this even close or do I need to have two seperate services each handling one script?



Also where is the appropriate place to put these scripts. /etc/init.d/ was just an idea.



Any input is appreciated



EDIT:



I invested some more time in it and I now have to separate services:



For startup:



Description=Custom Command on startup
After=network-online.target

[Service]
Type=idle
ExecStart=/etc/init.d/hostup

[Install]
WantedBy=network-online.target


and for shutdown:



[Unit]
Description=Execute custom script on shutdown
Before=shutdown.target network.target

[Service]
ExecStart=/bin/true
ExecStop=/etc/init.d/hostdown
RemainAfterExit=yes

[Install]
WantedBy=halt.target reboot.target shutdown.target


My current problem is defining the runlevels.



when I run sudo systemctl enable hostup.service following error pops up:



Synchronizing state of hostup.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable hostup
update-rc.d: error: hostup Default-Start contains no runlevels, aborting.


Any help on how to define the runlevels correctly would be highly appreciated.










share|improve this question
















I'm trying to setup a systemd service which executes a script after the system is fully booted (internet access is required) and also executes a different script right before the system shuts down or reboots (internet access is required).



I spend all day yesterday reading dozens of forum posts and guides but can't wrap my head around the logic.
Up until now I got:



[Unit]
Description=Execute custom script on shutdown
After=network.target

[Service]
Type=oneshot
ExecStart=/etc/init.d/hostup
ExecStop=/etc/init.d/hostdown

[Install]
WantedBy=halt.target reboot.target shutdown.target


My thoughts are:
The unit waits on startup for the network.target since it's required to perform the task After=network.target.



On service startup it executes the first script ExecStart=/etc/init.d/hostup.



And finally on shutdown or reboot the second script is executed ExecStop=/etc/init.d/hostdown



Is this even close or do I need to have two seperate services each handling one script?



Also where is the appropriate place to put these scripts. /etc/init.d/ was just an idea.



Any input is appreciated



EDIT:



I invested some more time in it and I now have to separate services:



For startup:



Description=Custom Command on startup
After=network-online.target

[Service]
Type=idle
ExecStart=/etc/init.d/hostup

[Install]
WantedBy=network-online.target


and for shutdown:



[Unit]
Description=Execute custom script on shutdown
Before=shutdown.target network.target

[Service]
ExecStart=/bin/true
ExecStop=/etc/init.d/hostdown
RemainAfterExit=yes

[Install]
WantedBy=halt.target reboot.target shutdown.target


My current problem is defining the runlevels.



when I run sudo systemctl enable hostup.service following error pops up:



Synchronizing state of hostup.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable hostup
update-rc.d: error: hostup Default-Start contains no runlevels, aborting.


Any help on how to define the runlevels correctly would be highly appreciated.







linux boot linux-mint shutdown systemd






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 3 at 14:43







Eike

















asked Mar 3 at 11:11









EikeEike

11




11













  • Multi-posted at unix.stackexchange.com/questions/504115 .

    – JdeBP
    Mar 4 at 16:21



















  • Multi-posted at unix.stackexchange.com/questions/504115 .

    – JdeBP
    Mar 4 at 16:21

















Multi-posted at unix.stackexchange.com/questions/504115 .

– JdeBP
Mar 4 at 16:21





Multi-posted at unix.stackexchange.com/questions/504115 .

– JdeBP
Mar 4 at 16:21










1 Answer
1






active

oldest

votes


















0














Found a solution that works for me.



Service running on shutdown:



[Unit]
Description=Execute custom script on shutdown
Before=network-online.target

[Service]
ExecStart=/bin/true
ExecStop=/usr/local/bin/hostdown.sh
RemainAfterExit=yes

[Install]
WantedBy=halt.target reboot.target shutdown.target poweroff.target



Service running on startup:



[Unit]
Description=Custom Command on startup
After=network-online.target
After=network.target
After=systemd-user-sessions.service

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStartPre=/bin/sleep 30
ExecStart=/usr/local/bin/hostup.sh
RestartSec=5

[Install]
WantedBy=multi-user.target


The latter may not be the smartest or prettiest solution and I still don't quite understand why it won't work without this line ExecStartPre=/bin/sleep 30



Just thought I'd document my solution for the future.



Comments and suggestions are very much welcome!






share|improve this answer
























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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%2fsuperuser.com%2fquestions%2f1410888%2fsystemd-service-on-startup-and-on-shutdown-requiring-network-access-linux-mint-1%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Found a solution that works for me.



    Service running on shutdown:



    [Unit]
    Description=Execute custom script on shutdown
    Before=network-online.target

    [Service]
    ExecStart=/bin/true
    ExecStop=/usr/local/bin/hostdown.sh
    RemainAfterExit=yes

    [Install]
    WantedBy=halt.target reboot.target shutdown.target poweroff.target



    Service running on startup:



    [Unit]
    Description=Custom Command on startup
    After=network-online.target
    After=network.target
    After=systemd-user-sessions.service

    [Service]
    Type=oneshot
    RemainAfterExit=yes
    ExecStartPre=/bin/sleep 30
    ExecStart=/usr/local/bin/hostup.sh
    RestartSec=5

    [Install]
    WantedBy=multi-user.target


    The latter may not be the smartest or prettiest solution and I still don't quite understand why it won't work without this line ExecStartPre=/bin/sleep 30



    Just thought I'd document my solution for the future.



    Comments and suggestions are very much welcome!






    share|improve this answer




























      0














      Found a solution that works for me.



      Service running on shutdown:



      [Unit]
      Description=Execute custom script on shutdown
      Before=network-online.target

      [Service]
      ExecStart=/bin/true
      ExecStop=/usr/local/bin/hostdown.sh
      RemainAfterExit=yes

      [Install]
      WantedBy=halt.target reboot.target shutdown.target poweroff.target



      Service running on startup:



      [Unit]
      Description=Custom Command on startup
      After=network-online.target
      After=network.target
      After=systemd-user-sessions.service

      [Service]
      Type=oneshot
      RemainAfterExit=yes
      ExecStartPre=/bin/sleep 30
      ExecStart=/usr/local/bin/hostup.sh
      RestartSec=5

      [Install]
      WantedBy=multi-user.target


      The latter may not be the smartest or prettiest solution and I still don't quite understand why it won't work without this line ExecStartPre=/bin/sleep 30



      Just thought I'd document my solution for the future.



      Comments and suggestions are very much welcome!






      share|improve this answer


























        0












        0








        0







        Found a solution that works for me.



        Service running on shutdown:



        [Unit]
        Description=Execute custom script on shutdown
        Before=network-online.target

        [Service]
        ExecStart=/bin/true
        ExecStop=/usr/local/bin/hostdown.sh
        RemainAfterExit=yes

        [Install]
        WantedBy=halt.target reboot.target shutdown.target poweroff.target



        Service running on startup:



        [Unit]
        Description=Custom Command on startup
        After=network-online.target
        After=network.target
        After=systemd-user-sessions.service

        [Service]
        Type=oneshot
        RemainAfterExit=yes
        ExecStartPre=/bin/sleep 30
        ExecStart=/usr/local/bin/hostup.sh
        RestartSec=5

        [Install]
        WantedBy=multi-user.target


        The latter may not be the smartest or prettiest solution and I still don't quite understand why it won't work without this line ExecStartPre=/bin/sleep 30



        Just thought I'd document my solution for the future.



        Comments and suggestions are very much welcome!






        share|improve this answer













        Found a solution that works for me.



        Service running on shutdown:



        [Unit]
        Description=Execute custom script on shutdown
        Before=network-online.target

        [Service]
        ExecStart=/bin/true
        ExecStop=/usr/local/bin/hostdown.sh
        RemainAfterExit=yes

        [Install]
        WantedBy=halt.target reboot.target shutdown.target poweroff.target



        Service running on startup:



        [Unit]
        Description=Custom Command on startup
        After=network-online.target
        After=network.target
        After=systemd-user-sessions.service

        [Service]
        Type=oneshot
        RemainAfterExit=yes
        ExecStartPre=/bin/sleep 30
        ExecStart=/usr/local/bin/hostup.sh
        RestartSec=5

        [Install]
        WantedBy=multi-user.target


        The latter may not be the smartest or prettiest solution and I still don't quite understand why it won't work without this line ExecStartPre=/bin/sleep 30



        Just thought I'd document my solution for the future.



        Comments and suggestions are very much welcome!







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 3 at 19:09









        EikeEike

        11




        11






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Super User!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1410888%2fsystemd-service-on-startup-and-on-shutdown-requiring-network-access-linux-mint-1%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 do I know what Microsoft account the skydrive app is syncing to?

            When does type information flow backwards in C++?

            Grease: Live!