Need help with VM startup & shutdown script

My VM is already setup via virt-manager, but I want to create a bash script that does the following

vmstartup script
checks to see if synergy is running, if it is skip else start it
check to see if VM is running, if so skip else start it
xrandr turns HDMI# off
start the vm via virsh command
xrandr turns on DP1 with hz and resolution.
exits script

Checks to see if vm is running
vmstop script
shutdown vm via virsh
xrandr turns DP1 off
xrandr turns on HDMI# with hz and resolution
Checks to see if synergy is running, if its not skip else turn off
exits script

Here is some of the code idea i got. Im not to skilled at scripts so any help I would be appreciated, or maybe someone already has a script like this i could snag.

#!/bin/bash

vmname="win10"

if ps -A | grep -q $vmname; then
   echo "$vmname is already running." &
   exit 1

else

virsh start win10
1 Like

Let me know if I am off on any of the following. Just trying to pull from varius scripts I have found. I think the following will script would remain open until vm is shutdown and synergy is off..... i think

#!/bin/bash

service="synergy"

if ps -A | grep -q $service; then
   echo "$service is already running." &
   exit 1

else

/usr/bin/synergy

echo "Starting VM"

vmname="win10"

if ps -A | grep -q $vmname; then
   echo "$vmname is already running." &
   exit 1

else

   /usr/bin/ virsh start win10

echo "VM Closed"                                                                                                                                            
                                                                                                                                                            
echo "Stopping Synergy"                                                                                                                                     
pkill synergy

Aw, you undid my bash highlighting edit :P

Do you want the script to keep running until the vm is shutdown?

You also might find it better to use systemctl to manage running the service / checking the service is good

Either way is fine with it script keeps running or if I need two scripts, one to turn on and and one to shutdown. Im just guessing here and looking for some help. And my bad man, I edited some of the bash cause something seemed out of the ordinary whether it works or not.

I actually have a reverse synergy setup, passing the usb 3.0 controller to VM to prevent game controller issues and Headset audio issues. This means I have the synergy server on the vm and the client is on the host.

Consider the following if you want to use them instead

systemctl is-active $service

You can use it like this

if [[ $(systemctl is-active $service) == "active" ]]
then
    #do nothgin
else
    #code
    systemctl start $service
fi

## you might want to check for errors though it should 
## output an error if it failed to start

This will have the upside of properly recording errors in start up as well.

For checking if a vm is running

virsh list --state-running --name

so you could do something like this

if [[ $(virsh list --state-running --name) == "win10" ]]
then
    #code
else
    #code
fi

If you have more than one VM

if [[ $(virsh list --state-running --name | grep win10) == "win10" ]]
then
    #code
else
    #code
fi

Once done, stop synergy with systemctl

systemcrl stop $service

You could put the script into a while look checking that the VM is up and running and only killing everything once the VM of turned off, if thats what your going for. because your script will likely kill synergy as soon as is starts your vm as is.

1 Like

How would I do the while part? I think thats what I am looking for, seems the safest.

You could probably do something like this.

while true
do
    if [[ $(virsh list --state-shutoff --name | grep win10) == "win10" ]]
    then
        echo "VM not running"
        # do stuff
        break # exit the loop
    fi
    # sleep for 15 seconds
    sleep 15
done

Ok cool thanks for your help. Ill start working on this in a few and post some stuff here with outcome.

1 Like

Does this seem right?

if [[ $(systemctl is-active synergy) == "active" ]]
then
    #do nothing
else
    #code
    systemctl start synergy
fi



if [[ $(virsh list --state-running --name | grep win10) == "win10" ]]
then
    xrandr --output HDMI2 --off
    xrandr --output DP1 --mode "1920x1080" --rate 144 --right-of HDMI1
else
    virsh start win10
    xrandr --output HDMI2 --off
    xrandr --output DP1 --mode "1920x1080" --rate 144 --right-of HDMI1
fi



while true
do
    if [[ $(virsh list --state-shutoff --name | grep win10) == "win10" ]]
    then
        echo "VM not running"
        xrandr --output DP1 --off
        xrandr --output HDMI2 --mode "1920x1080" --rate 144 --right-of HDMI1
        systemctl stop synergy
        break # exit the loop
    fi
    # sleep for 15 seconds
    sleep 15
done

Looks like it probably is