LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 08-29-2016, 09:28 PM   #16
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 1,789

Original Poster
Rep: Reputation: 139Reputation: 139

syg00, can you post an example (need not be tested). I am totally lost with this. It seems to me order/dependency would be a very needful thing. It can't be as hard as this Thread implies, can it?
 
Old 08-29-2016, 10:05 PM   #17
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,853

Rep: Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486
If you are trying to stop virtualbox, you might look to see if you have the systemd units for libvertd and libvirtd-guests. These are the ones used to stop qemu VMs. Mine has:

libvirt-guests.service:
Code:
[Unit]
Description=Suspend Active Libvirt Guests
After=network.target libvirtd.service time-sync.target
Documentation=man:libvirtd(8)
Documentation=http://libvirt.org

[Service]
EnvironmentFile=-/etc/sysconfig/libvirt-guests
# Hack just call traditional service until we factor
# out the code
ExecStart=/usr/libexec/libvirt-guests.sh start
ExecStop=/usr/libexec/libvirt-guests.sh stop
Type=oneshot
RemainAfterExit=yes
StandardOutput=journal+console
TimeoutStopSec=0

[Install]
WantedBy=multi-user.target
And libvirtd.service:

Code:
# NB we don't use socket activation. When libvirtd starts it will
# spawn any virtual machines registered for autostart. We want this
# to occur on every boot, regardless of whether any client connects
# to a socket. Thus socket activation doesn't have any benefit

[Unit]
Description=Virtualization daemon
Before=libvirt-guests.service
After=network.target
After=dbus.service
After=iscsid.service
After=apparmor.service
After=local-fs.target
After=remote-fs.target
Documentation=man:libvirtd(8)
Documentation=http://libvirt.org

[Service]
Type=notify
EnvironmentFile=-/etc/sysconfig/libvirtd
ExecStart=/usr/sbin/libvirtd $LIBVIRTD_ARGS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
# Override the maximum number of opened files
#LimitNOFILE=2048

[Install]
WantedBy=multi-user.target
Also=virtlockd.socket
Also=virtlogd.socket
The primary note here is that they require the network. Thus during shutdown, they must be stopped BEFORE the network is stopped. A secondary note is that this has a "WantedBy=multi-user.target". So as soon as multi-user.target is being shutdown (it is just a node in the dependency network, nothing actually gets done) the libvirtd and libvirtd-guests may be run (and with their dependency, the order during shutdown is determined by having to do libvirtd-guests.service before doing libvirtd.service.

Stopping the network is done rather early in the shutdown sequence - not first, as other services may ALSO need to use the network during their shutdown (databases, web servers, code management, sshd, ...).

Can this STILL cause problems? Yes - consider a service within a VM that uses a database from the host - is the database shutdown out from under the VM service that needs to be shutdown first? sometimes, sometimes not. The two processes being shutdown are done in "parallel".

What is ensured is if the VM uses NFS, the VM will be gracefully shutdown first (the libvirtd-guests.service does that, then the libvirtd service supporting the VMs is shutdown, then the network is shutdown).

These two units may get you what you want. Not necessarily perfectly, but perhaps close enough.
 
Old 08-29-2016, 10:38 PM   #18
notKlaatu
Senior Member
 
Registered: Sep 2010
Location: Lawrence, New Zealand
Distribution: Slackware
Posts: 1,075

Rep: Reputation: 725Reputation: 725Reputation: 725Reputation: 725Reputation: 725Reputation: 725Reputation: 725
I've not tested this with VM shutdowns, because that's not my use case, but I have systemd scripts on CentOS servers which run successfully prior to shutdown. The shutdown target is not permitted to finish until all of its dependent processes are complete.

You can see this, more or less, with an fake example to mimic the process:

Set up a service to trigger FIRST (this would be the shutdown service):

Code:
# cat /lib/systemd/system/fakehalt.service

[Unit]
Description=Fake-Halt Service
After=fakevm.service
Requires=fakevm.service

[Service]
Type=simple
ExecStart=/usr/local/bin/fakehalt.sh #this will fail until fakevm succeeds
ExecReload=/usr/local/bin/fakehalt.sh
And then set up the one that you want to run and complete BEFORE shutdown is permitted:

Code:
# cat /lib/systemd/system/fakevm.service
[Unit]
Description=Fake-VM-Shutdown Service
Before=fakehalt.service

[Service]
Type=simple
ExecStart=/usr/local/bin/fakevm.sh
Create a script to represent the VM shutdown (or any process that you cannot anticipate the duration of)

Code:
# cat /usr/local/bin/fakevm.sh
#!/bin/sh

test="1"
sleep 21
if [ X"$test" = "X1" ]; then
    echo "vm has shut down" > /tmp/vmfake.test
    exit 0
else 
    exit 1
fi
And a script to pass for a shutdown signal:

Code:
# cat /usr/local/bin/fakehalt.sh
#!/bin/sh

sleep 3
cat /tmp/vmfake.test > /tmp/haltfake.test
Start the service you want to happen AFTER the first one:

Code:
# systemctl start fakehalt
What "should" happen is that fakehalt will fail to find a file called /tmp/fakevm.test to cat from, and so everything should go horribly wrong.

What actually happens is that systemd being systemd, it places fakehalt service on hold until it gets an exit 0 signal from the fakevm service. So if you wait 21 seconds and cat /tmp/fakehalt.test, you see that the cat from a file that did not exist when fakehalt was started - actually succeeded.

I can confirm this works; it's a simplified version of a systemd scheme I am running on CentOS 7.2

I'd be interested in hearing the results from OP with a VM test.
 
Old 08-29-2016, 11:36 PM   #19
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 1,789

Original Poster
Rep: Reputation: 139Reputation: 139
jpollard: VirtualBox does not use libvertd or any other such service. Nothing about VirtualBox is started by services at boot time. This VM is started by the user only. In fact, root has no commands to even determine if a VM is running. I've solved that problem (see: https://forums.virtualbox.org/viewto...c350e049f356e2), and now need a way to run the script!

notKlaatu: I will study your extensive post, there are lots of eye-openers in there. Thanks for your extensive response. I've said I need my script to run "first" at shutdown/reboot, but in fact what I need for it to do is run before any login session are killed. Is there a target for that? In looking at my /lib/systemd/system unit files, I find killprocs.service, halt.service, reboot.service all symlinked to /dev/null. This is highly confusing to me. Who/what kills login sessions?

Last edited by mfoley; 08-29-2016 at 11:38 PM.
 
Old 08-30-2016, 12:01 AM   #20
notKlaatu
Senior Member
 
Registered: Sep 2010
Location: Lawrence, New Zealand
Distribution: Slackware
Posts: 1,075

Rep: Reputation: 725Reputation: 725Reputation: 725Reputation: 725Reputation: 725Reputation: 725Reputation: 725
Quote:
Originally Posted by mfoley View Post
Who/what kills login sessions?
I think it's /usr/lib/systemd/systemd-logind

which is sourced by:
/lib/systemd/system/systemd-logind.service

Code:
$ man systemd-logind.service
Worth a shot, anyway!

Last edited by notKlaatu; 08-30-2016 at 12:04 AM. Reason: copied path from man page, realised it was a path to a binary and not the service.
 
Old 08-30-2016, 06:27 AM   #21
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,853

Rep: Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486
Quote:
Originally Posted by mfoley View Post
jpollard: VirtualBox does not use libvertd or any other such service. Nothing about VirtualBox is started by services at boot time. This VM is started by the user only. In fact, root has no commands to even determine if a VM is running. I've solved that problem (see: https://forums.virtualbox.org/viewto...c350e049f356e2), and now need a way to run the script!
I know that. I provided the libvirtd-guest.service as an example. If virtual box has nothing to stop the VMs, then there is nothing to do.

Which only leaves libvertd.service as the example.

It is only an example of a similar feature. That is all.
Quote:

notKlaatu: I will study your extensive post, there are lots of eye-openers in there. Thanks for your extensive response. I've said I need my script to run "first" at shutdown/reboot, but in fact what I need for it to do is run before any login session are killed. Is there a target for that? In looking at my /lib/systemd/system unit files, I find killprocs.service, halt.service, reboot.service all symlinked to /dev/null. This is highly confusing to me. Who/what kills login sessions?
In which case it would be specified to run after "multi-user.target" - which would cause it to be processed on shutdown BEFORE multi-user.target is shutdown.
 
Old 08-30-2016, 12:09 PM   #22
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 1,789

Original Poster
Rep: Reputation: 139Reputation: 139
So, this?
Code:
[Unit]
Description=local shutdown
DefaultDependencies=no
Before=shutdown.target reboot.target halt.target multi-user.target

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/etc/rc.local_shutdown

[Install]
WantedBy=multi-user.target
All that's new is that I've added "multi-user.target" to the Before directive. Does this make my WantedBy now incorrect?
 
Old 08-30-2016, 01:01 PM   #23
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,853

Rep: Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486
Try it and see. It looks OK.

You might make certain there is some output from the script and see if it shows up in the logs before other services.

Last edited by jpollard; 08-30-2016 at 01:02 PM.
 
Old 08-30-2016, 02:28 PM   #24
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 1,789

Original Poster
Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by jpollard View Post
Try it and see. It looks OK.

You might make certain there is some output from the script and see if it shows up in the logs before other services.
No, doesn't work Here's my shutdown script:
Code:
#!/bin/bash

echo `date "+%Y-%m-%d %H:%M:%S" ` testing Rebooting argh, argh >>/shutdown.test
echo local shutdown running
echo local shutdown running >&2
exit 0
Nothing in /shutdown.test, although it should be noted that the script wasn't working before, when I did not have the "multi-user.target" in Before. So, there must be something fundamentally wrong with the unit in the first place.

... getting ready to give up on this ...

Last edited by mfoley; 08-30-2016 at 02:37 PM.
 
Old 08-30-2016, 07:00 PM   #25
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,853

Rep: Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486
There should be SOMETHING... even a log entry showing an error.
 
Old 08-30-2016, 11:53 PM   #26
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 1,789

Original Poster
Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by jpollard View Post
There should be SOMETHING... even a log entry showing an error.
Suggestions on which log might have that? I've looked in /var/log/syslog. Nothing.

I found a command: `systemd-analyze verify <unit>` which will syntax-check my unit file, but I have to wait until I reboot, and possibly restore that machine as my "experiments" have rendered it dead in the water for the moment.

Last edited by mfoley; 08-30-2016 at 11:55 PM.
 
Old 08-31-2016, 12:39 AM   #27
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,502

Rep: Reputation: 2617Reputation: 2617Reputation: 2617Reputation: 2617Reputation: 2617Reputation: 2617Reputation: 2617Reputation: 2617Reputation: 2617Reputation: 2617Reputation: 2617
the system logs are no longer in /var/log
systemd has it's own log SEE:
Code:
su -
systemd-journalctl --help
 
Old 08-31-2016, 02:02 AM   #28
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 15,305
Blog Entries: 9

Rep: Reputation: 4379Reputation: 4379Reputation: 4379Reputation: 4379Reputation: 4379Reputation: 4379Reputation: 4379Reputation: 4379Reputation: 4379Reputation: 4379Reputation: 4379
tl;dr.

Quote:
Originally Posted by mfoley View Post
As to my objective, I am running a VirtualBox Windows 7 VM. If the VM is still running at reboot or shutdown time, it is as if the power cord is pulled on the VM and I get the "Start Windows Normally" option screen, potentially with disk corruptions to deal with. I want to send a command to shut it down gracefully at shutdown or reboot time (which I can do). However, I want to make sure this runs before the login task are terminated or all is for nought! The VM will be long gone before I get to the graceful shutdown.
this belongs to logging out from a graphical session.
not to the init system.
i am achieving exactly what you want with this script, which is run when i press one of the logout/reboot/shutdown buttons:
Code:
#!/bin/bash
# close_all_windows (johnraff)
# depends on wmctrl

IGNORE=' Conky($|[ \t])| tint2($|[ \t])' # regular expression to check against window titles
ob_windows_list=($(wmctrl -l | awk '!/'"$IGNORE"'/{print $1}')) # get ids
for i in "${ob_windows_list[@]}"
do
    wmctrl -i -c $i
    sleep 0.2
    wmctrl -l | grep "$i" >/dev/null 2>&1 && wmctrl -i -R $i
    while wmctrl -l | grep "$i" >/dev/null 2>&1
    do
        sleep 0.2
    done
done

if pidof transmission-daemon >/dev/null 2>&1 ;\
then transmission-remote --exit ;\
    while pidof transmission-daemon >/dev/null 2>&1; do \
        sleep 0.1; \
    done ;\
fi

exec $@
you pass the actual logout/shutdown/reboot command as an argument to the script.

you're welcome.
 
Old 08-31-2016, 05:02 PM   #29
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 1,789

Original Poster
Rep: Reputation: 139Reputation: 139
ondoho: where do you keep that script and how is it invoked, software-wise? I.e., what causes the script to run?
 
Old 08-31-2016, 05:44 PM   #30
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 1,789

Original Poster
Rep: Reputation: 139Reputation: 139
I'm trying something a bit radical which I'll describe in a following posting, but first, regarding systemd. I've spent quite a few hours researching systemd and what it is all about. Apparently I've missed the debate over the past few years. The following links are to blogs from one of the (nameless) designers of systemd: http://0pointer.de/blog/projects/why.html, http://0pointer.de/blog/projects/systemd.html. The latter is a good overview of the design goals. Which are? Essentially parallelism (i.e. not serially waiting for one startup task to finish before running another) for faster booting, and "lazy-loading of services: if a service is rarely used, we can just load it the first time somebody accesses the socket or bus name, instead of starting it during boot." The author further enthuses, "And if that's not great, then I don't know what is great!". Also, if you look at the list of things systemd does (1st referenced link) you can see that systemd controls a lot, Mount handling, fsck handling, Quota handling, Automount handling, Swap handling, PAM integration and many, many more things. In short, it consolidates, centralizes and integrates lots of OS task that were handled by individual daemons. Other info resources, https://www.freedesktop.org/wiki/Software/systemd/ (links to manuals), https://wiki.debian.org/systemd, https://wiki.ubuntu.com./SystemdForUpstartUsers

I also found a few hater sites: http://systemd-free.org/ http://without-systemd.org/wiki/index.php/Main_Page, and misc related blogs.

My thoughts. We are now on 2 pages and 30 posts with some of the best minds in Unixdom trying to figure out a way to simply execute a script/service before all others at shutdown using systemd. From a user/sysadmin perspective, systemd is way more complex than Upstart or sysvinit. It should be noted that the developer/author's 1st cited blog mentions, without irony, that there is a company started up just to consult on systemd. Hmmm, that should be an indicator of systemd's complexity. I don't know of any companies started up just to help people with sysvinit or Upstart configurations. Beyond that, I think systemd solves non-problems. I've never groaned at the amount of time it takes to boot my Ubuntu system. Typically 20-30 seconds. My biggest time-waits are for network connections to be established with the various LAN servers -- nothing boot-speed can do about that. Secondly, my workstations are in a business office environment which are rarely rebooted (monthly at best) and even more rarely powered down -- backups are run after hours, updates done on the weekends, etc. Is reboot speed such a big issues that we need more parallelism? Could that not be largely solved with an apmersand '&' at the end of the script command? Next, is "lazy-loading of services" really that needful? So what if an unused daemon is sitting, waiting? Even on my most overworked 10 year old, 1GB memory Intel Atom Linux machine which does constant network backups to local USB drive while at the same time acting as a WAP typically runs 90% idle. Not sure "lazy-loading" sovles an actual problem.

Finally, I think the design ideas of systemd fly in the face of Unix design principles. The idea was small modules, connected by pipes, to accomplish bigger tasks. Unix was a rebellion against IBM/MVS and even DEC/VMS with their highly integrated, monolithic, interdependencies. Monolithic programs always end up getting the better of developers and users since we get lost among all the interdependent, often concurrent "twisty little passages" trying to track down problem (current thread is a case in point). By contrast, sysvint and Upstart scripts are small and easily debugged.

And ... it wouldn't take a week, 2 pages, 30 posts and half a dozen unix experts to fail to figure out how to do init prioritization with sysvint or Upstart! For the moment, I'm in the not-systemd camp.
 
2 members found this post helpful.
  


Reply

Tags
shutdow, systemd


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Systemd service: AUTOSSH.service does not start at boot, how can I fix this? The_Linux_Machine Linux - Newbie 1 07-06-2016 12:00 PM
Can't access full journalctl from script via systemd service even though user is in systemd-journal group iwtbf Linux - Newbie 0 02-19-2016 02:44 PM
systemd service aagaag Linux - Newbie 1 01-18-2016 07:42 PM
Boot Delay 30min: systemd-analyze blame systemd-tmpfiles-setup.service BGHolmes Fedora 0 07-27-2011 09:02 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 06:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration