• systemd and using the after.local script in openSUSE 12.3

    I have found out that in openSUSE 12.3, some startup scripts do not operate due to the usage of systemd. systemd really speeds up system startup by running the many startup processes in parallel and avoids the usage of shell scripts altogether. That is is good for speed, but not good if you are in need of running something in openSUSE as root when the system is restarted. In my case I was trying to run some VirtualBox mount commands in a VM running openSUSE 12.3 and could not get them to work properly. I nominally just add these commands to the /etc/init.d/after.local bash script, but in openSUSE 12.3 I found out that by default, after.local is not being run anymore. First off I have a procedure to make /etc/init.d/after.local run as normal, at least it seemed normal AND, I found out that you can instead just use /etc/init.d/boot.local as if it was after.local and it works just as before.

    Now, since I figured out how to make after.local work, lets see how that is done.

    1. We need to create the text file after-local.service (shown below) in the folder /lib/systemd/system as root (Alt-F2):

    Code:
    kdesu kwrite /lib/systemd/system/after-local.service
    This is what you put into this file and save it:

    Code:
    #  This file is part of systemd.
    #
    #  systemd is free software; you can redistribute it and/or modify it
    #  under the terms of the GNU General Public License as published by
    #  the Free Software Foundation; either version 2 of the License, or
    #  (at your option) any later version.
    
    [Unit]
    Description=/etc/init.d/after.local Compatibility
    ConditionFileIsExecutable=/etc/init.d/after.local
    
    [Service]
    Type=oneshot
    ExecStart=/etc/init.d/after.local
    TimeoutSec=0
    StandardOutput=tty
    RemainAfterExit=yes
    SysVStartPriority=99
    
    [Install]
    WantedBy=multi-user.target
    2. Next, we need to add the after-local.service to systemd. Open up a terminal session and run the following command:

    Code:
    sudo systemctl enable /lib/systemd/system/after-local.service
    3. Finally, you need to edit the file /etc/init.d/after.local as root and add in the stuff you want to run as root when you startup openSUSE (Alt-F2):

    Code:
    kdesu kwrite /etc/init.d/after.local
    The commands shown here assume you are using KDE, which is the default in openSUSE. Please let me know if you have any questions about using this procedure. I have a script called sasi that can install the after-local file and enable it all for you automatically. Open up a terminal session (in openSUSE 12.1) and run the following command (You can copy and paste it in if you like):

    Code:
    wget -nc http://paste.opensuse.org/view/download/15931498 -O  ~/bin/make-after-local ; chmod +x ~/bin/make-after-local ;  make-after-local
    You must enter the root user password once for the process to complete. This does not actually put anything into the after.local script, which you must do if you have something you want to run on your system startup as root.

    If the after.local bash script does not exist, it will now be created for you with this new make-after-local bash script. This script has even been tested with Fedora 16, which also uses systemd and it created the after.local bash script and activated it for you, ready to use.

    On openSUSE, as long as the after.local script exists, the make-after-local bash script will not over write it, but indicate the file was not written, which is OK.

    Thank You,

    Blogs: asroot : Bash : Packet Filter : C.F.U. : GPU's : fewrup : F.S.M. : H.I. : LNVHW : MMCHECK : N.S.F. : S.A.K.C.
    S.A.S.I. : S.C.L.U. : S.G.T.B. : S.K.I.M. : S.L.A.V.E. : S.L.R.C. : S.T.A.R.T. : S.U.F.F. : SYSEdit : systemd : YaST Power
    Comments 9 Comments
    1. MarcoMeswara's Avatar
      MarcoMeswara -
      systemd -> broken -> not ready yet ?
    1. jdmcdaniel3's Avatar
      jdmcdaniel3 -
      Quote Originally Posted by MarcoMeswara View Post
      systemd -> broken -> not ready yet ?
      I feel systemd works fine. Some applications have not been modified to work properly with it, but it is here to stay, so you need to learn how it works.

      Thank You,
    1. sebadamus's Avatar
      sebadamus -
      Thanks! it works fine you guide! I also created a before-local.service... because I need to start and gracefully stop my virtualboxes (dont know why cant do it right with the vboxes default init script, it works as for starting, but on shutdown kills my vms)

      I would like to ask, what is the default start and shutdown place to put my commands if I would like to use the default way Opensuse 12.2 uses? (as I read your guide, you said there is a default boot.local and shutdown place?)

      Thanks
    1. jdmcdaniel3's Avatar
      jdmcdaniel3 -
      Quote Originally Posted by sebadamus View Post
      Thanks! it works fine you guide! I also created a before-local.service... because I need to start and gracefully stop my virtualboxes (dont know why cant do it right with the vboxes default init script, it works as for starting, but on shutdown kills my vms)

      I would like to ask, what is the default start and shutdown place to put my commands if I would like to use the default way Opensuse 12.2 uses? (as I read your guide, you said there is a default boot.local and shutdown place?)

      Thanks
      So with systemd, its not just a matter of where to put your scripts, but you must write a service file for the script and activate it in systemd. You were short on description as to what you want to do, but lets see what we can do.

      1. Lets assume we have a bash script, marked executable and found to work beforehand that is to be run at shutdown and it is called: shutmeoff
      2. Copy the bash file to the folder /usr/local/bin as shutmeoff, or /usr/local/bin/shutmeoff
      3. Create a service text file for shutmeoff called shutmeoff.service and place the file in the folder /lib/systemd/system, or /lib/systemd/system/shutmeoff.service
      4. The shutmeoff.service text file should contain (items you modify are in bold):

      Code:
      [Unit]
      Description=Shuts Down What I want
      DefaultDependencies=no
      Before=shutdown.target reboot.target halt.target
      
      [Service]
      ExecStart=/usr/local/bin/shutmeoff
      Type=oneshot
      
      [Install] 
      WantedBy=multi-user.target
      5. Open up terminal and execute the command:

      Code:
      sudo systemctl enable /lib/systemd/system/shutmeoff.service
      6. Restart PC and see if it works as you desire...

      Thank You,
    1. sebadamus's Avatar
      sebadamus -
      Thanks Daniel! your responses are useful to me! could it be possible that the shutmeoff.service needs a section like this? (I dont know what is for, but I will learn from the opensuse guide as soon as possible!)

      Code:
      [Install]
      WantedBy=multi-user.target
      Because I get this error:
      Warning: unit files do not carry install information. No operation executed.

      ...after running: sudo systemctl enable /lib/systemd/system/shutmeoff.service

      Thanks for you help and patience teaching what you know
    1. jdmcdaniel3's Avatar
      jdmcdaniel3 -
      Quote Originally Posted by sebadamus View Post
      Thanks Daniel! your responses are useful to me! could it be possible that the shutmeoff.service needs a section like this? (I dont know what is for, but I will learn from the opensuse guide as soon as possible!)

      Code:
      [Install]
      WantedBy=multi-user.target
      Because I get this error:
      Warning: unit files do not carry install information. No operation executed.

      ...after running: sudo systemctl enable /lib/systemd/system/shutmeoff.service

      Thanks for you help and patience teaching what you know
      Sure, did you try adding it to see what you got? I added to my example, but at the time I had several things going at once. One thing is for sure and that is nothing beats trying it out to see what you get and I really like using a VM like VirtualBox to play around with such things.

      Thank You,
    1. sebadamus's Avatar
      sebadamus -
      Thanks Daniel, yesterday I had to prepare a barbacue (here we call it "asado") so I couldnt try more.

      Now I have! this are the results:

      I created the shutmeoff bash, but called /usr/local/bin/run_at_shutdown.sh (also I make it executable, and tested it, it works)

      I put this inside, following your help:
      Code:
      quahog:~ # cat /usr/local/bin/run_at_shutdown.sh
      ## shutdown commands service
      # this one writes a file just to see if it is being executed
      sudo -u vbox touch /home/vbox/SHUTDOWN_OK
      # here it saves the vm named machine1
      sudo -u vbox VBoxManage controlvm "machine1" savestate
      The I create the file /lib/systemd/system/run_at_shutdown.service with the data you toldme and the [install] section:
      Code:
      quahog:~ # cat /lib/systemd/system/run_at_shutdown.service
      [Unit]
      Description=Run at shutdown
      DefaultDependencies=no
      Before=shutdown.target reboot.target halt.target
      
      
      [Service]
      ExecStart=/usr/local/bin/run_at_shutdown.sh
      Type=oneshot
      
      
      [Install]
      WantedBy=multi-user.target
      Then I enable it just fine:
      Code:
      quahog:~ # systemctl enable /lib/systemd/system/run_at_shutdown.service
      Then I reboot my system (twice just in case and starting the vm each time), so if I am right... a file should be created called /home/vbox/SHUTDOWN_OK but theres no file, and the vbox "savestate" command is not executed.

      What am I missing to make the this commands work on shutdown (next I will do the same but on startup to start the machines )

      Here i test this also, maybe it says you something:
      Code:
      quahog:/usr/local/bin # systemctl status run_at_shutdown.service
      run_at_shutdown.service - Run at shutdown
            Loaded: loaded (/lib/systemd/system/run_at_shutdown.service; enabled)
            Active: failed (Result: exit-code) since Wed, 23 Jan 2013 17:12:52 -0300; 1min 33s ago
           Process: 8292 ExecStart=/usr/local/bin/run_at_shutdown.sh (code=exited, status=203/EXEC)
            CGroup: name=systemd:/system/run_at_shutdown.service
      Thank you!
    1. sebadamus's Avatar
      sebadamus -
      Daniel, here is my post about virtualbox shutdown problem... if you are interested or just curious, but I think I will solve it your way (with systemd) because I havent got too much response on that post (maybe its not an usual virtualbox option)
      https://forums.opensuse.org/english/...ml#post2521470
    1. MarcoMeswara's Avatar
      MarcoMeswara -
      Just wanted to note here that this method works on a 12.2 box as well.
      Had to use this method on a old box and it worked flawlessly