CentOS 7- Boot Doesn’t Wait For Startup Script To Complete

Home » CentOS » CentOS 7- Boot Doesn’t Wait For Startup Script To Complete
CentOS 8 Comments

Hi All,I have a general question.  I have an old rc type startup script that I am still using on CentOS 7.  I got it to run by creating the systemd service file… a simple one as shown below, for Myservices.
# used to set up the Myservices onstartup[Unit]Description=Start and stop Myservices[Service]Type=oneshotExecStart=/etc/init.d/Myservices startExecStop=/etc/init.d/Myservices stopRemainAfterExit=yes[Install]WantedBy=multi-user.targetThis works. When the server starts Myservices is initiated.  However the boot keeps going. When I log in Myservices is still running.  it takes about 5 minutes to complete.  This is normal.  What is different is the boot up used to wait for the service to finish and then continue.   Can I use any options in my service file shown above, to make it wait before finishing the boot up?
ThanksKM

8 thoughts on - CentOS 7- Boot Doesn’t Wait For Startup Script To Complete

  • Reformatting so it is readable.

    What you probably want to do is to add something to the [Unit] section to make the completion of the be a requirement for the user login service. Something like:

    Before=systemd-user-sessions.service

    You will most likely also need to add a TimeoutStartSec= to your
    [Service] section to give it a longer time to run before systemd times out the service start.

  • thank you for the feedback.  I tried using Before=systemd-user-sessions.service.  At first glance it seems to work, but then the oracle DB shuts down.  Note that myservices starts oracle and then does a few things for our application. When I remove that entry, it goes right to the login as I originally described, but the oracle DB does not shut down.  maybe I have the wrong combination of timeout options with this other option or something, although the timeouts seemed to work also. Just thought I’d throw it out there.  In either case, thanks for helping. I looked online for an explanation of the options for these files, other that the systemd manual page which is hard to use.  I really couldn’t find one.  Any suggestions?
    Thanks again.KM
    From: Jonathan Billings
    To: CentOS mailing list
    Sent: Tuesday, August 2, 2016 9:28 AM
    Subject: Re: [CentOS] CentOS 7- boot doesn’t wait for startup script to complete

    Reformatting so it is readable.

    What you probably want to do is to add something to the [Unit] section to make the completion of the be a requirement for the user login service.  Something like:

    Before=systemd-user-sessions.service

    You will most likely also need to add a TimeoutStartSec= to your
    [Service] section to give it a longer time to run before systemd times out the service start.


    Jonathan Billings

  • Is your service still a oneshot type? If you are starting daemonized services, systemd is probably terminating them. You should use a ‘forking’ type for services that will keep around processes.

  • Thanks for all the help.  At this point it really only works the original way.  Let the login prompt appear, and have the service continue to start.  So I will talk to the other folks on the project to see if that is acceptable. Thanks againKM

    From: Jonathan Billings
    To: CentOS mailing list
    Sent: Thursday, August 4, 2016 5:10 PM
    Subject: Re: [CentOS] CentOS 7- boot doesn’t wait for startup script to complete

    Is your service still a oneshot type?  If you are starting daemonized services, systemd is probably terminating them. You should use a ‘forking’ type for services that will keep around processes.

  • I am taking a step back.  I have a normal rc script that is installed with our product.  All of the CentOS 7 documentation says the script should work on startup/shutdown.   But it doesn’t.   If I run the script manually it works as it should of course. When I restart the server, nothing happens or starts up.  So my question is should it be working as it was without me changing anything?
    The only way I was able to make it work was to add a service file for it in the systemd directory which is what led to my email in the trail attached below.  Any way to see what it is doing? 
    I’ll try debugging into out application logs again, but haven’t had much luck with that.Thanks in advanceKM

    From: Jonathan Billings
    To: CentOS mailing list
    Sent: Thursday, August 4, 2016 5:10 PM
    Subject: Re: [CentOS] CentOS 7- boot doesn’t wait for startup script to complete

    Is your service still a oneshot type?  If you are starting daemonized services, systemd is probably terminating them. You should use a ‘forking’ type for services that will keep around processes.

  • FYI – I just found the following post, https://www.CentOS.org/forums/viewtopic.php?f=47&t=48140 , which I hadn’t seen before.   It has to do with adding an entry to rc.local.  This has worked for me. 
    I know the correct way would be to set up a systemd service file as I started doing.  I will consider all of this while we are supporting different flavors of CentOS/RedHat. 
    Any feedback on this let me know, and as always…. thanks in advance.KM

    From: KM
    To: CentOS mailing list
    Sent: Tuesday, August 9, 2016 3:07 PM
    Subject: Re: [CentOS] CentOS 7- boot doesn’t wait for startup script to complete

    I am taking a step back.  I have a normal rc script that is installed with our product.  All of the CentOS 7 documentation says the script should work on startup/shutdown.   But it doesn’t.   If I run the script manually it works as it should of course. When I restart the server, nothing happens or starts up.  So my question is should it be working as it was without me changing anything?
    The only way I was able to make it work was to add a service file for it in the systemd directory which is what led to my email in the trail attached below.  Any way to see what it is doing? 
    I’ll try debugging into out application logs again, but haven’t had much luck with that.Thanks in advanceKM

    From: Jonathan Billings
    To: CentOS mailing list
    Sent: Thursday, August 4, 2016 5:10 PM
    Subject: Re: [CentOS] CentOS 7- boot doesn’t wait for startup script to complete

    Is your service still a oneshot type?  If you are starting daemonized services, systemd is probably terminating them. You should use a ‘forking’ type for services that will keep around processes.

  • Hello,Nothing I have found has solved this issue.  the service starts on boot, but the booting process continues.  When I log in my startup script is still running and finishes up.  that is good at least, but I’d still like to know how to make it wait. Any feedback is appreciated. Thanks in advance.KM

    From: KM
    To: CentOS mailing list
    Sent: Tuesday, August 9, 2016 3:38 PM
    Subject: Re: [CentOS] CentOS 7- boot doesn’t wait for startup script to complete

    FYI – I just found the following post, https://www.CentOS.org/forums/viewtopic.php?f=47&t=48140 , which I hadn’t seen before.   It has to do with adding an entry to rc.local.  This has worked for me. 
    I know the correct way would be to set up a systemd service file as I started doing.  I will consider all of this while we are supporting different flavors of CentOS/RedHat. 
    Any feedback on this let me know, and as always…. thanks in advance.KM

    From: KM
    To: CentOS mailing list
    Sent: Tuesday, August 9, 2016 3:07 PM
    Subject: Re: [CentOS] CentOS 7- boot doesn’t wait for startup script to complete

    I am taking a step back.  I have a normal rc script that is installed with our product.  All of the CentOS 7 documentation says the script should work on startup/shutdown.   But it doesn’t.   If I run the script manually it works as it should of course. When I restart the server, nothing happens or starts up.  So my question is should it be working as it was without me changing anything?
    The only way I was able to make it work was to add a service file for it in the systemd directory which is what led to my email in the trail attached below.  Any way to see what it is doing? 
    I’ll try debugging into out application logs again, but haven’t had much luck with that.Thanks in advanceKM

    From: Jonathan Billings
    To: CentOS mailing list
    Sent: Thursday, August 4, 2016 5:10 PM
    Subject: Re: [CentOS] CentOS 7- boot doesn’t wait for startup script to complete

    Is your service still a oneshot type?  If you are starting daemonized services, systemd is probably terminating them. You should use a ‘forking’ type for services that will keep around processes.

  • Your systemd service unit is behaving as it would if you didn’t specify that user logins couldn’t happen until the service had finished.

    I’ve done something like this by using a systemd unit that looks like this: (it’s a trivial sleep, but you get the idea)

    # systemctl cat waitcommand.service
    # /etc/systemd/system/waitcommand.service
    [Unit]
    Description=Wait 120 seconds Before=systemd-user-sessions.service

    [Service]
    Type=oneshot ExecStartPre=/usr/bin/sleep 120
    ExecStart=/usr/bin/logger “Wait finished”
    TimeoutStartSec=125s KillMode=process

    [Install]
    WantedBy=multi-user.target

    If you try to SSH into the host, you’ll get an error like this until the service completes:

    $ SSH my host System is booting up. See pam_nologin(8)
    Authentication failed.
    $

    The reason why I have the ExecStartPre is because the service won’t be considered “started” until the ExecStartPre command finishes and the ExecStart can run. The unit says that it runs before systemd-user-sessions.service, which is what prevents user logins (and shows the pam_nologin prompt) until it starts. I also set a timeout with a bit longer than the expected time to run. Once my service is started, then user sessions can start.

    Kinda hacky but it probably will work for you.

LEAVE A COMMENT