Where to put commands to be ran after starting up and before shutting down?

Hello,

I am trying to put some commands for my virtualboxes machines to start up and shutdown gracefully, on OpenSuse 12.2. I found this blog that helps to create and enable the old after.local and before.local scrips and worked:
https://forums.opensuse.org/content/120-systemd-using-after-local-script-opensuse-12-1.html

But this seems to be a way to “enable the old way” when systemd was not present, so is there a default and ready place on Opensuse 12.2 to put some commands to run after startup and before shutdown?

Thank you very much,

S.

Here’s a way to do it with systemd:

scripting - Execute Script automatically at shutdown on Fedora 15/16 - Unix and Linux

You can enable the service with

systemctl enable runonshutdown.service

BTW, here’s a nice openSUSE guide for systemd:

openSUSE 12.2: Chapter 8. The systemd daemon

Note:

When enabling a service on the command line, it is not started automatically. It is scheduled to be started with the next system start-up or runlevel/target change. In order to immediately start a service after having enabled it, explicitly run systemctl start <my_service>.service or rc<my_service> start**.

Why do you want it “after” and “before”? If you simply create service and enable it it will be started during startup and stopped during shutdown. Won’t that work for you?

Thanks for the opensuse 12.2 systemd guide link I am already reading about systemd and its features, also your help is great!

To answer why I want to run commands at start (just after everything is up and running) and before shutdown (just before starting to shutdown every service) its because I need to start and shutdown gracefully my virtualbox machines.

Its me, my hardware or my 12.2 installation… I dont know, still looking for a clean solution. The problem I have is that I can startup the virtualbox machines on startup automatically (using the **vboxes service **that gets installed when you install vbox) but this services seems not to shutdown gracefully (I mean it should do a savestate or an acpi shutdown… but dont kill the virtual machine because its like pluggin out the power cord) so, thats why I am trying to put the commands I need after startup and before shutdown.

Thanks,

PD: here I post the problem with virtualbox shutdown, but I have no answer, maybe I need to post more info now I have tested several ways.
https://forums.opensuse.org/english/get-technical-help-here/virtualization/482311-opensuse-12-2-vb-4-2-6_ose-cant-auto-shutdown-cleanly-vms-shutdown-reboot.html

On 2013-01-22 20:36, sebadamus wrote:
> To answer why I want to run commands at start (just after everything is
> up and running) and before shutdown (just before starting to shutdown
> every service) its because I need to start and shutdown gracefully my
> virtualbox machines.

Well, I think that should be done as a service defined to start after
what it needs is already started. Actually, the virtualbox developers
should already provide such a service.


Cheers / Saludos,

Carlos E. R.
(from 11.4, with Evergreen, x86_64 “Celadon” (Minas Tirith))

For different reasons I’ve been delving into systemd over the past couple days (There’s a lot to know!) but since I noticed your post have been keeping an eye out for your solution as well…

I have not seen anything obvious yet to shutdown manually started processes, but there is documentation how to define a “Unit Service” so that you can then define dependencies and requirements whenever you launch your “Service” which doesn’t necessarily have to be autostarted.

Also, because of backwards compatibility with systemV init, I imagine you could also similarly define your “Service” as an init script also defining dependencies and requirements.

There are a couple ways to learn how to do it the “systemd way,” although you can read the man pages, I’ve found the following series of blog posts by one of the authors of systemd very easy reading and immensely infomrative

Wunschkonzert, Ponyhof und Abenteuerspielplatz
Wunschkonzert, Ponyhof und Abenteuerspielplatz

Although I don’t recognize how to do it immediately, if you’re using KDE I think you should be able to declare something very easily in

App Launcher > Configure Desktop > Startup and Shutdown

HTH,
TSU

HTH,
TSU

Then this is the problem you need to solve, not where to put shutdown script.

The first step would to be to manually run the same script that should shutdown your VMs. Does it work when running it manually? How much time does it need to complete?

Thanks arvidjaar! If I know where to put my custom startup/shutdown commands (not just for a user login, I mean for the system), I could be able to startup my vm and shut it down fine, without looking for a solution for the vboxes service that came with virtualbox that seems to be buggy. Anyway I prefer if I know how :X to run this commands myself in a script)

I can start it fine with:

sudo -u vbox VBoxHeadless --startvm "machine1" -vrde off &

(I need to run “sudo -u vbox” because its the user where I have installed my machine1 vm)

I can “savestate” fine with:

VBoxManage controlvm "machine1" savestate

What is the exact name of these “vboxes services”? Is it initscript or systemd unit?

Thanks Arvidjaar. Well, I think its a systemd. its on “/etc/init.d/vboxes”

If I run “service vboxes status”

It says:

quahog: # service vboxes status
vboxes.service - LSB: Autostart Virtual Box VMs
      Loaded: loaded (/etc/init.d/vboxes)
      Active: inactive (dead) since Wed, 23 Jan 2013 20:13:44 -0300; 19h ago
      **CGroup: name=systemd:/system/vboxes.service**

Jan 23 20:13:42 quahog vboxes[2873]: Starting Virtualbox machines: Win2003 ...x)
Jan 23 20:13:42 quahog vboxes[2873]: Starting virtual machine: Win2003 (use...ne
Jan 23 20:13:42 quahog vboxes[2873]: rm: cannot remove '/tmp/vboxes.2873': ...ry
Jan 23 20:13:42 quahog su[3085]: (to vbox) root on none
Jan 23 20:13:44 quahog vboxes[3022]: ...killed.
Jan 23 20:13:44 quahog vboxes[3022]: Shutting down Virtualbox machines: no ...ed

And if I: “systemctl status vboxes.service”


quahog: # systemctl status vboxes.service
vboxes.service - LSB: Autostart Virtual Box VMs
      Loaded: loaded (/etc/init.d/vboxes)
      Active: inactive (dead) since Wed, 23 Jan 2013 20:13:44 -0300; 19h ago
      CGroup: name=systemd:/system/vboxes.service

Jan 23 20:13:42 quahog vboxes[2873]: Starting Virtualbox machines: Win2003 (us...x)
Jan 23 20:13:42 quahog vboxes[2873]: Starting virtual machine: Win2003 (user: ...ne
Jan 23 20:13:42 quahog vboxes[2873]: rm: cannot remove '/tmp/vboxes.2873': No ...ry
Jan 23 20:13:42 quahog su[3085]: (to vbox) root on none
Jan 23 20:13:44 quahog vboxes[3022]: ...killed.
Jan 23 20:13:44 quahog vboxes[3022]: Shutting down Virtualbox machines: no vir...ed 

Well … I would rather try to investigate why it does not start properly, but if you prefer other way …

Something like this may work.

[Unit]
Description=Start virtual machine machine1
After=vboxes.service

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=-/path/to/vbox VBoxHeadless --startvm machine1 -vrde off
ExecStop=-/path/to/vbox VBoxManage controlvm machine1 savestate
KillMode=none
User=vbox

[Install]
WantedBy=multi-user.target

Put it in the file /etc/systemd/system/vbox-machine1.service, do “systemctl daemon-reload”, try “systemctl start vbox-machine1.service” and “systemctl stop vbox-machine1.service” and if they work as expected do “systemctl enable vbox-machine1.service”.

It will be started during startup after /etc/init.d/vboxes and should be stopped during shutdown before vboxes … if they had been started successfully. If there is second service that starts some vbox related daemons, you should add it to After= line.

Completely untested of course :slight_smile:

Checkout https://github.com/Jetchisel/VBoxAutostart/archive/master.zip and the instructions are there as well cheers lol!

Thanks for your post Jetchisel I will take a look! As far as I could test, Opensuse 12.2 and Virtualbox are not fine tunned… seems to me Ubuntu 12.04 is more customized to work out of the box with virtualbox.