I have the following function set up which looks at a given directory and decompresses any files using qpress. It also records how long the process runs for.

fc_decompress () {
    startTime="$(date +%s)";
    find "$1/" -type f -iname '*.qp' -exec sh -c 'for f in "$@"; do /usr/bin/qpress -d "$f" "$(dirname "$f")" && rm "$f"; done' find-sh {} +
    endTime="$(date +%s)";
    runTime=$(( ${endTime}-${startTime} ));
    echo "Total Run Time: ${runTime}";

If I run the function as:

fc_decompress "${testDir}"

It works fine. However, on some occasions this can be a lengthy process so I figured I would run it as:

nohup sh -c 'fc_decompress "${testDir}"' &

But this no longer works, giving the error:

sh: fc_decompress: command not found

How can I pass the function into the nohup command so I can set it running and walk away?

  • see stackoverflow.com/q/16435629/10622916 – Bodo Feb 13 at 15:32
  • 2
    If you're starting that from a shell with job control enabled, you can simply run it as (fc_decompress "{testDir}" &). That won't be immune to SIGHUP, but won't be sent a SIGHUP either if the terminal is torn down. If that's not enough, you can trap the SIGHUP. Look here for a lengthier discussion and examples. – mosvy Feb 13 at 15:45
  • basically, you can run your function with bg_nohup(){ (trap '' HUP; "$@" &) }; bg_nohup func args... – mosvy Feb 13 at 15:52
  • OK cheers, the (fc_decompress "{testDir}" &) seems to do what I need. – IGGt Feb 13 at 16:03

Your Answer

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

Browse other questions tagged or ask your own question.