This is a practical example how to use systemd. There are two examples of services:
StartVM.service
[Unit]Description=Start VirtualBox Machine and power on qnap.netAfter=network.targetAfter=systemd-user-sessions.serviceAfter=network-online.target[Service]Type=oneshotExecStart=/home/oscargomezf/SVN/admin/virtualbox_scripts/StartVM
StandardOutput=syslogStandardError=syslog[Install]WantedBy=multi-user.targetAnd StopVM.service
[Unit]Description=Stop VirtualBox Machine and power off qnap.netRequires=network.targetAfter=network.targetBefore=shutdown.target reboot.target halt.target[Service]Type=oneshotRemainAfterExit=yesExecStop=/home/oscargomezf/SVN/admin/virtualbox_scripts/StopVMExecStopPost=/bin/sleep 200StandardOutput=syslogStandardError=syslogTimeoutStopSec=220[Install]WantedBy=multi-user.targetAnd these are the scripts that we are going to execute:
StartVM
#!/bin/bash#Edit these variables!VM_USER="oscargomezf"
VM_REDMINE="bitnami-redmine-3.3.1-0-ubuntu-14.04-x86_64"MAC_QNAP_NET=XX:XX:XX:XX:XX:XX
#echo "Starting VirtualBox $VM_REDMINE ..."#sudo -u $VM_USER VBoxManage startvm $VM_REDMINE --type headlessecho "Wake On Lan qnap.net: $MAC_QNAP_NET" powerwake $MAC_QNAP_NETsleep 1powerwake $MAC_QNAP_NETsleep 1powerwake $MAC_QNAP_NETexit 0StopVM
#!/bin/bash
#Edit these variables!
VM_USER="oscargomezf"VM_REDMINE="bitnami-redmine-3.3.1-0-ubuntu-14.04-x86_64"MAC_QNAP_NET=XX:XX:XX:XX:XX:XXecho "Shutdown qnap.net"/home/oscargomezf/SVN/admin/qnap_scripts/poweroff_qnap.net.shecho "Saving state of Virtualbox $VM_REDMINE ..."#sudo -u $VM_USER VBoxManage controlvm $VM_REDMINE savestateexit 0To enable the services you have to copy the *.service files to /etc/systemd/system/ execute:
# cp *.service /etc/systemd/system/# systemctl enable StartVM.service# systemctl enable StopVM.service# systemctl daemon-reload
And for logging the result you have to use:
# journalctl -xe