3

In a previous question, I found out how to shut down all instances of running VMs started through vagrant. I'd like to call that script when logging out of my Desktop Manager which is currently Cinnamon.

Some have pointed to another similar similar, but I did not understand the selected answer(s) enough to be able to solve my issue and put anything into action. The comments below point to start on stopping lightdm. This sounds like what I'm looking for. That is how can I run a script to stop all VMs (called: stop-vagrant.sh) when my log out of my desktop manager (start on stopping lightdm)?

Rick
  • 2,847
  • try this link – Lety Oct 17 '14 at 19:46
  • Logout is trickier, because Upstart has events for a GUI logout, but not for a TTY/SSH one. Otherwise @Letizia's suggestion is the way to go. – muru Dec 02 '14 at 15:54
  • Can it be connected to the stopping of the desktop manager? I appreciate @Letizia 's suggestion but I didn't understand those answers to a point where I could begin to take action. Do I place start on / stop on commands in .bashrc/.bash_profile? Do I append that phrase to the start of a command vagrant up stop on <condition>. – Rick Dec 02 '14 at 15:54
  • @Richard yes.. Something like start on stopping lightdm – muru Dec 02 '14 at 15:55
  • But do I key that phrase into the shell or profile? And how do those four words get associated with my vagrant-stop.sh script? @muru – Rick Dec 02 '14 at 15:57

1 Answers1

3

What you need to do is create an Upstart job. Let us say your script for stopping VMs is available at /some/path/vagrant-stop.sh.

Depending on the exact nature of your VMs (are they run only when you're logged into a GUI, or are they run when the system starts up, etc.), you could use either a System job or a Session job. For the case where the VMs are run only when you're logged into a GUI, you can use a session job, a simple example of which I'll present here:

Create a file in ~/.config/upstart with a .conf extension (use your favourite editor):

vim ~/.config/upstart/stop-vms.conf

The content would be:

description "Give your task a description"
start on session-end

task

exec /some/path/vagrant-stop.sh

Run initctl check-config to see if there are any errors.

Now, you can stop your VMs with (pardon the phrasing):

start stop-vms

Upstart will automatically run this when you log out.

muru
  • 197,895
  • 55
  • 485
  • 740
  • What will be the systemd way of doing this? I mean I need to run a script at logout, not shutdown event – Anwar Apr 24 '18 at 13:38
  • @Anwar I haven't tested it, but I'd think a user service, say ~/.config/systemd/user/foo.servce, with an ExecStop=some-command would be enough (since systemd stops all user services on logout, unless the user has linger enabled) – muru Apr 24 '18 at 13:44
  • hmm.. thanks for quick response. I tested with user service, but couldn't get it to work. for some reason the service was active. Basically what I wanted was running two separate script with systemd upon login and logout (graphical would be better) – Anwar Apr 24 '18 at 13:48
  • @Anwar which version of Ubuntu? If you're on 16.04, Upstart user sessions are still available. – muru Apr 24 '18 at 13:53
  • I've on 17.10 and more importantly, I don't want to use upstart etc. – Anwar Apr 27 '18 at 16:15