Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Shells Virtual Desktop
BMail.ag - Secure Email Service
Server.net
CPLicense.net
VPS Server
Buy VPN
Vultr
VMs for AI
HostDare
HostDare
ReliableSite White-Label Dedicated Hosting for Resellers
25% Recurring Discount on NVMe VPS
InterServer VPS
BMail.ag - Secure Email Service
Best VPN
High-Performance Bare Metal Server Solutions
Karvl.com
Server Mania Cloud Hosting
DataWagon Hosting
AlphaVPS Hosting
Evoxt.com
Clouvider
VPS Hosting with NVMe
Residential IPs in the US & 4G Mobile Proxies in EU & US with Unlimited Bandwidth
ReliableSite White-Label Dedicated Hosting for Resellers
Rabisu - Hosting Solutions
Shells Virtual Desktop
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

Here Today, Gone When You Exit: Proper Tempfiles in Shell Scripts

raindog308raindog308 Administrator, Veteran

Earlier this week I inherited a pile of utterly craptacular shell scripts (running in production, naturally) at work and spend the week essentially rewriting them so they didn't look like they'd been bought on fiverr.

I was told "weird things happen sometimes and we can't figure out a pattern...and the busier we are, the more weird things happen".

It was pretty easy to find the reason why...this identical line was in many scripts that frequently executed:

TEMPFILE=/tmp/tempfile

Every execution was a timing Russian roulette.

Although in one script they used

TEMPFILE=/tmp/tempfile.$$

...and just never rm'd it so there were tens of thousands of tempfiles in /tmp.

As I cleaned up I wrote up a tutorial on how to use tempfiles properly, and how to automatically clean them up when your script exits. It's easy to clean up one, but cleaning up multiple requires some extra thought.

I had a couple solutions in mind (UUID patterns, using an array) but also found a 2009 LJ article with a third approach.

Enjoy: https://lowendbox.com/blog/here-today-gone-when-you-exit-proper-tempfiles-in-shell-scripts/

Comments

  • jsgjsg Member, Resident Benchmarker

    BUMP

    because that nice tutorial doesn't deserve to drop so quickly

  • AlwaysSkintAlwaysSkint Member
    edited April 2022

    I like a KISS philosophy:
    TEMPFILE=/tmp/tempfile.$$
    .. along with a daily cron that does..
    /usr/bin/find //tmp/tempfile.* -mtime +60 -exec rm {} \; > /dev/null 2>&1

    This may suffice.

    I enjoyed that article, which reminded me that even with a formal sysadmin training, you can never know all the nuances of bash scripting etc. ;)
    (I only remembered enough to do slightly more than basic tasks.)

    Thanked by 1PandaRain
  • LisoLiso Member

    Nice article

  • At first, I thought the title was an article about exit scammers. Here today, gone tomorrow.

  • raindog308raindog308 Administrator, Veteran

    @AlwaysSkint said: .. along with a daily cron that does..

    So handling a single tempfile cleanup with trap is easy.

    But to automatically clean up tempfiles, between you and the article we've now listed:

    • using a UUID pattern
    • using a temp directory
    • using a function to "register" tempfiles and add them to a list
    • not worrying about them and cleaning up aged tempfiles via cron

    Seems to me like there is a missing feature here :-)

    Though now that I think about it, I don't think either perl or python offer a "guaranteed cleanup" when they exit unless you code it yourself.

  • edited April 2022

    @raindog308 said: I don't think either perl or python offer a "guaranteed cleanup" when they exit

    Not as a built-in.

    unless you code it yourself.

    There are libraries out there that do that sort of thing, but they expect you to call the “clean up” function as you exit, and register a global exception handler (or whatever your language/framework supports to this effect) and/or a signal handler to fire that automatically upon uncontrolled exit. You might still have some left around to remove by other means, due to really uncontrolled exits.

    Also take care when using mktemp to pick an arbitrary name to avoid conflicts – for some reason a lot of people assume such files are not created until you output something when they are created as zero-length. I've seen scripts create one initially whether they need it or not, then never remove it assuming that not having used it means it didn't really get created.

    (or empty, if creating a directory) by mktemp itself. (edit: actually looked at the linked article, and it explicitly shows cleaning up in repose to bash's EXIT signal – note that this is a bashism so don't rely on it unless you explicitly specify bash as the script runner instead of relying on the OS/user's default)

    Thanked by 1raindog308
  • Have you tried systemd-tmpfiles? It looks pretty neat.

    Thanked by 1raindog308
  • raindog308raindog308 Administrator, Veteran

    @MeAtExampleDotCom said: (or empty, if creating a directory) by mktemp itself. (edit: actually looked at the linked article, and it explicitly shows cleaning up in repose to bash's EXIT signal – note that this is a bashism so don't rely on it unless you explicitly specify bash as the script runner instead of relying on the OS/user's default)

    Yes...I think in other shells you have to trap each signal.

  • raindog308raindog308 Administrator, Veteran

    @__no_preserve_root said: Have you tried systemd-tmpfiles? It looks pretty neat.

    The only negative is that I think you either have to have root or play by root's rules (put files where they'll be cleaned up), or convince someone with root to put in new rules for you. But interesting, and apparently it's been around for quite a while. Here's an overview from 2016:

    https://developers.redhat.com/blog/2016/09/20/managing-temporary-files-with-systemd-tmpfiles-on-rhel7

  • imagine not using pastebin for temporary files

  • Nice article. If you're having to make complex bash scripts, especially in a team environment, this is a good read too:
    https://google.github.io/styleguide/shellguide.html

    Thanked by 1raindog308
  • raindog308raindog308 Administrator, Veteran

    Today I learned why I don't want to work for Google.

    Forcing others to use your preferences for indentation is evil.

Sign In or Register to comment.