I have a script that creates a temporary file as a flag to guard against the script being run simultaneously. Currently it uses tempfile
, e.g.
if ! tempfile -n /tmp/updating > /dev/null; then
echo 'Another synchronization is currently running' >&2
exit 1
fi
The tempfile
program is now deprecated, it suggests using mktemp
instead, but mktemp
doesn't seem to have an option similar to -n
.
I'm using Ubuntu 21.04.
So how should I safely create a flag file?
touch
an option? – Peregrino69 Sep 26 '21 at 08:52-n
option supposed to do? I've never heard oftempfile
before and have been usingmktemp
for many years. It seems, from an old man page I found online, that-n
just gives a specific name for the "temp" file which seems very odd: if you already know the name, why would you needtempfile
to create it? Why not just make your own lockfile name like/tmp/thisisalongfilenamethatonlymyprogramnamedbestprogramwoulduse
? – terdon Sep 26 '21 at 09:03O_EXCL
? How can you do that from the shell? – Sep 26 '21 at 09:38O_EXCL
is. My point is that if you don't need a tool likemktemp
that will create a unique, random name and instead you already know the name you'll be using, all you need isif [[ -e $file ]]
. – terdon Sep 26 '21 at 09:39touch
for this. Could you please explain further? – Steve Piner Sep 26 '21 at 09:53