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.
Watchguard XCS 570 Firewall SuperMicro 1U Server Intel 4GB VPN Router LINUX 🍁
$182.36
1U BareMetal pfsense opnsense Router Firewall DNS Server 6x 10GB Ethernet Ports
$149.00
PFSENSE 15" Depth Server Router Firewall Supermicro X11SSH-F E3-1240 V5 32GB RAM
$382.00
POGO Linux WEBWARE 1150 Rack-Mount Server Pentium 4 2.8GHz 512MB - No Drives
$179.99
Domino Lotus Server 5.0.7 for OS/2 WSeB, RH Linux 6, Solaris, AIX, HP-UX, Win NT
$60.00
IBM CS821 20-Core 2.827GHz 128Gb 1.92Tb SSD 1U Linux Server - 8005-12N Power 8
$449.96
Red Hat Enterprise Linux 5 Server - New and Sealed
$16.99
2 x HP ProLiant BL460c (447707-B21) Blade Servers No RAM No HDD
$30.00
8x 240GB SSD 1U Rackmount Deduplication Compression Backup RAID Server X10DRW-iT
$499.00
VPS Windows / Linux Server - 12GB RAM, 4 Core, 1 TB HD, Unltd. bandwidth 1 Year
$272.98