0

I have 2 scripts.

I'm calling my script2 as sh scriptpath/script2.sh & inside script1 which compresses files using a combination of find, xargs and gzip commands on 16 files at a time. It is basically a file watcher script integrated to run the process below after checking if a file is present. (Not cron)

Reference: https://it.toolbox.com/question/file-watcher-script-070510

find ${Filepath}/ -maxdepth 1 -type f -name "${Pattern}" -print0 | xargs -0 -t -n 1 -P 16 gzip > /dev/null

After calling my script 2, it's hanging at the command above.

Script1 session is getting closed and script2's shell is opening up with above command status. I need the gzip command of the second script to be run in the background and not the foreground.

Script1 - Generates few files. Exports variables to be used in script2

And then calls script2 as sh script2 needed parameters & (ampersand to push script2 to go into background) and script1 completes, but after script 2 finds one touch file. It begins its execution. But the prompt of script2 where gzip is executing after it finds touch file is coming to foreground.

Script2 Gunzips files created before calling script 2

fileflag=0
timer1=0
check_interval=300 # check every 5 minutes
(( check_interval_minutes=${check_interval}/60 ))
while [ ${timer1} -lt 180 ]
do
if [ -f /path/to/my/file ]
then
find ${Filepath}/ -maxdepth 1 -type f -name "${Pattern}" -print0 | xargs -0 -t -n 1 -P 16 gzip > /dev/null
else
sleep ${check_interval}
fi
(( timer1=${timer1} ${check_interval_minutes} ))
done
5
  • 1
    Please edit your question and show us your scripts. I am afraid it is impossible to debug your code if you cannot share it. If possible, try to make a simplified example that still reproduces the problem: remove anything not relevant and show us a minimal example of your two scripts that we can use to test our solutions.
    – terdon
    Jul 3, 2020 at 17:29
  • Done..please check and try to answer
    – Mohammed
    Jul 3, 2020 at 18:06
  • Maybe not relevant to the problem but timer1=${timer1} ${check_interval_minutes} is wrong. I guess a + is missing there. Jul 3, 2020 at 20:44
  • What happens if you just press enter? Jul 3, 2020 at 20:49
  • Oh this is reference code, i have added + in my code already
    – Mohammed
    Jul 4, 2020 at 3:47

2 Answers 2

0

xargs -t writes to stderr. Your > /dev/null doesn't affect stderr. So you are writing to the terminal from a background process which usually is a bad idea.

1
  • Thanks Hauke, it worked !
    – Mohammed
    Jul 6, 2020 at 5:02
0

If you want the find/gzip process to happen in the background you can run that entire block in the background by using something like:

( find ${Filepath}/ -maxdepth 1 -type f -name "${Pattern}" -print0 | xargs -0 -t -n 1 -P 16 gzip > /dev/null ) &

However, depending on how long it takes for all those gzip processes to finish that might be unwise, if it takes longer than the interval between when your script runs you may end up with more than one process attempting to compress the same file.

You would probably be better off using something like inotifywatch to watch the directory for files to appear and compress them.

3
  • my gzip happens in seconds, but command still hangs. Standalone script 2 runs fine but while calling from script 1 to 2 gzip command command is coming to foreground even after its successful execution and just staying in there
    – Mohammed
    Jul 3, 2020 at 20:40
  • There is no need for the brackets around the command. Jul 3, 2020 at 20:50
  • Your answer does not address the xargs -t problem in the background. Jul 5, 2020 at 0:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .