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
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
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.
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