Wait for systemd oneshot service to finish

11,452

Solution 1

Found the answer here: https://unix.stackexchange.com/questions/216045/systemd-configure-unit-file-so-that-login-screen-is-not-shown-until-service-exi

Just set Before to when the terminal is available:

[Unit]
Description=Prepare the system after installation
[email protected] [email protected] [email protected] [email protected] [email protected] [email protected]

[Service]
Type=oneshot
ExecStart=/usr/bin/prepare-system

[Install]
WantedBy=multi-user.target

Solution 2

Assuming you use GDM as your login manager, add a line with Before=gdm.service in the [Unit] section.

If you don't use gdm, find out which service starts xorg and put that in the Before= line.

This will cause the task to complete before the login manager displays.

Share:
11,452

Related videos on Youtube

Christoffer Reijer
Author by

Christoffer Reijer

Working as a security technician with a masters degree in computer science, and two small kids. So life is usually very busy with way too little sleep. But I still try to find some time to code on small hobby projects such as Stoffi Music Player. Really enjoy Ruby on Rails, Python and C#. Oh, and IT-security of course.

Updated on September 18, 2022

Comments

  • Christoffer Reijer
    Christoffer Reijer over 1 year

    I am doing some server provisioning and I need to run a script AFTER installation as it requires the system to be fully functional, include services. So I cannot put it inside the %post section of my kickstart file.

    Instead I have created a systemd service which disables itself as the last thing it does.

    [Unit]
    Description=Prepare the system after installation
    
    [Service]
    Type=oneshot
    ExecStart=/usr/bin/prepare-system
    
    [Install]
    WantedBy=multi-user.target
    

    The problem is that during the first boot, I get to the login prompt and can even login and start using the system, while the "first boot script" is still running.

    This gives the Administrator setting up the system the illusion that the system is finished, when it's really not.

    I would instead want the service to delay the booting, preferably showing a message like "The system is being prepared, please wait..." and wait there until the script is finished.

    • Michael Hampton
      Michael Hampton about 7 years
      The login prompt appears before many things have started on the system, such as networking. Startup is much more highly parallel with systemd than was possible before. If you do this, you may introduce lengthy (and possibly unnecessary) delays in startup of unrelated services. Be careful.