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?
touchan option? – Peregrino69 Sep 26 '21 at 08:52-noption supposed to do? I've never heard oftempfilebefore and have been usingmktempfor many years. It seems, from an old man page I found online, that-njust gives a specific name for the "temp" file which seems very odd: if you already know the name, why would you needtempfileto 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_EXCLis. My point is that if you don't need a tool likemktempthat 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:39touchfor this. Could you please explain further? – Steve Piner Sep 26 '21 at 09:53