vol.2
Posts: 22
Joined: Tue Dec 13, 2016 5:17 pm

run a script when starting and exiting a program

Sat May 18, 2019 2:06 am

Hello,

How can I run a command when starting and exiting a program? I would like to change the system video mode when I run a specific program and then change it back to the mode in config.txt when the program exits.

This would be what I want to change it to:

vcgencmd hdmi_timings 1600 1 85 157 192 240 1 4 3 15 0 0 0 60 0 32000000 1
tvservice -e "DMT 87"
fbset -depth 32 && fbset -depth 32 -xres 1600 -yres 240

and then back to:

vcgencmd hdmi_timings 320 1 23 25 39 240 1 4 3 15 0 0 0 60 0 6400000 1
tvservice -e "DMT 87"
fbset -depth 32 && fbset -depth 32 -xres 320 -yres 240

when the program exits.

I think there must be a way to do this, like there is with a batch file in dos/windows.

Thanks,

klricks
Posts: 6503
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: run a script when starting and exiting a program

Sat May 18, 2019 3:05 am

vol.2 wrote:
Sat May 18, 2019 2:06 am
Hello,

How can I run a command when starting and exiting a program? I would like to change the system video mode when I run a specific program and then change it back to the mode in config.txt when the program exits.

This would be what I want to change it to:

vcgencmd hdmi_timings 1600 1 85 157 192 240 1 4 3 15 0 0 0 60 0 32000000 1
tvservice -e "DMT 87"
fbset -depth 32 && fbset -depth 32 -xres 1600 -yres 240

and then back to:

vcgencmd hdmi_timings 320 1 23 25 39 240 1 4 3 15 0 0 0 60 0 6400000 1
tvservice -e "DMT 87"
fbset -depth 32 && fbset -depth 32 -xres 320 -yres 240

when the program exits.

I think there must be a way to do this, like there is with a batch file in dos/windows.

Thanks,
You can use a bash script.
Open nano or your favorite editor:

Code: Select all

nano my_script

Code: Select all

#!/bin/bash
vcgencmd hdmi_timings 1600 1 85 157 192 240 1 4 3 15 0 0 0 60 0 32000000 1
tvservice -e "DMT 87"
fbset -depth 32 && fbset -depth 32 -xres 1600 -yres 240

 <<<< Put the program you want to run here..........

vcgencmd hdmi_timings 320 1 23 25 39 240 1 4 3 15 0 0 0 60 0 6400000 1
tvservice -e "DMT 87"
fbset -depth 32 && fbset -depth 32 -xres 320 -yres 240
CTRL x they y to save and exit

Note you will have to start your program by running this bash file.... Starting your program in the normal way won't run your special commands.

Code: Select all

bash /home/pi/my_script
Unless specified otherwise my response is based on the latest and fully updated Raspbian Buster w/ Desktop OS.

tpyo kingg
Posts: 562
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: run a script when starting and exiting a program

Sat May 18, 2019 3:41 am

You can take the above to the next level by referencing that script from a .desktop file which also launches your program. There are a lot of samples already there in Raspbian in places like /usr/share/applications/ which you can use for examples. However, the official specification is online but difficult to read so may have to refer to the examples a lot at first.

The gist is that you'd set something like the following line in there:

Code: Select all

Exec=sh -c '/path/to/script; /path/to/program; /path/to/script;'
Mind the quotes. Then you'll have an icon you can click on and it will run your script before and after the program. The icon won't automatically replace the one(s) already in any menus you might have. You'll have to do that manually.

vol.2
Posts: 22
Joined: Tue Dec 13, 2016 5:17 pm

Re: run a script when starting and exiting a program

Sat May 18, 2019 2:53 pm

klricks wrote:
Sat May 18, 2019 3:05 am
vol.2 wrote:
Sat May 18, 2019 2:06 am
Hello,

How can I run a command when starting and exiting a program? I would like to change the system video mode when I run a specific program and then change it back to the mode in config.txt when the program exits.

This would be what I want to change it to:

vcgencmd hdmi_timings 1600 1 85 157 192 240 1 4 3 15 0 0 0 60 0 32000000 1
tvservice -e "DMT 87"
fbset -depth 32 && fbset -depth 32 -xres 1600 -yres 240

and then back to:

vcgencmd hdmi_timings 320 1 23 25 39 240 1 4 3 15 0 0 0 60 0 6400000 1
tvservice -e "DMT 87"
fbset -depth 32 && fbset -depth 32 -xres 320 -yres 240

when the program exits.

I think there must be a way to do this, like there is with a batch file in dos/windows.

Thanks,
You can use a bash script.
Open nano or your favorite editor:

Code: Select all

nano my_script

Code: Select all

#!/bin/bash
vcgencmd hdmi_timings 1600 1 85 157 192 240 1 4 3 15 0 0 0 60 0 32000000 1
tvservice -e "DMT 87"
fbset -depth 32 && fbset -depth 32 -xres 1600 -yres 240

 <<<< Put the program you want to run here..........

vcgencmd hdmi_timings 320 1 23 25 39 240 1 4 3 15 0 0 0 60 0 6400000 1
tvservice -e "DMT 87"
fbset -depth 32 && fbset -depth 32 -xres 320 -yres 240
CTRL x they y to save and exit

Note you will have to start your program by running this bash file.... Starting your program in the normal way won't run your special commands.

Code: Select all

bash /home/pi/my_script
Thank you!
This is very helpful. I was able to do this. Ultimately, it was a dead-end because the program I wanted to change resolutions for (retroarch) was having trouble adjusting it's own resolution.
The issue with doing emulation of older systems on the pi is that there are problems with the output frequencies at lower settings (and this is outputting to a 15Khz tv) so in order to hit the correct freq and resolutions, the best solution is to have the pi squish the image really tight, and then have retroarch stretch it back out to the appropriate size for each system.
For some reason, I ended up having to back into the video settings everytime I start retroarch before the resolution stretches back out (i don't have to change anything, just accessing the video settings is enough) And then when I start a core (emulation core) it won't appy the core specific settings and it's still squished.
I think this is a little too hard for me still, so I'm going to try a different approach for now and maybe come back to this when I am more capable.

klricks
Posts: 6503
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: run a script when starting and exiting a program

Sat May 18, 2019 3:36 pm

vol.2 wrote:
Sat May 18, 2019 2:53 pm
.........
This is very helpful. I was able to do this. Ultimately, it was a dead-end because the program I wanted to change resolutions for (retroarch) was having trouble adjusting it's own resolution.
The issue with doing emulation of older systems on the pi is that there are problems with the output frequencies at lower settings (and this is outputting to a 15Khz tv) so in order to hit the correct freq and resolutions, the best solution is to have the pi squish the image really tight, and then have retroarch stretch it back out to the appropriate size for each system.
For some reason, I ended up having to back into the video settings everytime I start retroarch before the resolution stretches back out (i don't have to change anything, just accessing the video settings is enough) And then when I start a core (emulation core) it won't appy the core specific settings and it's still squished.
I think this is a little too hard for me still, so I'm going to try a different approach for now and maybe come back to this when I am more capable.
I don't follow what your problem is? But if it works from the command line then it should work from a script.. though you may need to add a delay to make sure the previous commands have completed before the program starts.

Code: Select all

#!/bin/bash
vcgencmd hdmi_timings 1600 1 85 157 192 240 1 4 3 15 0 0 0 60 0 32000000 1
tvservice -e "DMT 87"
fbset -depth 32 && fbset -depth 32 -xres 1600 -yres 240

sleep 3           #  adjust as necessary

 <<<< Put the program you want to run here..........

vcgencmd hdmi_timings 320 1 23 25 39 240 1 4 3 15 0 0 0 60 0 6400000 1
tvservice -e "DMT 87"
fbset -depth 32 && fbset -depth 32 -xres 320 -yres 240
It's also possible that you may need more delay between some of those commands...........
Unless specified otherwise my response is based on the latest and fully updated Raspbian Buster w/ Desktop OS.

vol.2
Posts: 22
Joined: Tue Dec 13, 2016 5:17 pm

Re: run a script when starting and exiting a program

Tue May 21, 2019 1:20 pm

klricks wrote:
Sat May 18, 2019 3:36 pm
vol.2 wrote:
Sat May 18, 2019 2:53 pm
.........
This is very helpful. I was able to do this. Ultimately, it was a dead-end because the program I wanted to change resolutions for (retroarch) was having trouble adjusting it's own resolution.
The issue with doing emulation of older systems on the pi is that there are problems with the output frequencies at lower settings (and this is outputting to a 15Khz tv) so in order to hit the correct freq and resolutions, the best solution is to have the pi squish the image really tight, and then have retroarch stretch it back out to the appropriate size for each system.
For some reason, I ended up having to back into the video settings everytime I start retroarch before the resolution stretches back out (i don't have to change anything, just accessing the video settings is enough) And then when I start a core (emulation core) it won't appy the core specific settings and it's still squished.
I think this is a little too hard for me still, so I'm going to try a different approach for now and maybe come back to this when I am more capable.
I don't follow what your problem is? But if it works from the command line then it should work from a script.. though you may need to add a delay to make sure the previous commands have completed before the program starts.

Code: Select all

#!/bin/bash
vcgencmd hdmi_timings 1600 1 85 157 192 240 1 4 3 15 0 0 0 60 0 32000000 1
tvservice -e "DMT 87"
fbset -depth 32 && fbset -depth 32 -xres 1600 -yres 240

sleep 3           #  adjust as necessary

 <<<< Put the program you want to run here..........

vcgencmd hdmi_timings 320 1 23 25 39 240 1 4 3 15 0 0 0 60 0 6400000 1
tvservice -e "DMT 87"
fbset -depth 32 && fbset -depth 32 -xres 320 -yres 240
It's also possible that you may need more delay between some of those commands...........
So, the piece of information I neglected to include is that I can only get it working this way when I set the video settings in the config.txt in raspian, and in the retroarch.cfg file for retroarch. However, that way is impractical because you load into an unreadable raspian state.
Unfortunately, if I try to do it the way you helped me to do in this post, it malfunctions. It switches the resolution for me, but then when I launch retroarch, it stays squished almost all the time. The only time I seem to be able to get retroarch to unsquish in this method is when I go to the video settings menu, but this only lasts until I try to launch an emulator and then it re-squishes itself. None of this should be happening because retroarch itself has it's own video mode settings that are supposed to take effect (which is apparent by the fact that it works correctly when I load the raspian settings via config.txt). I suppose that means that there is some kind of dependency the retroarch has on the contents of the config.txt file, although I don't know enough about the way it works to figure it out at the moment.

I found a specialized image of lakka that works really well with the kind of rgb setup i have, so I decided to abandon this approach for at least the time being. I will maintain a second image with raspian on it to use for those purposes.

Thanks for the guidance!

Return to “Beginners”