User d-bus service post-start/pre-stop

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












1















I need to run a script AFTER systemd has started the user dbus.service and run another script BEFORE systemd attempts to stop dbus.service. I don't want to modify the existing service files. The script is supposed to save and restore the loaded CDEmu images across reboots. The global service that called cdemu with --bus system has stopped working a few years ago, because in modern distros each user has their own set of loaded virtual images.



My current solution is having two user services: the -pre.service WantedBy=dbus.service and the -post.service that Requires=dbus.service



The latter is auto-stopped along with dbus.service. The first service must be enabled manually for each user who wants it. It starts the second service by launching systemctl start in background to avoid deadlock. Also, the first service stops immediately after start to properly handle dbus restart.



I want to know whether it's possible to reduce the number of services to just one, possibly with StopWhenUnneeded.



For testing I exit the X session and do:



$ systemctl --user stop dbus
$ systemctl --user start dbus


_



# /etc/systemd/user/dbus-pre.service
[Service]
ExecStart=/usr/local/bin/dbus-hook pre-start
# will be started again when dbus restarts
RemainAfterExit=false

[Install]
# systemctl --user enable dbus-pre
WantedBy=dbus.service


_



# /etc/systemd/user/dbus-post.service
[Service]
ExecStart=/usr/local/bin/dbus-hook post-start
ExecStop=/usr/local/bin/dbus-hook pre-stop
# oneshot supposed to spare dbus until ExecStop finishes
Type=oneshot
# RemainAfterExit=true prevents immediate stop of our service
RemainAfterExit=true

[Unit]
# Requires= supposed to stop us when dbus stops
Requires=dbus.service
# After= supposed to spare dbus until our service stops
After=dbus.service


_



#!/bin/bash
# /usr/local/bin/dbus-hook
echo "$1"
case $1 in
pre-start)
echo "starting dbus-post.service..."
/usr/bin/systemctl --user start dbus-post.service >/dev/null 2>&1
;;
post-start)
echo "restoring cdemu mounts..."
if [ -f ~/.config/cdemu.save ]; then

read -r x
read -r x
while read -r n b f; do
if [ x"True" = x"$b" ]; then
/usr/bin/cdemu load "$n" "$f"
fi
done
<~/.config/cdemu.save
fi
;;
pre-stop)
echo "saving cdemu mounts..."
#echo "DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS"
if /usr/bin/pgrep -u "$USER:?" -x cdemu-daemon >/dev/null; then
mkdir -p ~/.config
/usr/bin/cdemu status >~/.config/cdemu.save
fi
;;
esac









share|improve this question




























    1















    I need to run a script AFTER systemd has started the user dbus.service and run another script BEFORE systemd attempts to stop dbus.service. I don't want to modify the existing service files. The script is supposed to save and restore the loaded CDEmu images across reboots. The global service that called cdemu with --bus system has stopped working a few years ago, because in modern distros each user has their own set of loaded virtual images.



    My current solution is having two user services: the -pre.service WantedBy=dbus.service and the -post.service that Requires=dbus.service



    The latter is auto-stopped along with dbus.service. The first service must be enabled manually for each user who wants it. It starts the second service by launching systemctl start in background to avoid deadlock. Also, the first service stops immediately after start to properly handle dbus restart.



    I want to know whether it's possible to reduce the number of services to just one, possibly with StopWhenUnneeded.



    For testing I exit the X session and do:



    $ systemctl --user stop dbus
    $ systemctl --user start dbus


    _



    # /etc/systemd/user/dbus-pre.service
    [Service]
    ExecStart=/usr/local/bin/dbus-hook pre-start
    # will be started again when dbus restarts
    RemainAfterExit=false

    [Install]
    # systemctl --user enable dbus-pre
    WantedBy=dbus.service


    _



    # /etc/systemd/user/dbus-post.service
    [Service]
    ExecStart=/usr/local/bin/dbus-hook post-start
    ExecStop=/usr/local/bin/dbus-hook pre-stop
    # oneshot supposed to spare dbus until ExecStop finishes
    Type=oneshot
    # RemainAfterExit=true prevents immediate stop of our service
    RemainAfterExit=true

    [Unit]
    # Requires= supposed to stop us when dbus stops
    Requires=dbus.service
    # After= supposed to spare dbus until our service stops
    After=dbus.service


    _



    #!/bin/bash
    # /usr/local/bin/dbus-hook
    echo "$1"
    case $1 in
    pre-start)
    echo "starting dbus-post.service..."
    /usr/bin/systemctl --user start dbus-post.service >/dev/null 2>&1
    ;;
    post-start)
    echo "restoring cdemu mounts..."
    if [ -f ~/.config/cdemu.save ]; then

    read -r x
    read -r x
    while read -r n b f; do
    if [ x"True" = x"$b" ]; then
    /usr/bin/cdemu load "$n" "$f"
    fi
    done
    <~/.config/cdemu.save
    fi
    ;;
    pre-stop)
    echo "saving cdemu mounts..."
    #echo "DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS"
    if /usr/bin/pgrep -u "$USER:?" -x cdemu-daemon >/dev/null; then
    mkdir -p ~/.config
    /usr/bin/cdemu status >~/.config/cdemu.save
    fi
    ;;
    esac









    share|improve this question


























      1












      1








      1








      I need to run a script AFTER systemd has started the user dbus.service and run another script BEFORE systemd attempts to stop dbus.service. I don't want to modify the existing service files. The script is supposed to save and restore the loaded CDEmu images across reboots. The global service that called cdemu with --bus system has stopped working a few years ago, because in modern distros each user has their own set of loaded virtual images.



      My current solution is having two user services: the -pre.service WantedBy=dbus.service and the -post.service that Requires=dbus.service



      The latter is auto-stopped along with dbus.service. The first service must be enabled manually for each user who wants it. It starts the second service by launching systemctl start in background to avoid deadlock. Also, the first service stops immediately after start to properly handle dbus restart.



      I want to know whether it's possible to reduce the number of services to just one, possibly with StopWhenUnneeded.



      For testing I exit the X session and do:



      $ systemctl --user stop dbus
      $ systemctl --user start dbus


      _



      # /etc/systemd/user/dbus-pre.service
      [Service]
      ExecStart=/usr/local/bin/dbus-hook pre-start
      # will be started again when dbus restarts
      RemainAfterExit=false

      [Install]
      # systemctl --user enable dbus-pre
      WantedBy=dbus.service


      _



      # /etc/systemd/user/dbus-post.service
      [Service]
      ExecStart=/usr/local/bin/dbus-hook post-start
      ExecStop=/usr/local/bin/dbus-hook pre-stop
      # oneshot supposed to spare dbus until ExecStop finishes
      Type=oneshot
      # RemainAfterExit=true prevents immediate stop of our service
      RemainAfterExit=true

      [Unit]
      # Requires= supposed to stop us when dbus stops
      Requires=dbus.service
      # After= supposed to spare dbus until our service stops
      After=dbus.service


      _



      #!/bin/bash
      # /usr/local/bin/dbus-hook
      echo "$1"
      case $1 in
      pre-start)
      echo "starting dbus-post.service..."
      /usr/bin/systemctl --user start dbus-post.service >/dev/null 2>&1
      ;;
      post-start)
      echo "restoring cdemu mounts..."
      if [ -f ~/.config/cdemu.save ]; then

      read -r x
      read -r x
      while read -r n b f; do
      if [ x"True" = x"$b" ]; then
      /usr/bin/cdemu load "$n" "$f"
      fi
      done
      <~/.config/cdemu.save
      fi
      ;;
      pre-stop)
      echo "saving cdemu mounts..."
      #echo "DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS"
      if /usr/bin/pgrep -u "$USER:?" -x cdemu-daemon >/dev/null; then
      mkdir -p ~/.config
      /usr/bin/cdemu status >~/.config/cdemu.save
      fi
      ;;
      esac









      share|improve this question
















      I need to run a script AFTER systemd has started the user dbus.service and run another script BEFORE systemd attempts to stop dbus.service. I don't want to modify the existing service files. The script is supposed to save and restore the loaded CDEmu images across reboots. The global service that called cdemu with --bus system has stopped working a few years ago, because in modern distros each user has their own set of loaded virtual images.



      My current solution is having two user services: the -pre.service WantedBy=dbus.service and the -post.service that Requires=dbus.service



      The latter is auto-stopped along with dbus.service. The first service must be enabled manually for each user who wants it. It starts the second service by launching systemctl start in background to avoid deadlock. Also, the first service stops immediately after start to properly handle dbus restart.



      I want to know whether it's possible to reduce the number of services to just one, possibly with StopWhenUnneeded.



      For testing I exit the X session and do:



      $ systemctl --user stop dbus
      $ systemctl --user start dbus


      _



      # /etc/systemd/user/dbus-pre.service
      [Service]
      ExecStart=/usr/local/bin/dbus-hook pre-start
      # will be started again when dbus restarts
      RemainAfterExit=false

      [Install]
      # systemctl --user enable dbus-pre
      WantedBy=dbus.service


      _



      # /etc/systemd/user/dbus-post.service
      [Service]
      ExecStart=/usr/local/bin/dbus-hook post-start
      ExecStop=/usr/local/bin/dbus-hook pre-stop
      # oneshot supposed to spare dbus until ExecStop finishes
      Type=oneshot
      # RemainAfterExit=true prevents immediate stop of our service
      RemainAfterExit=true

      [Unit]
      # Requires= supposed to stop us when dbus stops
      Requires=dbus.service
      # After= supposed to spare dbus until our service stops
      After=dbus.service


      _



      #!/bin/bash
      # /usr/local/bin/dbus-hook
      echo "$1"
      case $1 in
      pre-start)
      echo "starting dbus-post.service..."
      /usr/bin/systemctl --user start dbus-post.service >/dev/null 2>&1
      ;;
      post-start)
      echo "restoring cdemu mounts..."
      if [ -f ~/.config/cdemu.save ]; then

      read -r x
      read -r x
      while read -r n b f; do
      if [ x"True" = x"$b" ]; then
      /usr/bin/cdemu load "$n" "$f"
      fi
      done
      <~/.config/cdemu.save
      fi
      ;;
      pre-stop)
      echo "saving cdemu mounts..."
      #echo "DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS"
      if /usr/bin/pgrep -u "$USER:?" -x cdemu-daemon >/dev/null; then
      mkdir -p ~/.config
      /usr/bin/cdemu status >~/.config/cdemu.save
      fi
      ;;
      esac






      linux systemd d-bus






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 11 at 22:10









      Rui F Ribeiro

      39.6k1479132




      39.6k1479132










      asked Jan 11 at 21:41









      basinbasin

      7481821




      7481821




















          0






          active

          oldest

          votes











          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',
          autoActivateHeartbeat: false,
          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%2f494031%2fuser-d-bus-service-post-start-pre-stop%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • 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%2funix.stackexchange.com%2fquestions%2f494031%2fuser-d-bus-service-post-start-pre-stop%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