[GRASS-dev] Python scripts: trigger cleanup function at keyboard interrupt (CTRL-C)

Hi,

in the GRASS GIS 6 shell scripts we used this code to clean up temp
files upon CTRL-C signal:

cleanup()
{
   g.message -v "Cleaning up ..."
   cd "$MSET_DIR"
   rm -rf "$TMP_DIR"
}
#### trap ctrl-c so that we can clean up tmp
trap 'cleanup' 2 3 15

If there anything similar we could add to the python scripts?

thanks
Markus

This is used in many scripts and should work:
https://grasswiki.osgeo.org/wiki/GRASS_Python_Scripting_Library#Sophisticated_cleanup_procedure

On Mon, Oct 14, 2019 at 10:00 AM Markus Neteler <neteler@osgeo.org> wrote:

Hi,

in the GRASS GIS 6 shell scripts we used this code to clean up temp
files upon CTRL-C signal:

cleanup()
{
g.message -v “Cleaning up …”
cd “$MSET_DIR”
rm -rf “$TMP_DIR”
}

trap ctrl-c so that we can clean up tmp

trap ‘cleanup’ 2 3 15

If there anything similar we could add to the python scripts?

thanks
Markus


grass-dev mailing list
grass-dev@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-dev

On 14/10/19 16:44, Anna Petrášová wrote:

This is used in many scripts and should work:
https://grasswiki.osgeo.org/wiki/GRASS_Python_Scripting_Library#Sophisticated_cleanup_procedure

Not sure this works on a Ctrl-C. ISTR it doesn't, but this would have to be verified.

Moritz

On Mon, Oct 14, 2019 at 10:00 AM Markus Neteler <neteler@osgeo.org <mailto:neteler@osgeo.org>> wrote:

    Hi,

    in the GRASS GIS 6 shell scripts we used this code to clean up temp
    files upon CTRL-C signal:

    cleanup()
    {
      g.message -v "Cleaning up ..."
      cd "$MSET_DIR"
      rm -rf "$TMP_DIR"
    }
    #### trap ctrl-c so that we can clean up tmp
    trap 'cleanup' 2 3 15

    If there anything similar we could add to the python scripts?

    thanks
    Markus
    _______________________________________________
    grass-dev mailing list
    grass-dev@lists.osgeo.org <mailto:grass-dev@lists.osgeo.org>
    https://lists.osgeo.org/mailman/listinfo/grass-dev

_______________________________________________
grass-dev mailing list
grass-dev@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-dev

On Mon, Oct 14, 2019 at 5:04 PM Moritz Lennert
<mlennert@club.worldonline.be> wrote:

On 14/10/19 16:44, Anna Petrášová wrote:
> This is used in many scripts and should work:
> https://grasswiki.osgeo.org/wiki/GRASS_Python_Scripting_Library#Sophisticated_cleanup_procedure

Thanks for the link (now I remember again).

Not sure this works on a Ctrl-C. ISTR it doesn't, but this would have to
be verified.

It doesn't:

## LL location:
# random command
r.in.srtm.region -1 user="Neteler4520" password="13Heymel.@nasa"
output=srtm_sicily_1arc memory=2000 --o
Importing 4 SRTM tiles...
^C^C^C 100%
^C^CTraceback (most recent call last):
  File "/home/mneteler/.grass7/addons/scripts/r.in.srtm.region", line
480, in <module>
    main()
  File "/home/mneteler/.grass7/addons/scripts/r.in.srtm.region", line
406, in main
    quiet = True)
  File "/home/mneteler/software/grass78_git/dist.x86_64-pc-linux-gnu/etc/python/grass/script/core.py",
line 496, in read_command
    stdout, stderr = process.communicate()
  File "/usr/lib64/python3.7/subprocess.py", line 926, in communicate
    stdout = self.stdout.read()
KeyboardInterrupt
^CError in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "/usr/lib64/python3.7/subprocess.py", line 990, in wait
    return self._wait(timeout=timeout)
  File "/usr/lib64/python3.7/subprocess.py", line 1624, in _wait
    (pid, sts) = self._try_wait(0)
  File "/usr/lib64/python3.7/subprocess.py", line 1582, in _try_wait
    (pid, sts) = os.waitpid(self.pid, wait_flags)
KeyboardInterrupt

The function atexit() is used:

grep atexit $HOME/.grass7/addons/scripts/r.in.srtm.region
import atexit
    atexit.register(cleanup)

We discussed this in March 2018, but yet without result:

pygrass: How to allow Ctrl-C to stop all subprocesses of GridModule
https://lists.osgeo.org/pipermail/grass-dev/2018-March/087732.html
... and following messages.

Markus