MazHoot
Posts: 7
Joined: Tue Dec 15, 2020 11:40 pm

shutting down in sh script don't work after exec [ RESOLVED ]

Tue Dec 15, 2020 11:50 pm

I'm trying to shut down raspberry pi when I close a program which is automatically started. I use it with a gamepad and when I quit the program, I would like to the rapsberry pi quit.

I have a pig.sh file located in /home/pi/.config/lxsession/LXDE-pi :

Code: Select all

#!/bin/sh
cd /home/pi/Desktop/lgpt/lgpt_DEB/bin
exec ./lgpt.rpi-exe
sudo shutdown -h now
sudo poweroff
sudo halt
As you can see I tried the 3 methods I found to shut down but none of these works. Lgpt.rpi-exe is successfully started, in fullscreen, but when I quit it, the raspberry pi is not shutting down, nothing happen.

pig.sh is started with autostart file in same folder which is like this :

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
@point-rpi
@sh /home/pi/.config/lxsession/LXDE-pi/pig.sh

I tried also inserting the shuting down command at the end of auto start file. If I do that, it works but it shut down just after pig.sh is started. It is supposed to do it only when I quit the program, not at startup.
Last edited by MazHoot on Wed Dec 16, 2020 1:52 pm, edited 1 time in total.

markkuk
Posts: 265
Joined: Thu Mar 22, 2018 1:02 pm
Location: Finland

Re: shutting down in sh script don't work after exec

Wed Dec 16, 2020 11:18 am

The shell builtin command "exec" replaces the currently running shell with the command given as parameter: https://www.gnu.org/software/bash/manua ... l-Builtins
This means the lines after "exec" are unreachable. Run the command normally without exec if you want the script to continue after the command finishes.

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

Re: shutting down in sh script don't work after exec

Wed Dec 16, 2020 12:56 pm

MazHoot wrote:
Tue Dec 15, 2020 11:50 pm
.....
I tried also inserting the shuting down command at the end of auto start file. If I do that, it works but it shut down just after pig.sh is started. It is supposed to do it only when I quit the program, not at startup.
That is normal behavior. Any commands put directly in autostart are all started at near the same time and are run independently.
See my write-up here: viewtopic.php?f=66&t=294014
Unless specified otherwise my response is based on the latest and fully updated RPiOS Buster w/ Desktop OS.

MazHoot
Posts: 7
Joined: Tue Dec 15, 2020 11:40 pm

Re: shutting down in sh script don't work after exec

Wed Dec 16, 2020 1:51 pm

@markkuk

It works!! Thank you!! I was stuck on this since several days! So I just removed "exec" (my command is sudo ./lgpt-rpi-exe and not exec sudo ./lgpt-rpi-exe) and it works.

Return to “General discussion”