You are not logged in.

#1 2020-09-14 12:59:27

tomsk
Member
Registered: 2016-12-17
Posts: 224

Run bash commands when I close terminal

Hello,

I use this code for starting my gaming VM (with GPU passthrough):

#!/bin/bash

chmod 777 /dev/input/by-id/usb-Razer_Razer_Lancehead_Tournament_Edition-event-mouse

# Start evdev (input)
exec -a EVDEV-INPUT-VM ./evdev /dev/input/by-id/usb-Razer_Razer_BlackWidow_Chroma_V2-if01-event-kbd &

# Fix permissions for scream-ivshmem (sound)
chmod 777 /dev/shm/scream-ivshmem

# Set CPU governor to performance
sh -c "echo -n performance > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor";
sh -c "echo -n performance > /sys/devices/system/cpu/cpu1/cpufreq/scaling_governor";
sh -c "echo -n performance > /sys/devices/system/cpu/cpu2/cpufreq/scaling_governor";
sh -c "echo -n performance > /sys/devices/system/cpu/cpu3/cpufreq/scaling_governor";

# Turn on presentation mode (disable sleeping)
inhibit_cookie=$(qdbus org.freedesktop.PowerManagement.Inhibit /org/freedesktop/PowerManagement/Inhibit org.freedesktop.PowerManagement.Inhibit.Inhibit "VM" "because")

# Move all windows to second screen if available


# Change video input on monitor
#ddccontrol -r 0x60 -w 17 dev:/dev/i2c-X

# KERNEL_SECURITY_CHECK_FAILURE fix for Fortnite
echo 1 > /sys/module/kvm/parameters/ignore_msrs

# Sleep for 1 sec
sleep 1

# Start VM
virsh start windows10Gaming-8GB

# Sleep for 1 sec
sleep 1

# Start htop
htop

but I wonder how can I run sets of commands when I close that terminal or I press Ctrl+C? --> Because I want to reset some values to default state after that.


I use several linux distros like: Archlinux, Ubuntu, Fedora, Linux Mint

Offline

#2 2020-09-14 13:26:48

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 77,724

Re: Run bash commands when I close terminal

trap SIGTERM, man trap

Offline

#3 2020-09-14 13:39:32

tomsk
Member
Registered: 2016-12-17
Posts: 224

Re: Run bash commands when I close terminal

seth wrote:

trap SIGTERM, man trap

Thank you so like this?

_term() { 
  echo "exit"
}

trap _term SIGTERM

I use several linux distros like: Archlinux, Ubuntu, Fedora, Linux Mint

Offline

#4 2020-09-14 13:48:59

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 77,724

Re: Run bash commands when I close terminal

"TERM", idk whether "SIGTERM" will cause an error, also you may want to trap "EXIT" (ie. "trap _term TERM EXIT") - and be sure to call it before running a blocking loop or child.

Offline

#5 2020-09-14 14:51:07

solskog
Member
Registered: 2020-09-05
Posts: 462

Re: Run bash commands when I close terminal

Maybe also interrupt and hungup signal?
SIGHUP - when you close terminal window.
SIGINT - Ctl_c to interrupt execution.

trap _term SIGTERM SIGINT SIGHUP EXIT

sh -c "echo -n performance > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor"

Just curious, do you have to spawn another shell to call echo? will this not working?

#/!/bin/bash
echo -n performance > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor

Last edited by solskog (2020-09-14 14:56:48)

Offline

#6 2020-09-14 14:59:49

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 77,724

Re: Run bash commands when I close terminal

That's actually the signals I bindly slap behind every "standard" trap smile
(So if I don't want to handle SIGSTOP or SIGUSR*)

Edit: nb. for completeness sale you cannot trap sigstop (or kill) this way

Last edited by seth (2020-09-14 15:04:16)

Offline

Board footer

Powered by FluxBB