I am trying to run a cleanup script that gets invoked on every core dump. I first wrote the contents of the core file to another file, and after then I tried to do a cleanup on the oldest ten files.

However, the xargs rm doesn't work when the script gets invoked via a core_dump. When I run the script by itself, the rm works.


|core_cleanup.sh /tmp/cores/core.%t

cleanup.sh file

cat > "$1"
ls -tr /tmp/cores/ | head -10 | xargs rm

If I instead do the below to delete files, it works. But I need to delete only the oldest x files.

cat > "$1"
rm /tmp/cores

Can someone shed some light?

  • Redirect stderr and stdout to a file in /tmp to see the error message. Find out the user that runs the script and whether restrictions from SELinux apply.
    – RalfFriedl
    Sep 21 '18 at 17:34
  • Does the cat redirection work? (Is the script being invoked?)
    – Jeff Schaller
    Sep 21 '18 at 19:03
  • @JeffSchaller the script is being invoked and the cat redirection works. But xargs doesn't. When I modified the statement below cat to just do an rm /tmp/cores/, it also works. Seems to be an issue with xargs. Is there an alternative I should try?
    – Mozbi
    Sep 21 '18 at 19:08
  • 1
    Ralf has a good point about SELinux possibly preventing the removal; is sestatus reporting "enforcing" mode, and if so, what's the context in ls -Z /tmp/cores?
    – Jeff Schaller
    Sep 21 '18 at 19:24
  • 1
    @JeffSchaller sestatus shows disabled.
    – Mozbi
    Sep 21 '18 at 20:32

ls -tr /tmp/cores doesn't list the full paths of the files, but just their basenames.

Try ls -tr /tmp/cores/* | ... instead.

But you don't need all that -- since all file names are of the form core.%t (%t = unix time of dump), you can simply rely on glob to sort them. Also, it doesn't make sense to remove 10 files after you're adding just one -- after a number of steps, you will end up with no files in the directory.

#! /bin/sh
cat > "$1"
# remove the oldest file if there are more than 100 
cleanup(){ test "$#" -gt 100 && rm "$1" }
cleanup /tmp/cores/*
  • well-spotted with the base names and the %t glob-sorting! I might suggest testing for more than 2 instead of 10, as that seems to be the requirement.
    – Jeff Schaller
    Sep 22 '18 at 13:13
  • @mosvy what if the core patterns are of the form: core.%e.%s.%t?
    – Mozbi
    Sep 23 '18 at 20:26
  • @JeffSchaller ^
    – Mozbi
    Sep 23 '18 at 20:28
  • @Mozbi I think it’d be fair to open a new question focused on removing the X oldest files based on that (different) core pattern. Let mosvy Answer this question as asked.
    – Jeff Schaller
    Sep 23 '18 at 20:31
  • 1
    @Mozbi you change them to core.%t.%s.%e -- or even better, core.%t.%p.%s.%e (include the pid too, since a program may crash more than once per second).
    – mosvy
    Sep 23 '18 at 21:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.