You can use 'lsof' to do it and probably that's the same scheme which is used by Puppy.
But you have to know that it is still not 100% safe. Because a file system allows concurrent access, at one moment your check says the file is not opened, but as soon as you start the copying and deleting the copy on the ramdisk, another process starts opening it for modification or deletion. You can imagine it will result in various confusion to your file system.
A-Tech 8GB DDR3 1600 PC3-12800 Laptop SODIMM 204-Pin Memory RAM PC3L DDR3L 1x 8G
$13.99
Crucial DDR3L 16GB 1600 2x 8GB PC3-12800 Laptop SODIMM Memory RAM PC3 16G DDR3
$22.45
Micron 8GB DDR4 1Rx16 PC4-3200AA Laptop RAM Memory MTA4ATF1G64HZ-3G2E2
$10.00
A-Tech 8GB PC3-12800 Desktop DDR3 1600 MHz Non ECC 240-Pin DIMM Memory RAM 1x 8G
$13.99
CORSAIR - VENGEANCE LPX 32GB (2x16GB) 3200MHz DDR4 C16 DIMM Desktop Memory
$59.99
SK Hynix 8GB (1x8GB) PC4-19200 DDR4-2400T Laptop Memory SDRAM HMA81GS6AFR8N-UH
$10.99
Crucial 8GB SO-DIMM PC3-12800 (DDR3-1600) Memory
$9.49
HyperX FURY RAM DDR4 16GB 8GB 32GB 4GB 3200 2666 2400 2133 Desktop Memory DIMM
$9.64
LOT 8x 16GB (128GB) Samsung M393B2G70DB0-CMA PC3-14900R DIMM Server Memory
$35.99
Set Of 10 Samsung (1 x 8GB) PC4-21300 So DIMM So(DDR4-2666) Memory - SET OF 10
$100.00